根据Python中文件名中的数字按顺序组合mp4文件 [英] Combine mp4 files by order based on number from filenames in Python

查看:65
本文介绍了根据Python中文件名中的数字按顺序组合mp4文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 ffmpeg 将目录 test 中的许多 mp4 文件合并到一个 output.mp4 中在Python中.

I try to merge lots of mp4 files from a directory test into one output.mp4 using ffmpeg in Python.

path = '/Users/x/Documents/test'

import os

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

我从以下参考文献中尝试了以下解决方案: ffmpy将多个文件与文件列表连接起来

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

我已经合并它们并得到一个 output.mp4 ,但是文件未按第一个按点分隔的数字顺序进行合并( 1、2、3,... ):我可以通过 filename.split(.")[0] :

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

如何在Python中正确,简洁地合并它们?谢谢.

How can I merge them correctly and concisely in Python? Thanks.

推荐答案

此解决方案有效:

from moviepy.editor import *
import os
from natsort import natsorted

L =[]

for root, dirs, files in os.walk("/path/to/the/files"):

    #files.sort()
    files = natsorted(files)
    for file in files:
        if os.path.splitext(file)[1] == '.mp4':
            filePath = os.path.join(root, file)
            video = VideoFileClip(filePath)
            L.append(video)

final_clip = concatenate_videoclips(L)
final_clip.to_videofile("output.mp4", fps=24, remove_temp=False)

这篇关于根据Python中文件名中的数字按顺序组合mp4文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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