1

我目前正在尝试使用 ffmpeg 和 ffmpy 将多个视频文件与 python 脚本合并。正如ffmpeg concatenate wiki所建议的那样,文件的名称被写入文件列表。

在我的示例中,我只使用了两个文件,但实际上,会有几个 hundert 文件,这就是我选择文件列表方法的原因。

我当前的代码如下所示:

import os
import ffmpy


base_dir = "/path/to/the/files"

# where to seek the files
file_list = open("video_list.txt", "x")

# scan for the video files
for root, dirs, files in os.walk(base_dir):
    for video_file in files:
        if video_file.endswith(".avi"):
            file_list.write("file './%s'\n" % video_file)

# merge the video files
ff = ffmpy.FFmpeg(
    global_options={"-f",
                    "concat ",
                    "-safe",
                    "0"},
    inputs={file_list: None},
    outputs={"-c",
             "copy",
             "output.avi"},
)
ff.run()

所以我想用 ffmpy 运行的代码是

ffmpeg -f concat -safe 0 -i video_list.txt -c copy output.avi

但不幸的是,我的脚本不起作用,导致的错误是

Traceback (most recent call last):
  File "concat.py", line 20, in <module>
    "output.avi", }
  File "/usr/lib/python3.7/site-packages/ffmpy.py", line 54, in __init__
    self._cmd += _merge_args_opts(outputs)
  File "/usr/lib/python3.7/site-packages/ffmpy.py", line 187, in _merge_args_opts
    for arg, opt in args_opts_dict.items():
AttributeError: 'set' object has no attribute 'items'

任何提示为什么该命令没有按应有的方式工作?我是否遗漏了有关 ffmpy 的命令格式的内容?

谢谢你。

4

1 回答 1

2

作为一种解决方法,我能够使用子进程例程调用 ffmpeg,因为 ffmpy 仍然让我头疼。如果其他人有这个问题,这是我正在使用的代码

import os
import subprocess
import time


base_dir = "/path/to/the/files"
video_files = "video_list.txt"
output_file = "output.avi"

# where to seek the files
file_list = open(video_files, "w")

# remove prior output
try:
    os.remove(output_file)
except OSError:
    pass

# scan for the video files
start = time.time()
for root, dirs, files in os.walk(base_dir):
    for video in files:
        if video.endswith(".avi"):
            file_list.write("file './%s'\n" % video)
file_list.close()

# merge the video files
cmd = ["ffmpeg",
       "-f",
       "concat",
       "-safe",
       "0",
       "-loglevel",
       "quiet",
       "-i",
       "%s" % video_files,
       "-c",
       "copy",
       "%s" % output_file
       ]

p = subprocess.Popen(cmd, stdin=subprocess.PIPE)

fout = p.stdin
fout.close()
p.wait()

print(p.returncode)
if p.returncode != 0:
    raise subprocess.CalledProcessError(p.returncode, cmd)

end = time.time()
print("Merging the files took", end - start, "seconds.")
于 2018-11-10T20:55:26.493 回答