#pip install MoviePy from moviepy.editor import * video = VideoFileClip(\'F://a.mp4\') audio = video.audio audio.write_audiofile(\'F://dsa.mp3\')
# 从本地载入视频myHolidays.mp4并截取00:00:50 - 00:00:60部分
clip = VideoFileClip("D://a.mp4").subclip(50, 60)
# 调低音频音量 (volume x 0.8)
clip = clip.volumex(0.8)
# 把最后生成的视频导出到文件内
clip.write_videofile("D://myHolidays_edited.webm")
合并音频和视频以及插入文字和图片
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(\'aaa\', 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) # background_clip.write_videofile(output_video) if __name__ == \'__main__\': print(\'start!!!\') render(input_video="./模板.mp4", output_video="new_video.mp4")
这里面会遇到问题:
你得下载安装ImageMagick
安装的时候,安装过程注意勾选Install development headers and libraries for C and C++ 。
修改moviepy包(\Lib\site-packages\moviepy\)中的config_defaults.py文件,修改为
FFMPEG_BINARY = os.getenv(\'FFMPEG_BINARY\', \'ffmpeg-imageio\')
IMAGEMAGICK_BINARY = os.getenv(\'IMAGEMAGICK_BINARY\', \'auto-detect\')
IMAGEMAGICK_BINARY = r"G:\ImageMagick-7.0.9-Q16\magick.exe"
安装包我已经存在百度云了 ImageMagick-7.0.9-22-Q16-x64-dll.exe