简体   繁体   English

如何使用 python 连接一堆视频?

[英]How to Concatenate bunch of videos using python?

So, I have over 5000 small clips that I need to combine.所以,我有超过 5000 个需要组合的小片段。 To apply various custom filter over their names, I want to do it with python.为了在他们的名字上应用各种自定义过滤器,我想用 python 来做。 I have the following code:我有以下代码:

import os
from moviepy.editor import *
os.chdir('D:/videos')
list1, list2 = os.listdir(), []

for i in list1: #filtering
    if i[-6:] != '-l.mp4' and i[-7:] != 'ALT.mp4':
        list2.append(i)
print('Getting Video Info:')

final = VideoFileClip(list2[0])


for i in range(1,len(list2)):
    final = concatenate_videoclips([final, VideoFileClip(list2[i])])
    print('\r' + str(i+1) + '/' + str(len(list2)), end='')


os.chdir('D:')
final.write_videofile('Merged.mp4')

But the program is creating lots of process and just after reading 150 clips it's crashing.但是该程序正在创建大量进程,并且在读取 150 个剪辑后就崩溃了。 在此处输入图像描述 It keeps increasing?一直在增加? Is there any easier way/alternative to do this?有没有更简单的方法/替代方法来做到这一点? Thanks!谢谢!

Edit:编辑:
I've tried using ffmpeg too, but concatenation removes the audio since concat protocol doesn't support.mp4 extension.我也尝试过使用 ffmpeg,但是由于 concat 协议不支持.mp4 扩展名,因此连接会删除音频 In that case.在这种情况下。 Even if I convert all the files to.ts extension and try to concatenate them , WindowsError: [Error 206] The filename or extension is too long pops up because of too many files separated by |.即使我将所有文件转换为 .ts 扩展名并尝试将它们连接起来WindowsError: [Error 206] The filename or extension is too long弹出,因为用 | 分隔的文件太多。 I did the following changes after converting all the files to.ts format:将所有文件转换为 .ts 格式后,我做了以下更改:

import os
import ffmpeg
os.chdir('D:/videos')
list1 = os.listdir()
list2 = [i for i in list1 if i[-3:] == '.ts']
list2[0] = ffmpeg.input(list2[0])
for i in range(1, len(list2)):
    list2[i] = ffmpeg.concat(list2[i-1], ffmpeg.input(list2[i]))
    print('\r' + str(i) + '/' + str(len(list2)), end='')
ffmpeg.output(list2[-1], 'D:\Merged.mp4')
ffmpeg.run(list2[-1])

But now I'm getting RecursionError: maximum recursion depth exceeded while calling a Python object .但现在我得到RecursionError: maximum recursion depth exceeded while calling a Python object

Can you try deleting explicitly and garbage collection in between like this.您可以尝试像这样显式删除并在两者之间进行垃圾收集。

import os
import gc
from moviepy.editor import *
os.chdir('D:/videos')
list1, list2 = os.listdir(), []

for i in list1: #filtering
    if i[-6:] != '-l.mp4' and i[-7:] != 'ALT.mp4':
        list2.append(i)
print('Getting Video Info:')

final = VideoFileClip(list2[0])


for i in range(1,len(list2)):
    curclip = VideoFileClip(list2[i])
    final = concatenate_videoclips([final, curclip])
    print('\r' + str(i+1) + '/' + str(len(list2)), end='')
    curclip.close()
    del curclip
    gc.collect()

os.chdir('D:')
final.write_videofile('Merged.mp4')

If above doesn't work, try saving final video at a certain frequency eg 8. Saving and reading as raw file will release extra memory accumulating inside final object如果上述方法不起作用,请尝试以特定频率保存最终视频,例如 8。保存和读取为原始文件将释放额外的 memory 累积在最终 object 中

for i in range(1,len(list2)):
    if (i % 8) == 7:
        final.write_videofile('D:/Merged.mp4')
        final = VideoFileClip('D:/Merged.mp4')
    curclip = VideoFileClip(list2[i])
    final = concatenate_videoclips([final, curclip])
    print('\r' + str(i+1) + '/' + str(len(list2)), end='')
    curclip.close()
    del curclip
    gc.collect()

os.chdir('D:')
final.write_videofile('Merged.mp4')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM