How to convert mp4 to mp3 using python

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…


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Jima Victor

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.


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Jima Victor


Print Share Comment Cite Upload Translate Updates
APA

Jima Victor | Sciencx (2023-01-19T15:28:30+00:00) How to convert mp4 to mp3 using python. Retrieved from https://www.scien.cx/2023/01/19/how-to-convert-mp4-to-mp3-using-python/

MLA
" » How to convert mp4 to mp3 using python." Jima Victor | Sciencx - Thursday January 19, 2023, https://www.scien.cx/2023/01/19/how-to-convert-mp4-to-mp3-using-python/
HARVARD
Jima Victor | Sciencx Thursday January 19, 2023 » How to convert mp4 to mp3 using python., viewed ,<https://www.scien.cx/2023/01/19/how-to-convert-mp4-to-mp3-using-python/>
VANCOUVER
Jima Victor | Sciencx - » How to convert mp4 to mp3 using python. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/01/19/how-to-convert-mp4-to-mp3-using-python/
CHICAGO
" » How to convert mp4 to mp3 using python." Jima Victor | Sciencx - Accessed . https://www.scien.cx/2023/01/19/how-to-convert-mp4-to-mp3-using-python/
IEEE
" » How to convert mp4 to mp3 using python." Jima Victor | Sciencx [Online]. Available: https://www.scien.cx/2023/01/19/how-to-convert-mp4-to-mp3-using-python/. [Accessed: ]
rf:citation
» How to convert mp4 to mp3 using python | Jima Victor | Sciencx | https://www.scien.cx/2023/01/19/how-to-convert-mp4-to-mp3-using-python/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.