根据python中的文件名组合订单的mp4文件
我尝试将大量mp4
从目录test
中的文件合并到一个output.mp4
使用ffmpeg
在Python中。
import os
path = '/Users/x/Documents/test'
for filename in os.listdir(path):
if filename.endswith(".mp4"):
print(filename)
输出:
4. 04-unix,minix,Linux.mp4
6. 05-Linux.mp4
7. 06-ls.mp4
5. 04-unix.mp4
9. 08-command.mp4
1. 01-intro.mp4
3. 03-os.mp4
8. 07-minux.mp4
2. 02-os.mp4
10. 09-help.mp4
我已经尝试了以下参考的解决方案:带有文件列表的多个文件
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.")
我已将它们合并并获取output.mp4
,但这些文件并非按顺序合并第一个数字按点(1,2,3,...
):我可以通过filename.split(“。”)[0])[0]
:
1. 01-intro.mp4
2. 02-os.mp4
3. 03-os.mp4
4. 04-unix,minix,Linux.mp4
5. 04-unix.mp4
6. 05-Linux.mp4
7. 06-ls.mp4
8. 07-minux.mp4
9. 08-command.mp4
10. 09-help.mp4
如何 获得。我可以在python中正确合理地合并它们吗?谢谢。
I try to merge lots of mp4
files from a directory test
into one output.mp4
using ffmpeg
in Python.
import os
path = '/Users/x/Documents/test'
for filename in os.listdir(path):
if filename.endswith(".mp4"):
print(filename)
Output:
4. 04-unix,minix,Linux.mp4
6. 05-Linux.mp4
7. 06-ls.mp4
5. 04-unix.mp4
9. 08-command.mp4
1. 01-intro.mp4
3. 03-os.mp4
8. 07-minux.mp4
2. 02-os.mp4
10. 09-help.mp4
I have tried with the solution below from the reference here: ffmpy concatenate multiple files with a file list
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.")
I have merged them and get an output.mp4
but the files are not merged in order with the first number split by point (1, 2, 3, ...
): which I can get by filename.split(".")[0]
:
1. 01-intro.mp4
2. 02-os.mp4
3. 03-os.mp4
4. 04-unix,minix,Linux.mp4
5. 04-unix.mp4
6. 05-Linux.mp4
7. 06-ls.mp4
8. 07-minux.mp4
9. 08-command.mp4
10. 09-help.mp4
How can I merge them correctly and concisely in Python? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
该解决方案有效:
This solution works:
由于框架的大小不同,我的视频被划过并模糊。为了避免使用此视频。这只是该行的一个变化:
尽管框架大小不同,它仍将所有剪辑添加在一起。这是完整的代码:
My videos got crossed up and blurred due to different frame sizes.. to avoid that you can use this. It was only a change to this line:
which adds all the clips together despite the different frame sizes. Heres the full code:
这将创建一个名为
output.mp4
的串联视频文件,来源
this will create a concatenated video file named
output.mp4
in the working directory,,code source