How to convert mp4 to mp3 using python

Question:

How can I convert a mp4 or mpeg4 file to mp3 using python?

I have looked at several libraries without success.

Answers:

This sounds like task for MoviePy. After you install it (installation howto) it could be used following way:

import os
from moviepy.editor import *
video = VideoFileClip(os.path.join("path","to","movie.mp4"))
video.audio.write_audiofile(os.path.join("path","to","movie_sound.mp3"))

Just replace "path","to","movie.mp4" and "path","to","movie_sound.mp3" according to your needs.

EDIT: To avoid the KeyError: ‘video_fps’, do ensure that you aren’t inputting any video which does not contain any visual content.

From Comments:

from moviepy.editor import *

def MP4ToMP3(mp4, mp3):
    FILETOCONVERT = AudioFileClip(mp4)
    FILETOCONVERT.write_audiofile(mp3)
    FILETOCONVERT.close()

VIDEO_FILE_PATH = "/Full/File/Path/ToSong.mp4"
AUDIO_FILE_PATH = "/Full/File/Path/ToSong.mp3"

MP4ToMP3(VIDEO_FILE_PATH, AUDIO_FILE_PATH)
# MoviePy - Writing audio in /Full/File/Path/ToSong.mp3
# MoviePy - Done.                                                                                                                   
Answered By: Daweo
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.