In this tutorial, I am going to be showing you how to convert files from mp4 to mp3, using python. In order to do this, we're going to be using the moviePy library, and it's only going to take about three lines of code. Yes! Three lines of code. That's the beauty of python as a lazy programmer😁.
Let's begin.
Installing moviePy
Firstly, we're going to need the moviePy library on our computer. So run the following command to install it:
> pip install moviepy
Import the necessary modules
from moviepy.editor import *
Load the mp4 file
Using the VideoFileClip
method, we would load our video into a variable video
video = VideoFileClip("example.mp4")
where example.mp4
is the filename and the format of the video you want to convert.
Extract audio from video
Then, we are going to extract the audio from the video and save it with our desired filename.
video.audio.write_audiofile("example.mp3")
where example.mp3
is the filename and the format of the audio extracted from example.mp4
.
Final code
from moviepy.editor import *
# Load the mp4 file
video = VideoFileClip("example.mp4")
# Extract audio from video
video.audio.write_audiofile("example.mp3")
So if you've done everything correctly, and run your code, you should see your conversion in progress in your terminal.
Top comments (3)
Simple! Straight forward! I love it !!
Thank you!😀