根据Python中文件名中的数字按顺序组合mp4文件

时间:2019-07-07 08:35:39

标签: python ffmpeg operating-system subprocess

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

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 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]来获得:< / p>

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中正确,简洁地合并它们?谢谢。

2 个答案:

答案 0 :(得分:1)

import glob
import os
 
def concatenate():
  global stringa
  stringa = "ffmpeg -i \"concat:"
  elenco_video = glob.glob("*.mp4")
  elenco_file_temp = []
  for f in elenco_video:
    file = "temp" + str(elenco_video.index(f) + 1) + ".ts"
    os.system("ffmpeg -i " + f + " -c copy -bsf:v h264_mp4toannexb -f mpegts " + file)
    elenco_file_temp.append(file)
  print(elenco_file_temp)
  for f in elenco_file_temp:
    stringa += f
    if elenco_file_temp.index(f) != len(elenco_file_temp)-1:
      stringa += "|"
    else:
      stringa += "\" -c copy  -bsf:a aac_adtstoasc output.mp4"
  print(stringa)
  os.system(stringa)

这将在工作目录中创建一个名为 output.mp4 的串联视频文件, code source

答案 1 :(得分:0)

此解决方案有效:

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)