In this article we will see how we can insert text to a video clip in 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. In order to do this we need to have ImageMagick installed else it will not work. Adding text to a video is similar to creating composite video.
In order to do so we have to do the following :
1. Import the moviepy module
2. Load the video file
3. Create a text clip and set its position and duration
4. Create a composite video clip using video file and the text clip.
5. Show the final video.
Below is the implementation
# Import everything needed to edit video clips from moviepy.editor import * # loading video dsa gfg intro video clip = VideoFileClip( "dsa_geek.mp4" ) # clipping of the video # getting video for only starting 10 seconds clip = clip.subclip( 0 , 10 ) # Reduce the audio volume (volume x 0.8) clip = clip.volumex( 0.8 ) # Generate a text clip txt_clip = TextClip( "Lazyroar" , fontsize = 75 , color = 'black' ) # setting position of text in the center and duration will be 10 seconds txt_clip = txt_clip.set_pos( 'center' ).set_duration( 10 ) # Overlay the text clip on the first video clip video = CompositeVideoClip([clip, txt_clip]) # showing video video.ipython_display(width = 280 ) |
Moviepy - Building video __temp__.mp4. Moviepy - Writing video __temp__.mp4 Moviepy - Done ! Moviepy - video ready __temp__.mp4
Another example
# Import everything needed to edit video clips from moviepy.editor import * # loading video file clip clip = VideoFileClip( "Lazyroar.mp4" ) # clipping of the video # getting video for only starting 10 seconds clip = clip.subclip( 0 , 5 ) # Reduce the audio volume (volume x 0.5) clip = clip.volumex( 0.5 ) # Generate a text clip txt_clip = TextClip( "GfG-MoviePy" , fontsize = 75 , color = 'green' ) # setting position of text in the center and duration will be 5 seconds txt_clip = txt_clip.set_pos( 'bottom' ).set_duration( 5 ) # Overlay the text clip on the first video clip video = CompositeVideoClip([clip, txt_clip]) # showing video video.ipython_display(width = 280 ) |
Moviepy - Building video __temp__.mp4. Moviepy - Writing video __temp__.mp4 Moviepy - Done ! Moviepy - video ready __temp__.mp4