In this article we will see how we can move the letters of the text clip MoviePy. MoviePy is a Python module for video editing, which can be used for basic operations on videos and GIF’s. Video is formed by the frames, combination of frames creates a video each frame is an individual image. VideoClip is the base class for all the other video clips in MoviePy. Text with moving letters, is like letters are spreading and rearranging themselves, creating a moving effect.
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
Example: Below is the implementation
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( 'Lazyroar' , 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() |
Output :
Moviepy - Building video __temp__.mp4. Moviepy - Writing video __temp__.mp4 Moviepy - Done ! Moviepy - video ready __temp__.mp4
Another example
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( 'Lazyroar' , 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() |
Output :
Moviepy - Building video __temp__.mp4. Moviepy - Writing video __temp__.mp4 Moviepy - Done ! Moviepy - video ready __temp__.mp4