我正在尝试使用moviepy创建一个视频幻灯片,并在幻灯片之间进行滑动过渡。如果它们只是ImageClip,我可以使转换工作正常,但是当我使用CompositeVideoClip向图像添加文本时,它会停止工作。如果我只有一个CompositeVideoClip和其余的ImageClip,它可以正常工作,但是一旦我有多个CompositeVideoClips,它就会开始破坏。
我不确定这是否是moviepy的错误,或者我设置的方式。
这是我的代码:
from moviepy.editor import *
H = 720
W = 1280
SIZE = (W, H)
HX = H + H * .10 # increase size 10%
WX = W + W * .10
bold_font = 'Liberation-Sans-Bold'
plain_font = 'Liberation-Sans'
def slide_out(clip, duration, height, counter):
def calc(t, counter, duration, h):
ts = t - (counter * duration)
val = min(-45, h*(duration-ts))
return ('center', val)
return clip.set_pos(lambda t: calc(t, counter, duration, height))
def add_transition(clip_size, counter, clip):
# reverse the count to get slide number.
counter = clip_size - 1 - counter
return slide_out(clip.resize(height=HX, width=WX), 3, HX, counter)
img_1 = ImageClip("/pics/1.jpg").set_duration(4).set_start(0).resize(height=H, width=W) # 3-8
txt_1 = TextClip("title 1", font=bold_font, color='white', fontsize=64, interline=9).set_duration(2).set_start(1).set_pos(('right', 360)).crossfadein(.3)
stxt_1 = TextClip("sub title 1", font=plain_font, color='white', fontsize=80, interline=9).set_duration(1.5).set_start(1.5).set_pos(('right', 440)).crossfadein(.3)
img_2 = ImageClip("/pics/2.jpg").set_duration(4).set_start(3).resize(height=H, width=W) # 3-8
txt_2 = TextClip("title 2", font=bold_font, color='white', fontsize=64, interline=9).set_duration(3).set_start(3.5).set_pos(('right', 360)).crossfadein(.3)
stxt_2 = TextClip("sub title 2", font=plain_font, color='white', fontsize=80, interline=9).set_duration(2.5).set_start(3.5).set_pos(('right', 440)).crossfadein(.3)
# slides images with text on top.
slide_1 = CompositeVideoClip([img_1, txt_1, stxt_1]) #.set_duration(4)
slide_2 = CompositeVideoClip([img_2, txt_2, stxt_2]) #.set_duration(4)
clips = [slide_2, slide_1] # reverse because we want the first slides on top.
slides = [add_transition(len(clips), x, clip) for x, clip in enumerate(clips)]
final_clip = CompositeVideoClip(slides, size=SIZE).set_duration(8)
final_clip.write_videofile("/pics/vids/video.mp4", fps=24, audio_codec="aac")
我尝试了一些不同的事情,看看能不知道发生了什么。你能给予的任何帮助都会很棒。
如果slide_1设置了持续时间且slide_2和final_clip没有设置持续时间。完成的视频持续时间= 4,总持续时间为4.它将显示完整的第一张幻灯片,仅显示第二张幻灯片的第一秒。
slide_1 = CompositeVideoClip([img_1, txt_1, stxt_1]).set_duration(4)
slide_2 = CompositeVideoClip([img_2, txt_2, stxt_2])
final_clip = CompositeVideoClip(slides, size=SIZE)
如果slide_1和slide_2没有设置持续时间,但是final_clip的持续时间为8,则为8秒,但第二张图片仅显示1秒(t = 3到t = 4),然后消失并且留下文字。
slide_1 = CompositeVideoClip([img_1, txt_1, stxt_1])
slide_2 = CompositeVideoClip([img_2, txt_2, stxt_2])
final_clip = CompositeVideoClip(slides, size=SIZE).set_duration(8)
如果所有三个都设置了持续时间。 slide_1工作正常,但幻灯片2只显示1秒钟,然后在剩下的时间内变黑。
slide_1 = CompositeVideoClip([img_1, txt_1, stxt_1]).set_duration(4)
slide_2 = CompositeVideoClip([img_2, txt_2, stxt_2]).set_duration(4)
final_clip = CompositeVideoClip(slides, size=SIZE).set_duration(8)
如果没有设置持续时间。与2相同。
slide_1 = CompositeVideoClip([img_1, txt_1, stxt_1])
slide_2 = CompositeVideoClip([img_2, txt_2, stxt_2])
final_clip = CompositeVideoClip(slides, size=SIZE)
如果幻灯片2有持续时间,但slide_1和final_clip没有。与1相同。
slide_1 = CompositeVideoClip([img_1, txt_1, stxt_1])
slide_2 = CompositeVideoClip([img_2, txt_2, stxt_2]).set_duration(4)
final_clip = CompositeVideoClip(slides, size=SIZE)
答案 0 :(得分:0)
请测试以下代码,然后您将找到CompositeVideoClip
from moviepy.editor import *
H = 720
W = 1280
SIZE = (W, H)
HX = H + H * .10 # increase size 10%
WX = W + W * .10
bold_font = 'Liberation-Sans-Bold'
plain_font = 'Liberation-Sans'
img_1 = ImageClip("bbb.jpeg").set_duration(4).set_start(0).resize(height=H, width=W) # 3-8
txt_1 = TextClip("title 1", font=bold_font, color='white', fontsize=64, interline=9).set_duration(2).set_start(1).set_pos(('right', 360)).crossfadein(.3)
stxt_1 = TextClip("sub title 1", font=plain_font, color='white', fontsize=80, interline=9).set_duration(1.5).set_start(1.5).set_pos(('right', 440)).crossfadein(.3)
img_2 = ImageClip("aaa.jpg").set_duration(8).set_start(4).resize(height=H, width=W) # 3-8
# look the img_2's set_duration and set_start,the same as txt_2 and stxt_2
txt_2 = TextClip("title 2", font=bold_font, color='white', fontsize=64, interline=9).set_duration(7).set_start(4.5).set_pos(('right', 360)).crossfadein(.3)
stxt_2 = TextClip("sub title 2", font=plain_font, color='white', fontsize=80, interline=9).set_duration(6).set_start(5.5).set_pos(('right', 440)).crossfadein(.3)
slide_1 = CompositeVideoClip([img_1, txt_1, stxt_1]).set_duration(4)
slide_2 = CompositeVideoClip([img_2, txt_2, stxt_2]).set_duration(8)
clips = [slide_2, slide_1]
final_clip = CompositeVideoClip(clips, size=SIZE).set_duration(8)
final_clip.write_videofile("video.mp4", fps=12, audio_codec="aac")
所有奇怪的行为都是由videopy.video.compositeVideoClip.py
# compute duration
ends = [c.end for c in self.clips]
if not any([(e is None) for e in ends]):
self.duration = max(ends)
self.end = max(ends)