QA Graphic

Extract Audio from Video with a Simple Python Script

Short and Simple Script

Have you ever watched a video and wished you could save the audio separately? Maybe it's a podcast, an interview, or a speech that you'd like to listen to later. Instead of using online converters, why not take control and do it yourself with Python?

Today, I'll show you how to extract audio from a video file using a simple Python script!


Why Use Python for This?

Python provides powerful libraries for video and audio processing. Instead of relying on clunky websites or expensive software, you can use a few lines of Python code to: - Extract audio from any video format (.mp4, .mov, .avi, etc.). - Save the audio in high-quality .mp3 format. - Automate the process for batch conversion.


The Magic Behind the Script

This script uses moviepy, a robust Python library for video editing. Here's what the script does: 1. It takes a video file as an argument. 2. It loads the video and extracts its audio. 3. It saves the audio as an .mp3 file in the same directory as the video.

Note: moviepy was recently updated. So if you used that library in the past, make note of the way the import call is being used in this file.



import sys
import os
from moviepy import *
def extract_audio(video_path):
    """Extracts audio from a video file and saves it as an MP3 file."""
    if not os.path.exists(video_path):
        print(f"Error: File '{video_path}' not found.")
        return
    
    try:
        video = VideoFileClip(video_path)
        audio_path = os.path.splitext(video_path)[0] + ".mp3"
        video.audio.write_audiofile(audio_path)
        print(f"Audio extracted successfully: {audio_path}")
    except Exception as e:
        print(f"Error extracting audio: {e}")
if __name__ == "__main__":
    if len(sys.argv) != 2:
        print("Usage: python getaudio.py <video_file>")
        sys.exit(1)
    video_file = sys.argv[1]
    extract_audio(video_file)

How to Use the Script

Step 1: Install Dependencies

Before running the script, you'll need moviepy. Install it with:

pip install moviepy

This will also install imageio, numpy, and other required libraries.

Step 2: Run the Script

Navigate to the folder where your script (getaudio.py) is located. Run the following command:

python getaudio.py movie01.mov

The script will process the video and create an movie01.mp3 file.


Bonus: Convert Multiple Videos at Once

Want to extract audio from multiple videos in one go? Modify the script to process all video files in a folder:


import glob
video_files = glob.glob("*.mov")  # Adjust extension if needed
for video in video_files:
    extract_audio(video)

This will extract audio from all .mov files in the directory.


Final Thoughts

This simple script is a great starting point for audio extraction. You can expand it further by adding: - Support for different output formats (.wav, .ogg). - A graphical user interface (GUI) for easy file selection. - Batch processing of videos from different folders.

Now, you have a powerful tool at your fingertips to convert videos to audio with just one command. Give it a try and let me know how you use it!

Read more about MoviePy in the Official Documentation.