我有一個功能,可以在臨時文件夾中找到mp4視頻,將其轉換爲單獨文件夾中的mp3(稱爲音樂),然後從temp文件夾中刪除mp4。它看起來像這樣:如何在python完成後停止ffmpeg
import imageio
imageio.plugins.ffmpeg.download()
import moviepy.editor as mp
def convert_mp4_to_mp3():
video_file = os.listdir(os.getcwd() + '//temp')[0]
audio_file = video_file.replace('mp4','mp3')
clip = mp.VideoFileClip(os.getcwd() + '//temp//' + video_file)
clip.audio.write_audiofile(os.getcwd() + '//music//' + audio_file)
os.system("taskkill /f /im ffmpeg.win32.exe*32")
os.remove(os.getcwd() + '//temp//' + video_file)
功能成功地完成MP4轉換爲MP3,但是當我嘗試刪除該文件,我得到以下錯誤:
PermissionError: [WinError 32] The process cannot access the file because it
is being used by another process
在嘗試手動刪除文件,我發現它是因爲「ffmpeg.win32.exe」仍然在打開文件時運行。在我殺死那個進程之前,我無法手動刪除它。
我嘗試實現第二到最後一行(使用os.system(「TASKKILL/F/IM ffmpeg.win32.exe * 32」),以終止該進程,但我仍然拿出同樣的錯誤。
所以我的問題是,是否有一個簡單的方法來殺死ffmpeg,一旦我知道我已經完成了它,或者有一種解決方法,以某種方式殺死它通過另一個進程?我在python3.6在Windows 7 pc上工作。對於幫助!
嘗試調用'clip.close()'而不是殺死ffmpeg。 –
我得到一個錯誤「'VideoFileClip'對象沒有'close'屬性」 –
這是否可以是你的代碼的真實副本,其中有雙斜線('//')? –