1.关于moviepy和原理
moviepy事github上面的一个开源项目,地址是:GitHub - Zulko/moviepy: Video editing with Python
官方文档地址:
User Guide — MoviePy 1.0.2 documentation
中文版文档可参考:
MoviePy中文手册 — moviepy-cn 文档
底层是调用其他开源的视频、图片等开源软件,操作视频使用的ffmpeg,地址为:
https://github.com/FFmpeg/FFmpeg
操作图片使用ImageMagick,地址为:
https://github.com/ImageMagick/ImageMagick
2.windows上安装
a.下载和安装FFmpeg,地址为:Download FFmpeg ,安装完成后设置环境变量
b.下载和安装ImageMagick,地址为:ImageMagick – Download
c.安装moviepy,使用命令 pip install moviepy
d.修改 moviepy/config_defaults.py 中的IMAGEMAGICK_BINARY的值为本地magic.exe的绝对路径
3.实操和代码示例
from moviepy.editor import *inPath = r'E:\songhq\video\car_concatenate.mp4'
outPath = r'E:\songhq\video\car_concatenate_word.mp4'clip = VideoFileClip(inPath)
print(TextClip.list('font'))
# 添加文本
text_clip = TextClip("端午节六折大甩卖!", fontsize=70, color="red", font='simfang.ttf')text_clip = text_clip.set_position((800, 50)).set_duration(3)
final_clip = CompositeVideoClip([clip, text_clip])final_clip.write_videofile(outPath)if __name__ == '__main__':# processors.reverse_video(r'E:\songhq\video\login.mp4',r'E:\songhq\video\aa.mp4')# clip = VideoFileClip(r'E:\songhq\video\login.mp4').rotate(180)# clip.write_videofile(r'E:\songhq\video\login_180.mp4')# font = TextClip.list('font')# for a in font:# print("字体" + a)inputPath = r'E:\songhq\video\login.mp4'inputPath2 = r'E:\songhq\video\video.mp4'outPath = r'E:\songhq\video\login_hebing.mp4'musicPath = r'E:\songhq\video\追光者.mp3'start_time = 0end_time = 7video = VideoFileClip(inputPath)video2 = VideoFileClip(inputPath2)# 对视频进行剪辑clipped_video = video.subclip(start_time, end_time)video2 = video2.subclip(0, 3)final_clip = concatenate_videoclips([clipped_video, video2])# 添加文字# txtclip = TextClip(inf, font='仿宋_GB2312.ttf', fontsize=18, color='blue', bg_color='white', transparent=True).set_duration(30).resize((clip.size[0], clip.size[1] * 2)).set_fps(clip.fps)text = TextClip("MyHolidays", fontsize=90, color='white', font='simfang.ttf')# 插入图片text = text.set_position('center').set_duration(5)# 添加音乐 .subclip(0, 7) 可以截片段audio = AudioFileClip(musicPath).subclip(40, 50)# 将文字和音乐添加到视频中final_video = CompositeVideoClip([final_clip, text])final_video = final_video.set_audio(audio)final_video.write_videofile(outPath)