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.
Install moviepy
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.
Hello
How would it to convert contents downloaded with requests.get, without saving to disk?
The method
audioclip.write_audiofile(mp3_file)
Requires a filename.
If I put instead:
audioclip.write_audiofile(r.content)
I’ve got an error
audioclip.write_audiofile(mp3_file)
, it means convert mp4 file to mp3 file and save it.However, if you have downloaded a mp4 from Internet and use a python variable
mp4_data
to save its content.You can not use
VideoFileClip(mp4_data)
, becauseVideoFileClip
need a filename.It will call
self.reader = FFMPEG_VideoReader(filename, pix_fmt=pix_fmt,
target_resolution=target_resolution,
resize_algo=resize_algorithm,
fps_source=fps_source)
by filename.
FFMPEG_VideoReader
will this filename to calldef ffmpeg_parse_infos(
filename, print_infos=False, check_duration=True, fps_source="tbr"
)
This code will call ffmpeg command to convert mp4 file.
https://github.com/Zulko/moviepy/blob/c8d02deea741be7f4196a31ea7ee0863769ca5b1/moviepy/video/io/ffmpeg_reader.py#L256
You can use a temporary file to save mp4 data, and delete it after having converted successfully. This is a good way, beacuse you may fail to convert, you can convert it again by avoiding to download mp4 file again.
I would like to ask if you need to convert multiple mp4 to mp3. Any suggestion how to do it?
There are three ways to convert multiple mp4 to mp3.
1.Convert mp4 files one by one
2.or concentrate mp3 files to one file
3.Concentrate mp4 files to a one file and then convert it to mp3
Here is a tutorial example:
https://www.tutorialexample.com/python-merge-two-videos-to-one-video-with-moviepy-a-completed-guide-python-tutorial/
Hi
when i run the script i get this error:
Traceback (most recent call last):
File “C:/Users/Sam/Desktop/python/music/mp4 to mp3/mp4 converter.py”, line 11, in
videoclip = VideoFileClip(mp4_file)
File “C:\Users\Sam\AppData\Local\Programs\Python\Python39\lib\site-packages\moviepy\video\io\VideoFileClip.py”, line 88, in __init__
self.reader = FFMPEG_VideoReader(filename, pix_fmt=pix_fmt,
File “C:\Users\Sam\AppData\Local\Programs\Python\Python39\lib\site-packages\moviepy\video\io\ffmpeg_reader.py”, line 37, in __init__
self.fps = infos[‘video_fps’]
KeyError: ‘video_fps’
do you have any recommendations on how to fix this?
Thanks, sam
solved
replace:
videoclip = VideoFileClip(mp4_file)
audioclip = videoclip.audio
audioclip.write_audiofile(mp3_file)
audioclip.close()
videoclip.close()
with:
audioclip = AudioFileClip(mp4_file)
audioclip.write_audiofile(mp3_file)
audioclip.close()
i constantly get a error: (my mp4s path): Permission Denied
Does anyone has an idea why this could be?
you may run your script as an administrator.