Welcome folks today in this blog post we will be using the moviepy
library to add the audio in video
file in command line. All the full source code of the application is shown below.
Get Started
First of all we need to install the below moviepy
library using the pip
command as shown below
pip install moviepy
In order to get started you need to make an app.py
file and copy paste the following code
app.py
1 2 3 4 5 6 7 8 9 10 |
# Import everything needed to edit video clips from moviepy.editor import * # loading video dsa gfg intro video clip = VideoFileClip("video.mp4") # getting only first 5 seconds clip = clip.subclip(0, 5) |
As you can see we are importing the moviepy
library at the very top and then we are only taking the first 5
seconds of the input video using the subclip()
method
1 2 3 4 5 6 7 8 |
# 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() |
As you can see we are loading the audio clip
using the AudioFileClip class of moviepy library and then we are only taking the first 5 seconds of the audio file using the subclip()
method. And then we are inserting
the audio inside the video file using the set_audio()
method. And then we are saving the resultant
video file with the audio as shown below