How to Extract Audio from AVI File Using Python

How to Extract Audio from AVI File Using Python

Hey friends! Today, I’m going to show you how to easily extract audio from an AVI video file using a simple Python script. Whether you’re on Windows, Linux, or Mac, this method will work smoothly. Let’s get right into it!


Step 1: Install FFmpeg (Required for MoviePy)

FFmpeg is a powerful multimedia framework needed to handle video and audio operations.

For Windows:

  • Download FFmpeg from https://ffmpeg.org/download.html.
  • Extract the ZIP file.
  • Add FFmpeg to your system PATH:
    1. Right-click This PC > Properties > Advanced system settings > Environment Variables.
    2. Under System variables, find Path, click Edit, then add the path to the bin folder (e.g., C:\ffmpeg\bin).

For Linux (Ubuntu/Debian):

sudo apt update && sudo apt install ffmpeg

For Mac:

Install via Homebrew (if you don’t have Homebrew, install it first):

brew install ffmpeg

Step 2: Install MoviePy

MoviePy is a handy Python library that simplifies video and audio editing tasks.

Install it using pip:

pip install moviepy

Step 3: Write the Python Script

Create a Python file (e.g., extract_audio_from_avi.py) and paste the following code:

from moviepy.editor import VideoFileClip
import os

def extract_audio_from_avi(avi_file, audio_file=None):
    """
    Extract audio from AVI file and save as MP3
    """
    if audio_file is None:
        audio_file = os.path.splitext(avi_file)[0] + '.mp3'

    video = VideoFileClip(avi_file)
    audio = video.audio
    audio.write_audiofile(audio_file)
    audio.close()
    video.close()

# Example usage
extract_audio_from_avi("input.avi", "output.mp3")

This script loads the AVI file, extracts the audio track, and saves it as an MP3 file.


Step 4: Run the Script

  • Place your AVI file (e.g., input.avi) in the same folder as the script.
  • Open your terminal or command prompt.
  • Navigate to the folder where the script is located.
  • Run the script with:
    python extract_audio_from_avi.py

Your extracted audio (output.mp3) will be created in the same directory.


SEO Keywords: extract audio from AVI, AVI to MP3 conversion, Python MoviePy audio extraction, FFmpeg install guide, how to get audio from AVI file

No comments:

Post a Comment