gpt4 book ai didi

python-2.7 - 从脚本运行 ffmpeg 命令时没有这样的文件或目录

转载 作者:行者123 更新时间:2023-12-03 23:55:56 25 4
gpt4 key购买 nike

我一直在尝试运行这个 ffmpeg来自 python 脚本的命令从静态图像生成一定长度的视频,但我不断收到 No such file or directory 错误!
这是我的代码:

import subprocess

def generate_white_vid (duration):
ffmpeg_create_vid_from_static_img = 'ffmpeg -loop 1 -i /same_path/WhiteBackground.jpg -c:v libx264 -t %f -pix_fmt yuv420p -vf scale=1920:1080 /same_path/white_vid2.mp4' % duration
print ffmpeg_create_vid_from_static_img
pp = subprocess.Popen(ffmpeg_create_vid_from_static_img)
pp.communicate()

generate_white_vid(0.5)

但是,当我运行相同的命令时:
ffmpeg -loop 1 -i /same_path/WhiteBackground.jpg -t 0.500000 -pix_fmt yuv420p -vf scale=1920:1080 /same_path/white_vid2.mp4
从cli,它工作得很好。我错过了哪里?
这是完整的跟踪:
Traceback (most recent call last):
File "gen.py", line 10, in <module>
generate_white_vid(0.5)
File "gen.py", line 7, in generate_white_vid
pp = subprocess.Popen(ffmpeg_create_vid_from_static_img)
File "/home/ubuntu/anaconda2/lib/python2.7/subprocess.py", line 390, in __init__
errread, errwrite)
File "/home/ubuntu/anaconda2/lib/python2.7/subprocess.py", line 1024, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory

当我使用列表传递 ffmpeg命令参数如下 ffmpeg_create_vid_from_static_img = ['ffmpeg', '-loop', '1', '-i', '/same_path/WhiteBackground.jpg', '-c:v', 'libx264', '-t', duration, '-pix_fmt', 'yuv420p', '-vf', 'scale=1920:1080', '/same_path/white_vid.mp4'] ,我得到一个类型错误:
TypeError                                 Traceback (most recent call last)
<ipython-input-137-4a9e12e42310> in <module>()
----> 1 generate_white_vid(0.5)

<ipython-input-136-edfbfc017557> in generate_white_vid(duration)
3 ffmpeg_create_vid_from_static_img = ['ffmpeg', '-loop', '1', '-i', '/home/ubuntu/matar/multispectral/WhiteBackground.jpg', '-c:v', 'libx264', '-t', duration, '-pix_fmt yuv420p', '-vf', 'scale=1920:1080', '/home/ubuntu/matar/multispectral/white_vid.mp4']
4 print ffmpeg_create_vid_from_static_img
----> 5 pp = subprocess.Popen(ffmpeg_create_vid_from_static_img)
6 pp.communicate()

/home/ubuntu/anaconda2/lib/python2.7/subprocess.pyc in __init__(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags)
388 p2cread, p2cwrite,
389 c2pread, c2pwrite,
--> 390 errread, errwrite)
391 except Exception:
392 # Preserve original exception in case os.close raises.

/home/ubuntu/anaconda2/lib/python2.7/subprocess.pyc in _execute_child(self, args, executable, preexec_fn, close_fds, cwd, env, universal_newlines, startupinfo, creationflags, shell, to_close, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite)
1022 raise
1023 child_exception = pickle.loads(data)
-> 1024 raise child_exception
1025
1026

TypeError: execv() arg 2 must contain only strings

最佳答案

由于shell=FalsePopen 的默认值,您通常需要将命令行构造为 单独的元素列表 (顺便说一句,这更安全)。

但是,如果您只想将命令作为字符串传递,则必须使用 shell=True将是必需的。

关于python-2.7 - 从脚本运行 ffmpeg 命令时没有这样的文件或目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47460177/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com