把玩了下moviepy蛮有意思的, 记录下来方便以后使用
(ps: 其实直接看官网就可以无视该教程了: https://pypi.python.org/pypi/moviepy)
1. 环境
(1) pip install moviepy
(2) 安装 ImageMagick, http://www.imagemagick.org/script/download.php
如果是windows玩家直接在这里下个最新的exe安装, 安装包在此, 安装完后设置环境变量: IMAGEMAGICK_BINARY 的值为convert.exe的路径;
(3) python工程根目录建个"ffmpeg"文件夹, 然后把"ffmpeg.win32.exe"丢进去, 记住名字必须为"ffmpeg.win32.exe";
2. 实例代码
from moviepy.editor import *
from moviepy.audio.fx import all
# 字体名字不能含有中文
FONT_URL = './font/heimi.TTF'
def render(input_video, output_video="new_video.mp4"):
# 剪个10s的720x1280px的视频
background_clip = VideoFileClip(input_video, target_resolution=(720, 1280)).subclip(0, 10)
# 音乐只要前10s
audio_clip = AudioFileClip('yuna.mp3').subclip(0, 10)
background_clip = background_clip.set_audio(audio_clip)
# 左下角加文字, 持续10s
text_clip1 = TextClip('我是左下角', fontsize=30, color='white', font=FONT_URL)
text_clip1 = text_clip1.set_position(('left', 'bottom'))
text_clip1 = text_clip1.set_duration(10)
# 右下角加文字, 持续3s
text_clip2 = TextClip('我是右下角', fontsize=30, color='white', font=FONT_URL)
text_clip2 = text_clip2.subclip(0, 3).set_position(('right', 'bottom'))
image_clip = ImageClip('shuoGG.png')
# 图片放中间, 从第2s开始播持续6s
image_clip = image_clip.set_duration(6).set_position('center').set_start(2)
video = CompositeVideoClip([background_clip, text_clip1, text_clip2, image_clip])
# 调节音量
video = all.volumex(video, 0.8)
video.write_videofile(output_video)
if __name__ == '__main__':
print('start!!!')
render(input_video="./模板.mp4", output_video="new_video.mp4")
3. demo工程地址
https://gitee.com/shuogg/shuoGG-demos/tree/master/PyVideoDemo