如何连接在moviepy视频? [英] How to concatenate videos in moviepy?

查看:1031
本文介绍了如何连接在moviepy视频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用moviepy生成带有文字的视频。首先,我要显示一个消息,然后另一个。在我来说,我想显示狗一秒钟,比猫猫。对于我用下面的code:

I am trying to use moviepy to generate video with texts. 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:

从moviepy.editor进口*

from 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)

这是我得到的结果是:

有谁知道为什么我有猫的问题?

Does anybody know why do I have problems with cats?

推荐答案

要被写入到文件中,所有的帧都必须具有相同的尺寸。在这里,你与狗帧是与CatCat,它败坏了视频帧较小。第一种方案是使用组合,在concatenate_videoclips,这将给予同样大小的所有剪辑的方法:

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. A first solution is to use the method "compose" in concatenate_videoclips, this will give the same size to all clips:

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

这篇关于如何连接在moviepy视频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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