furas

Python - moviepy - animated text

Jun 13th, 2018
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.38 KB | None | 0 0
  1. from moviepy.editor import *
  2.  
  3. # --- functions ---
  4.  
  5. def move(x1, y1, x2, y2, time):
  6.     step_x = (x2 - x1) / time
  7.     step_y = (y2 - y1) / time
  8.     return lambda t: (x1 + t*step_x, y1 + t*step_y)
  9.    
  10. # --- main ---
  11.  
  12. video_size = (600,600)
  13.  
  14. # ---
  15.  
  16. text_clip_1 = TextClip('Hello', color='red', font='Amiri-Bold', kerning=5, fontsize=60)
  17.  
  18. txt_width = text_clip_1.size[0]
  19. txt_height = text_clip_1.size[1]
  20.  
  21. y = (600-txt_height)//2
  22. right_x = 600-txt_width
  23. time = 1.5
  24.  
  25. move_right = text_clip_1.set_position(move(0, y, right_x, y, time), 0).set_duration(time)
  26. move_left  = text_clip_1.set_position(move(right_x, y, 0, y, time), 0).set_duration(time).set_start(time)
  27.  
  28. # ---
  29.  
  30. text_clip_2 = TextClip('World', color='red', font='Amiri-Bold', kerning=5, fontsize=60)
  31.  
  32. txt_width = text_clip_2.size[0]
  33. txt_height = text_clip_2.size[1]
  34.  
  35. x = (600-txt_width)//2
  36. bottom_y = 600-txt_height
  37. time = 1.5
  38.  
  39. move_down = text_clip_2.set_position(move(x, 0, x, bottom_y, time), 0).set_duration(time)
  40. move_up   = text_clip_2.set_position(move(x, bottom_y, x, 0, time), 0).set_duration(time).set_start(time)
  41.  
  42. # ---
  43.  
  44. output_clip = CompositeVideoClip([
  45.     move_right, move_left,
  46.     move_down, move_up
  47. ], video_size)
  48.  
  49. output_clip.fps = 24
  50. output_clip.write_videofile('animation.avi', fps=24, codec='mpeg4')#, ffmpeg_params=['-s', '600x600'])
  51.  
  52. # ffmpeg -i animation.avi -s 300x300 animation.gif
Add Comment
Please, Sign In to add comment