ffmpeg 子进程无法在 OS X 上打开

ffmpeg subprocess fail to open on OS X

我有这个脚本:

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 如果您想等到进程 returns:

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 will raise a 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)