moviepy==1.0.2
抄的别人的
import moviepy.editor as mpe
import numpy as np
rotMatrix = lambda a: np.array( [[np.cos(a),np.sin(a)], [-np.sin(a),np.cos(a)]] )
def vortex(screenpos, i, nletters):
d = lambda t: 1.0 / (0.3 + t ** 8) # damping
a = i * np.pi / nletters # angle of the movement
v = rotMatrix(a).dot([-1, 0])
if i % 2: v[1] = -v[1]
return lambda t: screenpos + 400 * d(t) * rotMatrix(0.5 * d(t) * a).dot(v)
def cascade(screenpos, i, nletters):
v = np.array([0, -1])
d = lambda t: 1 if t < 0 else abs(np.sinc(t) / (1 + t ** 4))
return lambda t: screenpos + v * 400 * d(t - 0.15 * i)
def arrive(screenpos, i, nletters):
v = np.array([-1, 0])
d = lambda t: max(0, 3 - 3 * t)
return lambda t: screenpos - 400 * v * d(t - 0.2 * i)
# 消失
def vortexout(screenpos, i, nletters):
d = lambda t: max(0, t) # damping
a = i * np.pi / nletters # angle of the movement
v = rotMatrix(a).dot([-1, 0])
if i % 2: v[1] = -v[1]
return lambda t: screenpos + 400 * d(t - 0.1 * i) * rotMatrix(-0.2 * d(t) * a).dot(v)
def txtanimation(screensize, lettersize, text, func, offsetx, offsety, fontsize=70, color='white'):
screenw = screensize[0] # 屏幕宽度
screenh = screensize[1] # 屏幕高度
letterw = lettersize[0] # 单个字符宽度
letterh = lettersize[1] # 单个字符高度
letternum = len(text) # 字符数量
# 字符的起始位置计算
letteronex = (screenw - letterw * letternum) / 2 + offsetx # 水平偏移
letteroney = (screenh - letterh) / 2 + offsety # 垂直偏移
tclips = [] # 剪辑列表
for i in range(letternum):
tclip = mpe.TextClip(
text[i],
color=color, # 设置字体颜色
font='KaiTi',
kerning=5,
fontsize=fontsize, # 设置字体大小
size=lettersize
) # 创建单个字符文本剪辑
# 设置字符的初始位置
tclip = tclip.set_pos((letteronex + i * letterw, letteroney))
# 调用特效函数来调整字符的位置
tclip = tclip.set_pos(func(tclip.pos(0), i, letternum))
# 添加到剪辑列表
tclips.append(tclip)
return tclips
screensize=(640,480)
lettersize=(80,80)
text=['s','啊','及','s','a']
clip1 = mpe.CompositeVideoClip(
txtanimation(screensize, lettersize, text, cascade, 50, 100, fontsize=100, color='red'),
size=screensize
).subclip(0, 10)
clip1 = clip1.fadeout(3)
# clip2 = mpe.CompositeVideoClip(
# txtanimation(screensize, lettersize, text, vortexout, 50, 100, fontsize=100, color='blue'),
# size=screensize
# ).subclip(0, 5)
iclip=mpe.ImageClip('1128.jpg').set_duration(10)
clip=mpe.CompositeVideoClip([iclip,clip1])
clip.write_videofile('letter.mp4', codec="libx264", fps=24)
标签:字符,moviepy,screenpos,lettersize,动态效果,screensize,np,tclip,lambda
From: https://www.cnblogs.com/msmsga/p/18591121