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

问题描述 投票:1回答:3

我有这个脚本:

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

python ffmpeg subprocess
3个回答
2
投票

subprocess.Popen需要字符串列表,类似于[ffmpeg, arg1, ...]

此命令在Linux上失败:

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

而这一个成功:

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

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


0
投票

有同样的问题。 Python 3.7和ffmpeg,都是用brew安装的。就像你一样,在终端工作,但不是作为(CRON)脚本。原来问题是没有为ffmpeg指定完整的PATH,在我的例子中是“/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)
© www.soinside.com 2019 - 2024. All rights reserved.