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😁.
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
VideoFileClip method, we would load our video into a variable
video = VideoFileClip("example.mp4")
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.
example.mp3 is the filename and the format of the audio extracted from
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.