How to Extract Audio from Video in Python

Learn how to convert video to audio using ffmpeg and subprocess, or using MoviePy library in Python.
  · 3 min read · Updated sep 2021 · Python for Multimedia


If you want to convert your video into an audio format using Python, then you're in the right place. In this tutorial, I will show you two simple methods to extract the audio from a video file with the help of FFmpeg and MoviePy library in Python.

Before we get started, you need to have FFmpeg installed on your machine. If you're on Windows, check this tutorial where you simply install it and add it to the PATH variable. If you're on Linux, then it's straightforward to install it using the following commands:

$ sudo apt update
$ sudo apt install ffmpeg

Note that MoviePy depends on the FFmpeg software for video reading and writing, and it'll automatically install it when you first use it. So you shouldn't worry much if you fail the above step and want to use the second method (i.e using MoviePy).

Let's install MoviePy now:

$ pip install moviepy

Method 1: Using FFmpeg Directly

This method involves using Python's built-in subprocess module to the appropriate FFmpeg command:

import subprocess
import os
import sys

def convert_video_to_audio_ffmpeg(video_file, output_ext="mp3"):
    """Converts video to audio directly using `ffmpeg` command
    with the help of subprocess module"""
    filename, ext = os.path.splitext(video_file)
    subprocess.call(["ffmpeg", "-y", "-i", video_file, f"{filename}.{output_ext}"], 
                    stdout=subprocess.DEVNULL,
                    stderr=subprocess.STDOUT)

if __name__ == "__main__":
    vf = sys.argv[1]
    convert_video_to_audio_ffmpeg(vf)

In the convert_video_to_audio_ffmpeg() function, we first split the input video file name into the original file name without extension and the extension itself, so we can get the output file name by simply adding the audio extension to the original name. In this case, it's mp3.

We use the subprocess.call() method to run the FFmpeg command, the -y flag is for ignoring the overwrite prompt, and -i for specifying the input video file. The last argument is the output file name with the mp3 audio extension, you can change it to wav if you want but that resulted in a large audio file in my case for some reason.

Lastly, we pass subprocess.DEVNULL to stdout argument, and subprocess.STDOUT to stderr so we can discard the FFmpeg output. You can run the script via:

$ python video2audio_ffmpeg.py zoo.webm

Where zoo.webm is your video file with any format.

Method 2: Using MoviePy

In this method, we use the pretty straightforward MoviePy library that uses FFmpeg under the hood:

import os
import sys
from moviepy.editor import VideoFileClip


def convert_video_to_audio_moviepy(video_file, output_ext="mp3"):
    """Converts video to audio using MoviePy library
    that uses `ffmpeg` under the hood"""
    filename, ext = os.path.splitext(video_file)
    clip = VideoFileClip(video_file)
    clip.audio.write_audiofile(f"{filename}.{output_ext}")


if __name__ == "__main__":
    vf = sys.argv[1]
    convert_video_to_audio_moviepy(vf)

Here, we instantiate the VideoFileClip() class, and then use the write_audiofile() method from the audio object of the clip to save it as an audio file. Using the script:

$ python video2audio_moviepy.py zoo.webm

Conclusion

Alright, that's it for this quick tutorial, I hope it helped you accomplish your projects quickly and reliably.

Learn also: How to Play and Record Audio in Python.

Happy coding ♥

View Full Code
Sharing is caring!



Read Also




Comment panel