Python moviepy library can convert mp4 to mp3 easily, in this tutorial, we will write an example to discuss how to convert, you can follow our steps to learn how to do.
You can use pip command to install moviepy library
pip install moviepy
If you are using anaconda, you also can use conda command to install.
conda install -c conda-forge moviepy
Import moviepy library
from moviepy.editor import *
Set mp4 file and mp3 file
You can set the file name of mp4 and mp3.
mp4_file = r'E:\VID_20180308_141907.mp4' mp3_file = r'E:\VID_20180308_141907.mp3'
Convert mp4 to mp3 using python moviepy
In this example, we use mp4 file to create a VideoFileClip object, then get the audo object of this mp4 file, finally save the audo of this mp4 into a mp3 file.
The example code is:
videoclip = VideoFileClip(mp4_file) audioclip = videoclip.audio audioclip.write_audiofile(mp3_file) audioclip.close() videoclip.close()
Run this python script, you will find result like:
Then convert mp4 to mp3 successfully.
Notice: When you are converting, you may find error like:
AttributeError: cffi library ‘_openssl’ has no function, constant or global variable named ‘Cryptography_HAS_ECDH’
To fix this error, you can check this tutorial.