首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

MoviePy - 中文文档4-MoviePy实战案例-做一个《星战》风格的片头

  • 23-09-22 20:22
  • 4692
  • 9725
blog.csdn.net

回到目录

做一个《星战》风格的片头

这种透视效果需要比较复杂的转换,但是这样的练习也挺不错的

  1. """
  2. Description of the video:
  3. Mimic of Star Wars' opening title. A text with a (false)
  4. perspective effect goes towards the end of space, on a
  5. background made of stars. Slight fading effect on the text.
  6. """
  7. import numpy as np
  8. from skimage import transform as tf
  9. from moviepy.editor import *
  10. from moviepy.video.tools.drawing import color_gradient
  11. # RESOLUTION
  12. w = 720
  13. h = w*9/16 # 16/9 screen
  14. moviesize = w,h
  15. # THE RAW TEXT
  16. txt = " ".join([
  17. "A long time ago, in a faraway galaxy,",
  18. "there lived a prince and a princess",
  19. "who had never seen the stars, for they",
  20. "lived deep underground.",
  21. "",
  22. "Many years before, the prince's",
  23. "grandfather had ventured out to the",
  24. "surface and had been burnt to ashes by",
  25. "solar winds.",
  26. "",
  27. "One day, as the princess was coding",
  28. "and the prince was shopping online, a",
  29. "meteor landed just a few megameters",
  30. "from the couple's flat."
  31. ])
  32. # Add blanks
  33. txt = 10*" " +txt + 10*" "
  34. # CREATE THE TEXT IMAGE
  35. clip_txt = TextClip(txt,color='white', align='West',fontsize=25,
  36. font='Xolonium-Bold', method='label')
  37. # SCROLL THE TEXT IMAGE BY CROPPING A MOVING AREA
  38. txt_speed = 27
  39. fl = lambda gf,t : gf(t)[int(txt_speed*t):int(txt_speed*t)+h,:]
  40. moving_txt= clip_txt.fl(fl, apply_to=['mask'])
  41. # ADD A VANISHING EFFECT ON THE TEXT WITH A GRADIENT MASK
  42. grad = color_gradient(moving_txt.size,p1=(0,2*h/3),
  43. p2=(0,h/4),col1=0.0,col2=1.0)
  44. gradmask = ImageClip(grad,ismask=True)
  45. fl = lambda pic : np.minimum(pic,gradmask.img)
  46. moving_txt.mask = moving_txt.mask.fl_image(fl)
  47. # WARP THE TEXT INTO A TRAPEZOID (PERSPECTIVE EFFECT)
  48. def trapzWarp(pic,cx,cy,ismask=False):
  49. """ Complicated function (will be latex packaged as a fx) """
  50. Y,X = pic.shape[:2]
  51. src = np.array([[0,0],[X,0],[X,Y],[0,Y]])
  52. dst = np.array([[cx*X,cy*Y],[(1-cx)*X,cy*Y],[X,Y],[0,Y]])
  53. tform = tf.ProjectiveTransform()
  54. tform.estimate(src,dst)
  55. im = tf.warp(pic, tform.inverse, output_shape=(Y,X))
  56. return im if ismask else (im*255).astype('uint8')
  57. fl_im = lambda pic : trapzWarp(pic,0.2,0.3)
  58. fl_mask = lambda pic : trapzWarp(pic,0.2,0.3, ismask=True)
  59. warped_txt= moving_txt.fl_image(fl_im)
  60. warped_txt.mask = warped_txt.mask.fl_image(fl_mask)
  61. # BACKGROUND IMAGE, DARKENED AT 60%
  62. stars = ImageClip('../../videos/stars.jpg')
  63. stars_darkened = stars.fl_image(lambda pic: (0.6*pic).astype('int16'))
  64. # COMPOSE THE MOVIE
  65. final = CompositeVideoClip([
  66. stars_darkened,
  67. warped_txt.set_pos(('center','bottom'))],
  68. size = moviesize)
  69. # WRITE TO A FILE
  70. final.set_duration(8).write_videofile("starworms.avi", fps=5)
  71. # This script is heavy (30s of computations to render 8s of video)
  72. """=====================================================================
  73. CODE FOR THE VIDEO TUTORIAL
  74. We will now code the video tutorial for this video.
  75. When you think about it, it is a code for a video explaining how to
  76. make another video using some code (this is so meta !).
  77. This code uses the variables of the previous code (it should be placed
  78. after that previous code to work).
  79. ====================================================================="""
  80. def annotate(clip,txt,txt_color='white',bg_color=(0,0,255)):
  81. """ Writes a text at the bottom of the clip. """
  82. txtclip = TextClip(txt, fontsize=20, font='Ubuntu-bold',
  83. color=txt_color)
  84. txtclip = txtclip.on_color((clip.w,txtclip.h+6), color=(0,0,255),
  85. pos=(6,'center'))
  86. cvc = CompositeVideoClip([clip , txtclip.set_pos((0,'bottom'))])
  87. return cvc.set_duration(clip.duration)
  88. def resizeCenter(clip):
  89. return clip.resize( height=h).set_pos('center')
  90. def composeCenter(clip):
  91. return CompositeVideoClip([clip.set_pos('center')],size=moviesize)
  92. annotated_clips = [ annotate(clip,text) for clip,text in [
  93. (composeCenter(resizeCenter(stars)).subclip(0,3),
  94. "This is a public domain picture of stars"),
  95. (CompositeVideoClip([stars],moviesize).subclip(0,3),
  96. "We only keep one part."),
  97. (CompositeVideoClip([stars_darkened],moviesize).subclip(0,3),
  98. "We darken it a little."),
  99. (composeCenter(resizeCenter(clip_txt)).subclip(0,3),
  100. "We generate a text image."),
  101. (composeCenter(moving_txt.set_mask(None)).subclip(6,9),
  102. "We scroll the text by cropping a moving region of it."),
  103. (composeCenter(gradmask.to_RGB()).subclip(0,2),
  104. "We add this mask to the clip."),
  105. (composeCenter(moving_txt).subclip(6,9),
  106. "Here is the result"),
  107. (composeCenter(warped_txt).subclip(6,9),
  108. "We now warp this clip in a trapezoid."),
  109. (final.subclip(6,9),
  110. "We finally superimpose with the stars.")
  111. ]]
  112. # Concatenate and write to a file
  113. concatenate_videoclips(annotated_clips).write_videofile('tutorial.avi', fps=5)

QQ群:MoviePy中文 :819718037

回到目录

文章知识点与官方知识档案匹配,可进一步学习相关知识
Python入门技能树首页概览333612 人正在系统学习中
注:本文转载自blog.csdn.net的ucsheep的文章"https://blog.csdn.net/ucsheep/article/details/82786622"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

101
推荐
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2024 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top