我有这个脚本:
PATH = os.path.dirname(os.path.abspath(__file__))
global TEMP
for video in os.listdir(VIDEOS):
ffmpeg = PATH + "/ffmpeg/ffmpeg"
arg1 = " -v 0 -i "
arg2 = VIDEOS + "/" + video
arg3 = " -r 1 -f image2 "
arg4 = TEMP + "/" + os.path.splitext(video)[0] + "-%d.jpg"
subprocess.Popen(ffmpeg + arg1 + arg2 + arg3 + arg4).wait()
在Windows上工作完美(当然使用ffmpeg.exe),但当我试图在Mac上运行它时,我得到了错误:
File "/Users/francesco/Desktop/untitled0.py", line 20, in Main
subprocess.Popen(ffmpeg + arg1 + arg2 + arg3 + arg4).wait()
File "subprocess.pyc", line 710, in __init__
File "subprocess.pyc", line 1327, in _execute_child
OSError: [Errno 2] No such file or directory
我已经尝试打印ffmpeg + arg1 + arg2 + arg3 + arg4并手动粘贴到终端中,没有发生任何事情,它只是卡住了,但如果我尝试手动复制所有打印的参数,它可以工作。
subprocess.Popen
需要字符串列表,类似于[ffmpeg, arg1, ...]
。
该命令在Linux上失败:
subprocess.Popen("ls -la").wait()
当这个成功时:
subprocess.Popen(["ls", "-la"]).wait()
传递一个参数列表,如果你想等待进程返回,使用check_call
:
from subprocess import check_call
for video in os.listdir(VIDEOS):
check_call(["ffmpeg","-v", "0", "-i","{}/{}".format(VIDEOS,video), "-r", "1", "-f",
"image2","{}/-%d.jpg".format(TEMP), os.path.splitext(video)[0]])
check_call将为任何非零退出状态引发CalledProcessError
有同样的问题。Python 3.7和ffmpeg,都安装在brew中。就像您一样,在终端中工作,但不是作为(CRON)脚本。原来,问题是没有指定ffmpeg的完整路径,在我的情况下是"/usr/local/Cellar/ffmpeg/4.1.3/bin/ffmpeg"。所以
[…]
import os
theCommand = "/usr/local/Cellar/ffmpeg/4.1.3/bin/ffmpeg -i /Volumes/ramDisk/audio.mp4 -i /Volumes/ramDisk/video.mp4 -c:a copy -c:v copy /Volumes/ArchiveDisk/final.mp4"
os.system(theCommand)