python-Moviepy缩放效果需要调整
作者:互联网
我想将剪辑缩放到某个尺寸,然后停止进一步缩放.换句话说,剪辑在达到一定大小后会停止进一步增加其大小,如果剪辑开始从其原始大小的小得多的地方缩放到更大的版本,则它会更好.我正在使用moviepy模块.通过下面的代码,我可以逐步缩放剪辑,但是很难弄清楚如何将剪辑从小到大,以创建动画效果.您提供的任何反馈意见都将不胜感激.
import os
from moviepy.editor import *
screensize = (640,360)
clip = (ImageClip(img)
.resize(height=screensize[1]*4)
.resize(lambda t : 1+0.02*t)
.set_position(('center', 'center'))
.set_duration(10)
)
我很难弄清楚如何编写函数以使用moviepy的txtclip创建打字效果,这意味着剪辑中的字母将一个接一个地显示,从而创建一个平滑的打字机动画文本. Moviepy提供了一些有用的类,例如findObjects,它们可以从剪辑中检测单个字母:
txtClip = TextClip('Cool effect',color='white', font="Amiri-Bold",
kerning = 5, fontsize=100)
cvc = CompositeVideoClip( [txtClip.set_pos('center')],
size=screensize, transparent=True)
letters = findObjects(cvc) # a list of ImageClips
here is the link: http://zulko.github.io/moviepy/examples/moving_letters.html
找到字母(字母夹)后,我想以一种又一种的方式加入它们,就像打字一样.
文档中已经有一些移动字母的示例,但是可能很有用.谢谢
解决方法:
这是实现您想要的方式.关键是在命名函数中定义大小调整逻辑,而不是使用lambda.
import os
from moviepy.editor import *
def resize_func(t):
if t < 4:
return 1 + 0.2*t # Zoom-in.
elif 4 <= t <= 6:
return 1 + 0.2*4 # Stay.
else: # 6 < t
return 1 + 0.2*(duration-t) # Zoom-out.
duration = 10
screensize = (640,360)
clip_img = (
ImageClip('test.png')
.resize(screensize)
.resize(resize_func)
.set_position(('center', 'center'))
.set_duration(duration)
.set_fps(25)
)
clip = CompositeVideoClip([clip_img], size=screensize)
clip.write_videofile('test.mp4')
编辑
后面的代码回答了问题的第二部分(我不知道最好做成两个单独的问题).
from __future__ import division
from moviepy.editor import *
from moviepy.video.tools.segmenting import findObjects
def clip_typewriter(text, duration_clip, duration_effect):
# `duration_effect` is effectively the time where the last letter appears.
clip_text = TextClip(
text,
color='white',
font='Consolas',
fontsize=80,
kerning=3,
)
letters = findObjects(clip_text, preview=False)
# Select the start time in seconds for each letter found:
n = len(letters)
times_start = [duration_effect*i/(n-1) for i in range(n)]
clips_letters = []
for i, letter in enumerate(letters):
clips_letters.append(letter
.set_position(letter.screenpos)
.set_start(times_start[i])
.set_end(duration_clip)
# Here you can add key sounds using `set_audio`.
)
return CompositeVideoClip(clips_letters, size=clip_text.size)
if __name__ == '__main__':
screensize = (320, 180)
fps = 12
clip_1 = clip_typewriter('hello', 2, 1).set_start(1).set_position('center')
clip_2 = clip_typewriter('world', 2, 1).set_start(4).set_position('center')
clip_final = CompositeVideoClip([clip_1, clip_2], size=screensize)
clip_final.write_gif('test_typewriter.gif', fps=fps)
结果:
标签:animation,video,moviepy,python 来源: https://codeday.me/bug/20191118/2029247.html