本文记录利用moviepy对视频文件进行编辑
- 项目主页:https://github.com/Zulko/moviepy
0 安装遇到的问题
利用pip安装时(pip install moviepy
),出现下列问题
ERROR: Cannot uninstall 'imageio'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.
解决方案:
pip install --ignore-installed imageio
pip install moviepy
1 一个入门例子(jupyter notebook中运行)
import moviepy.editor as med
clip = med.VideoFileClip("14-20-44_Trim.mp4").subclip(0,20) ## 选取视频的0-20s
clip.ipython_display(width=280)
效果如下:

2 给视频添加字幕
txt = med.TextClip("hello ",color='white', font=r"C:\Windows\Fonts\STXIHEI.TTF")
2.1 遇到的问题
FileNotFoundError: [WinError 2] 系统找不到指定的文件。
原因是未安装ImageMagick(下载地址:https://imagemagick.org/script/download.php#windows) 安装完成后需修改moviepy的配置


config_defaults.py
修改如下
- 如果不采用上述步骤可以设置
IMAGEMAGICK_BINARY
的环境变量。
以上该问题解决。
2.2 添加字幕
txt = med.TextClip("大家好 ",color='red',bg_color="transparent",
fontsize=60,
font=r"C:\Windows\Fonts\STXIHEI.TTF").set_duration(10).set_start(0).set_position(('bottom'))
new_vedio = med.CompositeVideoClip([clip,txt])
- set_duration设置持续时间
- set_start字幕开始时间
- set_position字幕位置 topleftrightbottom
最后保存视频:
new_vedio.write_videofile("test.mp4")
阅读原文