ffmpy用文件列表连接多个文件 [英] ffmpy concatenate multiple files with a file list

查看:213
本文介绍了ffmpy用文件列表连接多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm currently trying to merge multiple video files with a python script using ffmpeg and ffmpy. The names of the files are being written into a file list, as suggested by the ffmpeg concatenate wiki.

在我的示例中,我仅使用两个文件,但是在实践中,将有多个hundert文件,这就是为什么我选择该文件列表方法。

In my example I'm only using two files, but in practice, there will be several hundert files, that's why I'm choosing the file list approach.

我当前的代码如下:

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运行的代码是

So the code I want to run with ffmpy is

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

但是不幸的是,我的脚本无法正常工作,并且导致的错误是

But unfortunately my script isn't working and the resulting error is

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的命令格式的信息?

Any hints why the command isn't working the way it should? Am I missing something regarding the command formatting for ffmpy?

谢谢。

推荐答案

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

As a working workaround, I was able to call ffmpeg with a subprocess routine, since ffmpy still gave me headache. If somebody else has this problem, here is the code I'm using

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.")

这篇关于ffmpy用文件列表连接多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆