DISCLAIMER:
This project is highly beginner-friendly and deep explanation about moviepy.
This project is going to be crazy and can be completed in 4 lines of code…stay tuned.
INTRODUCTION:
Hello world.. here, I am gonna show you how to extract audio from video using python.
For this, we need a library called moviepy.
moviepy is a Python library used for video editings like cuttings, video concatenations, title insertions, video compositing i.e., non-linear editing, video processing, and creation of custom effects.
PREREQUISITES:
Simply, we need a single package.
we will use the moviepy package to complete this project.
PACKAGE INSTALLATION:
moviepy package depends on the python modules like Numpy, imageio, decorator, and tqdm which will be automatically installed during moviepy’s installation. The software FFMPEG will automatically download by the first use of moviepy.
To install the pip package use command (sudo) pip install package_name in the command prompt.
Command:
pip install moviepy
It will take a few seconds to install the moviepy package.
We have successfully installed the moviepy package.
PACKAGE EXPLANATION:
1. For the project, we just need the editor method of the moviepy module..hence, we will directly import the moviepy.editor
Import moviepy.editor
2. moviepy.editor.VideoFileClip( )
It is the function used to take the video path of the video clip.
Note: The video clip can be in any format..you have to specify the format correctly.
Note: if your video file is in the same folder then you can give the name directly..else you need to give the path of the video file.
3. audio is the function we used to extract the audio of the video clip.
we will see in the code implementation to understand more clearly.
4. audio.write_audiofile()
It is the function used to take the audiofile name to create the audiofile with given specific name.
Now, let’s start the code.
CODE IMPLEMENTATION:
Note: The Program will be completed in a Maximum of 4 lines.. to make it user-friendly the code will be Extended.
# Extract Audio From Video Using Python.
import moviepy.editor
s = input(‘Enter the video name: ‘) # the video can be any format like mp4…
# if your video file is in the same folder then you can give the name directly.
# else you need to give the path of the video file.
video = moviepy.editor.VideoFileClip(s)
audio = video.audio
a = input(‘Enter the audio file name: ‘) # Enter the audio file name to be saved with..
# the audio file will be in mp3 format.
audio.write_audiofile(a)
# audio file will be created in the same directory.
Here is the complete source code for the project.
# Extract Audio From Video Using Python..
import moviepy.editor
s = input('Enter the video name: ') # the video can be any format like mp4...
# if your video file is in the same folder then you can give the name directly..
# else you need to give the path of the video file..
video = moviepy.editor.VideoFileClip(s)
audio = video.audio
a = input('Enter the audio file name: ') # Enter the audio file name to be saved with..
# the audio file will be in mp3 format..
audio.write_audiofile(a)
# audio file will be created in the same directory..
Example output:
I have downloaded a video file named video(.mp4) in the same directory.
I am gonna extract audio from this video(.mp4).
Given video name: video.mp4
And audio name: audio.mp3
Here is the audio.
The program automatically extracted and created the audio file naming audio(.mp3) in the same directory.
Important Note:
Please note that you can change the video and get the desired audio from the video.
Thanks for your visit.
Make sure you comment below for the projects you need..or more simple projects like this…
where is 3d hand tracking full code.🤔
We are going to write an article on 3D hand tracking and upload it soon…Stay tuned.