Thursday, March 3, 2016

moviepy, library to editor video file

moviepy can help you with automatize tedious tasks, like title insertions tracking objects, cuting scenes, making end credits, subtitles. and it also can create animations from images generated by another python library (Matplotlib, Mayavi, Gizeh, scikit-images

1- example to add text to video

from moviepy.editor import *

video = VideoFileClip("inputvideo.mp4").subclip(50,60)
# Make the text. Many more options are available.
txt_clip = ( TextClip("Starting text",fontsize=70,color='white')
             .set_position('center')
             .set_duration(10)
             .margin(top=15, opacity=0) )

txt_clip2 = ( TextClip("Proceeding text",fontsize=70,color='white')
             .set_position('bottom')
             .set_duration(10)
             .margin(top=15, opacity=0)
         .set_start(10) )

result = CompositeVideoClip([video, txt_clip, txt_clip2])
result.write_videofile("outputvideo.webm",fps=25)


2- example to add random box in random position to video

from moviepy.editor import *
import random
 

video = VideoFileClip("inputvideo.mp4").subclip(50,60)
def randomBox(startTime):
    randW = random.randint(50,200)
    randH = random.randint(50,200)
    posW = random.randint(1,700)
    posH = random.randint(1,700)
    clip =  (ColorClip(size=(randW,randH), ismask=False)
             .set_duration(1)
             .set_pos((posW, posH))
             .set_start(startTime)
             .set_opacity(0.65))
    return clip


clipadd = []
clipadd.append(video)

   
for x in range(243):
    clip = randomBox(x + 1)
    clipadd.append(clip)

result = CompositeVideoClip(clipadd)
result.write_videofile("outputvideo.webm",fps=25) 

No comments:

Post a Comment