python - FFMPEG 文件输出仍在由进程使用
问题描述
我正在尝试完成我的程序的这一部分。在本节中,我试图根据因子变量加快或减慢视频的速度。完成后,我使用moviepy 将其转换为VideoFileClip,然后删除该文件。
if factor <= 2:
system("ffmpeg -i " + paths[dex] + " -vf setpts=" + str(vfactor) + "*PTS -an ./Media/Videos/temp.mp4")
system("ffmpeg -i " + paths[dex] + " -filter:a atempo=" + str(factor) + " -vn ./Media/ShortSounds/temp.mp3")
elif 2 < factor < 4:
factor = round(sqrt(factor), 1)
system("ffmpeg -i " + paths[dex] + " -vf setpts=" + str(vfactor) + "*PTS,setpts=" + str(vfactor) + "*PTS -an ./Media/Videos/temp.mp4")
system("ffmpeg -i " + paths[dex] + " -filter:a atempo=" + str(factor) + ",atempo=" + str(factor) + " -vn ./Media/ShortSounds/temp.mp3")
elif factor > 4:
raise Exception("File " + paths[dex] + " is too long.")
t = VideoFileClip("./Media/Videos/temp.mp4")
t.audio = AudioFileClip("./Media/Videos/temp.mp3")
templist.append(t)
remove("./Media/Videos/temp.mp4")
但是,当代码到达删除命令时,会出现以下错误:
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: './Media/Videos/temp.mp4'
奇怪的是,我可以看到 temp.mp4 文件,它运行得很好。手动运行 temp.mp4 文件时,我从未收到此错误。
我尝试了以下方法:
- 在删除文件之前等待 5、10 和 20 秒。
- 在删除文件之前运行“taskkill -f -im ffmpeg.exe”
- 我浏览了调试器,在删除之前,我检查了任务管理器以查看 ffmpeg 是否仍在运行,但事实并非如此。
你们知道是什么阻碍了这一切吗?我的代码以前在我尝试只做音频时工作,但我正在用视频尝试它,这正在发生。