Convert Mp4 to Mp3 using Moviepy in Python

In this tutorial, we are going to see how to convert Mp4 to Mp3 files using Python. This task can be easily done using the moviepy library from Python.

First, install the moviepy library

pip install moviepy

Now import the required functions from the library.

from moviepy.editor import VideoFileClip,AudioFileClip

Now, initialize the variables for audio and video files.
Note: Add your video file to the current folder to use in the code

mp4_file ="Video.mp4"#video file name
mp3_file="audio.mp3"#create new audio file

Using VideoFileClip(path) function from the moviepy library read the video file (.mp4)

videoclip = VideoFileClip(mp4_file)

Now convert the video into audio (.mp3) file

audioclip = videoclip.audio

Now write the audio into MP3 file we have previously created (audio.mp3) using write_audiofile(path) function

audioclip.write_audiofile(mp3_file)

Output :

MoviePy - Writing audio in audio.mp3
MoviePy - Done.

Close the video and audio files.

audioclip.close()
videoclip.close()

Also refer, Merge two videos using moviepy in Python

Leave a Reply

Your email address will not be published. Required fields are marked *