Last modified: Apr 16, 2026 By Alexander Williams

Extract Audio from Video with Python

You have a video file. You need just the audio. Maybe it's for a podcast, a sample, or analysis. Ripping audio from video is a common task. Python makes it simple.

This guide will show you how. We will use powerful libraries. You will learn step-by-step. No prior experience is needed. Let's get started.

Why Extract Audio with Python?

Python is a versatile language. It is great for automation. Extracting audio manually is slow. Python scripts do it in seconds.

You can process hundreds of files. You can integrate it into larger apps. It's perfect for developers and data scientists. For a broader look at handling sound, see our Python Audio Processing Guide for Beginners.

Prerequisites and Setup

First, ensure you have Python installed. Version 3.7 or higher is best. You will also need a package manager like pip.

The key tool is FFmpeg. It's a multimedia framework. Many Python libraries rely on it. Install it for your operating system.

On Ubuntu or Debian, use this command.


sudo apt-get install ffmpeg
    

On macOS, use Homebrew.


brew install ffmpeg
    

Windows users can download an executable from the FFmpeg website. Add it to your system PATH.

Method 1: Using MoviePy

MoviePy is a popular library. It is built on top of FFmpeg. It is easy to use for video editing tasks.

First, install the library using pip.


pip install moviepy
    

Now, let's write a script. We will import VideoFileClip. We will call the audio.write_audiofile method.


from moviepy.editor import VideoFileClip

# Load the video file
video = VideoFileClip("my_video.mp4")

# Extract the audio
audio = video.audio

# Write the audio to a file
audio.write_audiofile("extracted_audio.mp3")

# Close the clips to free resources
audio.close()
video.close()

print("Audio extraction complete!")
    

Run this script. It will create a new MP3 file. The file contains the audio from your video.

The VideoFileClip object loads the video. Its audio attribute gives you the audio track. The write_audiofile method does the saving.

You can change the output format. Use ".wav" for WAV files. Use ".ogg" for OGG. MoviePy handles the conversion.

Method 2: Using FFmpeg Directly with Subprocess

Sometimes you want more control. You can call FFmpeg directly from Python. Use the subprocess module.

This method is powerful. You can use all FFmpeg options. You don't need extra Python libraries.


import subprocess

# Define input and output file paths
input_video = "my_video.mp4"
output_audio = "extracted_audio.aac"

# Build the FFmpeg command
command = [
    'ffmpeg',
    '-i', input_video,      # Input file
    '-vn',                  # Disable video recording
    '-acodec', 'copy',      # Copy the audio codec (no re-encoding)
    output_audio
]

# Run the command
try:
    subprocess.run(command, check=True, capture_output=True, text=True)
    print(f"Success! Audio saved to {output_audio}")
except subprocess.CalledProcessError as e:
    print(f"An error occurred: {e.stderr}")
    

The -vn flag tells FFmpeg to ignore video. The -acodec copy flag copies the audio stream. It does not re-encode. This is very fast.

If you need to convert formats, change the -acodec flag. For example, use libmp3lame for MP3.

Method 3: Using pydub and ffmpeg

Pydub is another excellent library. It is focused on audio manipulation. It requires FFmpeg to handle video files.

It's part of a rich ecosystem for sound. Explore more tools in our guide on Python Audio Libraries: Play, Record, Process.

Install pydub with pip.


pip install pydub
    

Now, use it to extract audio.


from pydub import AudioSegment

# Load video file (requires ffmpeg)
video = AudioSegment.from_file("my_video.mp4", format="mp4")

# Export as an audio file
video.export("output_audio.wav", format="wav")

print("Audio extracted with pydub!")
    

The from_file method is smart. It detects the file type. Specify "mp4" format to be safe. The export method saves the file.

Pydub makes it easy to then edit the audio. You can trim, change volume, or add effects.

Comparing the Methods

Which method should you choose? It depends on your needs.

MoviePy is best for simple, high-level tasks. It's part of a video editing library. Great for quick scripts.

FFmpeg via subprocess offers maximum control. Use it for complex workflows or batch processing. You have access to all FFmpeg features.

Pydub is ideal if you plan to process the audio further. It provides an intuitive API for audio manipulation after extraction.

Handling Errors and Common Issues

Sometimes things go wrong. Here are common fixes.

FFmpeg not found: Ensure FFmpeg is installed and in your system's PATH. The error will say "FileNotFoundError" or similar.

Unsupported codec: The video might use a rare audio format. Try specifying a different codec in FFmpeg, like `-acodec libmp3lame`.

Large file sizes: Extracting raw audio can create big files. Use FFmpeg to compress. Add a bitrate flag: `-b:a 128k`.

Always use try-except blocks in your code. This catches errors gracefully.

Conclusion

Extracting audio from video in Python is straightforward. You have multiple tools at your disposal.

For most users, MoviePy provides the easiest path. For power users, direct FFmpeg commands are unbeatable. For audio-focused projects, pydub is a great choice.

You can now automate this task. Process whole folders of videos. Integrate audio extraction into your apps. The possibilities are vast.

Start with the simple MoviePy example. Then experiment with the other methods. You will quickly master audio extraction with Python.