MoviePy – 将音频剪辑分配给视频文件
在本文中,我们将了解如何在 MoviePy 中将外部音频添加到视频文件剪辑中。 MoviePy 是一个用于视频编辑的Python模块,可用于视频和 GIF 的基本操作。视频是由帧组成的,帧的组合创建一个视频,每一帧都是一个单独的图像。音频文件格式是用于在计算机系统上存储数字音频数据的文件格式。音频数据的位布局称为音频编码格式,可以不压缩,也可以压缩以减小文件大小,通常使用有损压缩。我们可以借助AudioFileClip
方法加载音频文件。
In order to do this we will use set_audio
attribute with the video file clip object
Syntax : clip.set_audio(audio)
Argument : It takes AudioFileClip object as argument
Return : It returns VideoFileClip object
下面是实现
# Import everything needed to edit video clips
from moviepy.editor import *
# loading video dsa gfg intro video
clip = VideoFileClip("dsa_geek.mp4")
# getting only first 5 seconds
clip = clip.subclip(0, 5)
# loading audio file
audioclip = AudioFileClip("allwell.mp3").subclip(0, 5)
# adding audio to the video clip
videoclip = clip.set_audio(audioclip)
# showing video clip
videoclip.ipython_display()
输出 :
Moviepy - Building video __temp__.mp4.
MoviePy - Writing audio in __temp__TEMP_MPY_wvf_snd.mp3
MoviePy - Done.
Moviepy - Writing video __temp__.mp4
Moviepy - Done !
Moviepy - video ready __temp__.mp4
另一个例子
# Import everything needed to edit video clips
from moviepy.editor import *
# loading video gfg
clip = VideoFileClip("geeks.mp4")
# getting only first 5 seconds
clip = clip.subclip(0, 5)
# loading audio file
audioclip = AudioFileClip("allwell.mp3").subclip(0, 5)
# adding audio to the video clip
videoclip = clip.set_audio(audioclip)
# showing video clip
videoclip.ipython_display()
输出 :
Moviepy - Building video __temp__.mp4.
MoviePy - Writing audio in __temp__TEMP_MPY_wvf_snd.mp3
MoviePy - Done.
Moviepy - Writing video __temp__.mp4
Moviepy - Done !
Moviepy - video ready __temp__.mp4