MoviePy - 带有移动字母的文本
在本文中,我们将了解如何移动文本剪辑 MoviePy 的字母。 MoviePy 是一个用于视频编辑的Python模块,可用于视频和 GIF 的基本操作。视频是由帧组成的,帧的组合创建一个视频,每一帧都是一个单独的图像。 VideoClip 是 MoviePy 中所有其他视频剪辑的基类。带有移动字母的文本,就像字母在传播和重新排列,创造出一种移动的效果。
In order to do so we have to do the following :
1. Import the numpy and moviepy module
2. Create a text clip and set its various properties
3. From text clip create a composite video clip
4. Create methods for moving letters effect
5. Create clips using the methods one by one
6. Combine the clips to obtain final video clip
7. Set fps to the final clip
8. Show the final clip
示例:下面是实现
Python3
# importing Numpy
import numpy as np
# importing moviepy module
from moviepy.editor import * from moviepy.video.tools.segmenting import findObjects
# screen size
screensize = (720, 460)
# creating a text clip of color green, font is Arial and size is 80
txtClip = TextClip('GeeksforGeeks', color = 'lightgreen', font = "Arial",
kerning = 5, fontsize = 80)
# creating a composite video of given size
cvc = CompositeVideoClip( [txtClip.set_pos('center')],
size = screensize)
# helper function
rotMatrix = lambda a: np.array( [[np.cos(a), np.sin(a)],
[-np.sin(a), np.cos(a)]] )
# creating a effect 1 method
def effect1(screenpos, i, nletters):
# damping
d = lambda t : 1.0/(0.3 + t**8)
# angle of the movement
a = i * np.pi / nletters
# using helper function
v = rotMatrix(a).dot([-1, 0])
if i % 2 : v[1] = -v[1]
# returning the function
return lambda t: screenpos + 400 * d(t)*rotMatrix(0.5 * d(t)*a).dot(v)
# method for effect 2
def effect2(screenpos, i, nletters):
# numpy array
v = np.array([0, -1])
d = lambda t : 1 if t<0 else abs(np.sinc(t)/(1 + t**4))
# returning the function
return lambda t: screenpos + v * 400 * d(t-0.15 * i)
# a list of ImageClips
letters = findObjects(cvc)
# method to move letters
def moveLetters(letters, funcpos):
return [ letter.set_pos(funcpos(letter.screenpos, i, len(letters)))
for i, letter in enumerate(letters)]
# adding clips with specific effect
clips = [ CompositeVideoClip( moveLetters(letters, funcpos),
size = screensize).subclip(0, 5)
for funcpos in [effect1, effect2] ]
# comping all the clips
final_clip = concatenate_videoclips(clips)
# setting fps of the final clip
final_clip.fps = 24
# showing video clip
final_clip.ipython_display()
Python3
# importing Numpy
import numpy as np
# importing moviepy module
from moviepy.editor import * from moviepy.video.tools.segmenting import findObjects
# screen size
screensize = (1080, 720)
# creating a text clip of color red, font is Arial and size is 80
txtClip = TextClip('GeeksforGeeks', color = 'red', font = "Arial",
kerning = 5, fontsize = 80)
# creating a composite video of given size
cvc = CompositeVideoClip( [txtClip.set_pos('center')],
size = screensize)
# helper function
rotMatrix = lambda a: np.array( [[np.cos(a), np.sin(a)],
[-np.sin(a), np.cos(a)]] )
# method for effect 3
def effect3(screenpos, i, nletters):
v = np.array([-1, 0])
d = lambda t : max(0, 3-3 * t)
# returning the function
return lambda t: screenpos-400 * v*d(t-0.2 * i)
# method for effect 4
def effect4(screenpos, i, nletters):
# damping
d = lambda t : max(0, t)
# angle of the movement
a = i * np.pi / nletters
v = rotMatrix(a).dot([-1, 0])
if i % 2 : v[1] = -v[1]
# returning the function
return lambda t: screenpos + 400 * d(t-0.1 * i)*rotMatrix(-0.2 * d(t)*a).dot(v)
# a list of ImageClips
letters = findObjects(cvc)
# method to move letters
def moveLetters(letters, funcpos):
return [ letter.set_pos(funcpos(letter.screenpos, i, len(letters)))
for i, letter in enumerate(letters)]
# adding clips with specific effect
clips = [ CompositeVideoClip( moveLetters(letters, funcpos),
size = screensize).subclip(0, 5)
for funcpos in [effect3, effect4] ]
# comping all the clips
final_clip = concatenate_videoclips(clips)
# setting fps of the final clip
final_clip.fps = 24
# showing video clip
final_clip.ipython_display()
输出 :
Moviepy - Building video __temp__.mp4.
Moviepy - Writing video __temp__.mp4
Moviepy - Done !
Moviepy - video ready __temp__.mp4
另一个例子
Python3
# importing Numpy
import numpy as np
# importing moviepy module
from moviepy.editor import * from moviepy.video.tools.segmenting import findObjects
# screen size
screensize = (1080, 720)
# creating a text clip of color red, font is Arial and size is 80
txtClip = TextClip('GeeksforGeeks', color = 'red', font = "Arial",
kerning = 5, fontsize = 80)
# creating a composite video of given size
cvc = CompositeVideoClip( [txtClip.set_pos('center')],
size = screensize)
# helper function
rotMatrix = lambda a: np.array( [[np.cos(a), np.sin(a)],
[-np.sin(a), np.cos(a)]] )
# method for effect 3
def effect3(screenpos, i, nletters):
v = np.array([-1, 0])
d = lambda t : max(0, 3-3 * t)
# returning the function
return lambda t: screenpos-400 * v*d(t-0.2 * i)
# method for effect 4
def effect4(screenpos, i, nletters):
# damping
d = lambda t : max(0, t)
# angle of the movement
a = i * np.pi / nletters
v = rotMatrix(a).dot([-1, 0])
if i % 2 : v[1] = -v[1]
# returning the function
return lambda t: screenpos + 400 * d(t-0.1 * i)*rotMatrix(-0.2 * d(t)*a).dot(v)
# a list of ImageClips
letters = findObjects(cvc)
# method to move letters
def moveLetters(letters, funcpos):
return [ letter.set_pos(funcpos(letter.screenpos, i, len(letters)))
for i, letter in enumerate(letters)]
# adding clips with specific effect
clips = [ CompositeVideoClip( moveLetters(letters, funcpos),
size = screensize).subclip(0, 5)
for funcpos in [effect3, effect4] ]
# comping all the clips
final_clip = concatenate_videoclips(clips)
# setting fps of the final clip
final_clip.fps = 24
# showing video clip
final_clip.ipython_display()
输出 :
Moviepy - Building video __temp__.mp4.
Moviepy - Writing video __temp__.mp4
Moviepy - Done !
Moviepy - video ready __temp__.mp4