[英]How to concatenate videos in moviepy?
I am trying to use moviepy to generate video with texts. 我正在尝试使用moviepy生成带有文本的视频。 First, I want to show one messages and then another one.
首先,我想显示一条消息,然后显示另一条消息。 In my case I want to show "Dog" for one second and than "Cat Cat".
在我的情况下,我希望显示“狗”一秒钟而不是“猫猫”。 For that I use the following code:
为此,我使用以下代码:
from moviepy.editor import * 来自moviepy.editor import *
def my_func(messeges):
clips = {}
count = 0
for messege in messeges:
count += 1
clips[count] = TextClip(messege, fontsize=270, color='green')
clips[count] = clips[count].set_pos('center').set_duration(1)
clips[count].write_videofile(str(count) + '.avi', fps=24, codec='mpeg4')
videos = [clips[i+1] for i in range(count)]
video = concatenate(videos)
video.write_videofile('test.avi', fps=24, codec='mpeg4')
video = VideoFileClip('test.avi')
video.write_gif('test.gif', fps=24)
if __name__ == '__main__':
ms = []
ms += ['Dog']
ms += ['Cat Cat']
my_func(ms)
This is the result that I get: 这是我得到的结果:
Does anybody know why do I have problems with cats? 有谁知道为什么我有猫的问题?
To be written to a file, all the frames must have the same size. 要写入文件,所有帧必须具有相同的大小。 Here you frames with Dog are smaller that the frames with CatCat, which spoils the video.
在这里,使用Dog的框架比使用CatCat的框架小,这会破坏视频。 A first solution is to use the method "compose" in concatenate_videoclips, this will give the same size to all clips:
第一种解决方案是在concatenate_videoclips中使用“compose”方法,这将为所有剪辑提供相同的大小:
import moviepy.editor as mp
messages = ["Dog", "Cat", "Duck", "Wolf"]
clips = [ mp.TextClip(txt, fontsize=170, color='green').set_duration(1)
for txt in messages ]
concat_clip = mp.concatenate_videoclips(clips, method="compose")
concat_clip.write_videofile("texts.mp4")
A second solution is to give the same size (width, height) to all of your text clips: 第二种解决方案是为所有文本剪辑提供相同的大小(宽度,高度):
import moviepy.editor as mp
messages = ["Dog", "Cat", "Duck", "Wolf"]
clips = [ mp.TextClip(txt, fontsize=170, color='green', size=(500,300))
.set_duration(1)
for txt in messages]
concat_clip = mp.concatenate_videoclips(clips)
concat_clip.write_videofile("texts.mp4")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.