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

时间:2015-05-27 10:46:43

标签: python ffmpeg subprocess

我有这个剧本:

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并在终端中手动粘贴,没有任何反应,它只是卡住,但如果我尝试手动复制所有打印的参数,它就可以了。

3 个答案:

答案 0 :(得分:2)

subprocess.Popen需要字符串列表,例如[ffmpeg, arg1, ...]

此命令在Linux上失败:

subprocess.Popen("ls -la").wait()

虽然这个成功了:

subprocess.Popen(["ls", "-la"]).wait()

答案 1 :(得分:0)

传递一个args列表并使用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

答案 2 :(得分:0)

有同样的问题。同时安装了brew的Python 3.7和ffmpeg。就像您一样,在终端中工作,但不是(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)