How to convert mp4 to mp3 using python

23,885

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.

Share:
23,885
Alejandro Veintimilla
Author by

Alejandro Veintimilla

I am an economist that decided to study programming languages for web development. So, I have been learning python for 10 months and recently started learning django.

Updated on June 27, 2021

Comments

  • Alejandro Veintimilla
    Alejandro Veintimilla about 3 years

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

    I have looked at several libraries without success.

  • Sarang Manjrekar
    Sarang Manjrekar almost 5 years
    give me the error while using VideoFileClip : KeyError: 'video_fps'
  • Yaroslav Dukal
    Yaroslav Dukal over 4 years
    same error.. any solutions to this??
  • megha
    megha almost 3 years
    This worked for me. @YaroslavDukal from moviepy.editor import * def mp4_to_mp3(mp4, mp3): mp4_without_frames = AudioFileClip(mp4) mp4_without_frames.write_audiofile(mp3) mp4_without_frames.close() # function call mp4_to_mp3("my_mp4_path.mp4", "audio.mp3")
  • drascom
    drascom over 2 years
    thanks megha your solution is working at 01/2022