Home | 简体中文 | 繁体中文 | 杂文 | 知乎专栏 | 51CTO学院 | CSDN程序员研修院 | Github | OSChina 博客 | 腾讯云社区 | 阿里云栖社区 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏多维度架构

15.2. MoviePy

		
pip install moviepy
		
		

15.2.1. 合成视频

			
clip1 = VideoFileClip("test1.mp4")
clip2 = VideoFileClip("test2.mp4")
clip3 = VideoFileClip("test3.mp4")

video = CompositeVideoClip([clip1,clip2,clip3],size=(1920, 1080))
video.write_videofile("video.mp4")				
			
			
			
from moviepy.editor import VideoFileClip, concatenate_videoclips
# 重新设置视频大小
clip1 = VideoFileClip("chip1.mp4").resize((1920, 1080)) 
# 剪切指定时间片段(单位秒)
clip2 = VideoFileClip("chip2.mp4").subclip(50,60) 
# 剪切持续时间(单位秒)
clip3 = VideoFileClip("chip3.mp4").set_duration(5) 
# 按顺序拼接视频
final_clip = concatenate_videoclips([clip1,clip2,clip3])
final_clip.write_videofile("video.mp4")			
			
			

15.2.2. 提取视频中的音频

			
#coding=utf-8
import moviepy.editor as mov

video = mov.VideoFileClip('test.mp4')
audio = video.audio
audio.write_audiofile('test.wav')		
			
			
			
neo@MacBook-Pro-Neo ~/workspace/python/video % python3 audio.py 
MoviePy - Writing audio in test.wav
MoviePy - Done.           
                                                                                                                                   
neo@MacBook-Pro-Neo ~/workspace/python/video % ls *.wav
test.wav
			
			

15.2.3. 加字幕

			
brew install ImageMagick			
			
			
			
			
			
			

查看可用字体

			
from moviepy.editor import TextClip
print ( TextClip.list("font") )		
			
			

运行结果

			
neo@MacBook-Pro-Neo ~/workspace/python/video % /usr/bin/python3 /Users/neo/workspace/python/video/font.py
['AvantGarde-Book', 'AvantGarde-BookOblique', 'AvantGarde-Demi', 'AvantGarde-DemiOblique', 'Bookman-Demi', 'Bookman-DemiItalic', 'Bookman-Light', 'Bookman-LightItalic', 'Courier', 'Courier-Bold', 'Courier-BoldOblique', 'Courier-Oblique', 'fixed', 'Helvetica', 'Helvetica-Bold', 'Helvetica-BoldOblique', 'Helvetica-Narrow', 'Helvetica-Narrow-Bold', 'Helvetica-Narrow-BoldOblique', 'Helvetica-Narrow-Oblique', 'Helvetica-Oblique', 'NewCenturySchlbk-Bold', 'NewCenturySchlbk-BoldItalic', 'NewCenturySchlbk-Italic', 'NewCenturySchlbk-Roman', 'Palatino-Bold', 'Palatino-BoldItalic', 'Palatino-Italic', 'Palatino-Roman', 'Symbol', 'Times-Bold', 'Times-BoldItalic', 'Times-Italic', 'Times-Roman']			
			
			

指定字体路径

			
from moviepy.editor import TextClip
font_path = './font/heimi.ttf'
txtClip = TextClip('《Netkiller Python 手札》',color='white', font=font_path,kerning = 5, fontsize=100)			
			
			

苹果电脑的字体安装在 /System/Library/Fonts/ 目录中。

			
from moviepy.editor import *

video = VideoFileClip("test.mp4").subclip(50,60)

# Make the text. Many more options are available.
txt_clip = ( TextClip("《Netkiller Python 手札》",font="/System/Library/Fonts/PingFang.ttc", fontsize=70,color='white')
             .set_position('center')
             .set_duration(10) )

result = CompositeVideoClip([video, txt_clip]) # Overlay text on video
result.write_videofile("test.webm",fps=25) # Many options...
			
			
			

15.2.4. 音量大小调节

			
VolumeMultiple = 0.8
clip = clip.volumex(VolumeMultiple)			
			
			

15.2.5. 音频合成剪辑

			
# 对白
dialogue1 = AudioFileClip("voice/tom.mp3")
dialogue2 = AudioFileClip("voice/jerry.mp3")

# 制作混合音轨
concat = concatenate_audioclips([dialogue1, dialogue2])
compo = CompositeAudioClip([dialogue1.volumex(1.2), # 音量设置大小
                            dialogue2.set_start(5)])# 音频起始时间				
			
			

15.2.6. 视频中设置背景音乐

			
bgm = AudioFileClip("music/paino.mp3")			
videoclip = videoclip.set_audio(bgm)			
			
			

15.2.7. 画面截图

			
# 保存视频第一帧的画面
clip.save_frame("Screenshot.jpeg") 

# 保存视频第10秒的画面
clip.save_frame("Screenshot.png", t=10) 
			
			

15.2.8.