【问题标题】:No such file or directory when running an ffmpeg command from script从脚本运行 ffmpeg 命令时没有这样的文件或目录
【发布时间】:2017-11-23 16:47:41
【问题描述】:

我一直在尝试从 python 脚本运行此 ffmpeg 命令以从静态图像生成一定长度的视频,但我不断收到 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=True 的参数Popen
  • @etopylight 这非常有效,非常感谢。你知道为什么吗?
  • 由于shell=FalsePopen 的默认值,您通常需要将命令行构造为单独的元素列表(顺便说一下更安全) .但是,如果您只想将命令作为字符串传递,则需要强制 shell=True
  • 当我尝试将命令作为单独的元素列表传递时,出现类型错误。有关完整的跟踪堆栈,请参见上文。另外,请您添加一个答案,以便我可以通过接受它来给予您应得的信任。谢谢
  • 谢谢,添加了答案。但是,对于类型错误,我认为错误消息是在告诉您您的 duration 不是字符串,因此您可能想尝试将其转换为字符串。

标签: python-2.7 ubuntu ffmpeg


【解决方案1】:

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

但是,如果您只想将命令作为字符串传递,则需要强制 shell=True

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-24
    • 2016-05-13
    • 1970-01-01
    • 2020-03-08
    • 1970-01-01
    • 2013-04-10
    • 2015-02-20
    • 2021-06-24
    相关资源
    最近更新 更多