def add_text_to_movie(movie_fol, movie_name, out_movie_name, subs, fontsize=50, txt_color='red', font='Xolonium-Bold'):
# Should install ImageMagick
# For centos6: https://www.vultr.com/docs/install-imagemagick-on-centos-6
from moviepy import editor
def annotate(clip, txt, txt_color=txt_color, fontsize=fontsize, font=font):
""" Writes a text at the bottom of the clip. """
txtclip = editor.TextClip(txt, fontsize=fontsize, font=font, color=txt_color)
# txtclip = txtclip.on_color((clip.w, txtclip.h + 6), color=(0, 0, 255), pos=(6, 'center'))
cvc = editor.CompositeVideoClip([clip, txtclip.set_pos(('center', 'bottom'))])
return cvc.set_duration(clip.duration)
video = editor.VideoFileClip(op.join(movie_fol, movie_name))
annotated_clips = [annotate(video.subclip(from_t, to_t), txt) for (from_t, to_t), txt in subs]
final_clip = editor.concatenate_videoclips(annotated_clips)
final_clip.write_videofile(op.join(movie_fol, out_movie_name))
python类CompositeVideoClip()的实例源码
def convert(data, outfile='composition.mp4'):
'''Converts popcorn track list into an mp4'''
clips = []
# generate background color clip
# convert hex color from popcorn to an RGB tuple
bg_color = data.get('backgroundColor', '#000000').lstrip('#')
bg_color = tuple(int(bg_color[i:i+2], 16) for i in (0, 2, 4))
total_duration = float(data['duration'])
background_clip = mpy.ColorClip(size=(WIDTH, HEIGHT), col=bg_color).set_duration(total_duration)
clips.append(background_clip)
# generate moviepy clips from popcorn options
tracks = sorted(data['tracks'], key=lambda k: k['order'], reverse=True)
for track in tracks:
for event in track['trackEvents']:
clip = create_clip(event['type'], event['popcornOptions'])
clips.append(clip)
# ignore track events that create_clip can't handle
clips = [c for c in clips if c is not None]
# composite the video
video = mpy.CompositeVideoClip(clips, size=(1280, 720))
tmpoutfile = outfile + '.temp.mp4'
video.write_videofile(
tmpoutfile,
fps=24,
codec='libx264',
audio_codec='aac',
temp_audiofile='temp-audio.m4a',
remove_temp=True
)
shutil.move(tmpoutfile, outfile)
return outfile
def fade_freeze(im_freeze, fade, tbc_overlay, tbc_duration, fadein_duration):
fade_fx = mp.CompositeVideoClip([fade, tbc_overlay]).add_mask().set_duration(tbc_duration).crossfadein(fadein_duration)
faded_clip = mp.CompositeVideoClip([im_freeze, fade_fx])
return faded_clip
def label_clip(video_path, label, start_second, end_second):
clip = VideoFileClip(video_path)
text_clip = TextClip(label, fontsize=40, color='white', bg_color='red')
text_clip = text_clip.set_pos(('center', 'bottom'))
text_clip = text_clip.set_start(start_second).set_duration(end_second -
start_second)
return CompositeVideoClip([clip, text_clip])