Extract audio from video files using Python

Introduction:

In multimedia processing, sometimes we need to extract audio from video files for further processing or analysis. This article will introduce how to use the Python programming language to extract audio from video files, and provide a simple GUI interface to facilitate user operation.

Text:
  1. Introduction: Introducing the background and importance of audio extraction, as well as the advantages of using Python programming for extraction.

  2. Environment preparation: Readers are reminded that they need to install the FFmpeg tool and add its path to the system environment variable before running the code.

  3. Code analysis:

    • Introducing the main libraries and modules: Tkinter, messagebox, filedialog, subprocess and os.

    • Explain the main functions and logic of the code: including single-file mode and multi-file mode, as well as the specific operation steps in each mode.

    • Comment and explain the key codes to help readers understand the implementation principle of the code.

  4. Detailed explanation of single file mode:

    • Introduces the operation process and interface layout of single file mode.

    • Explains how to select video files and audio output paths.

    • Describes how to use the FFmpeg command to extract audio from video files and save it to a specified path.

    • Demonstrates the operation effect of single file mode.

  5. Detailed explanation of multi-file mode:

    • Introduces the operation process and interface layout of multi-file mode.
    • Explain the way to select the video folder and audio output path.
    • Explains how to loop through the video files in a folder and extract the audio one by one using FFmpeg commands.
    • Demonstrate the operation effect of multi-file mode
  6. Summary and outlook:

    • Summary This article explains how to extract audio from video files using the Python programming language.
      -Remind readers that they can expand and optimize the code according to their own needs, such as adding progress bars, handling exceptions, etc.
    • Look ahead to possible future improvements and areas of application.
Code part:
import tkinter as tk
from tkinter import messagebox
from tkinter import filedialog
import subprocess
import os


#Single file
def single_video_mode():
    def choose_single_video():
        video_path = filedialog.askopenfilename()
        video_input.delete(0, tk.END)
        video_input.insert(0, video_path)

    def choose_audio_output():
        audio_output_path = filedialog.asksaveasfilename(defaultextension='.wav')
        audio_output_input.delete(0, tk.END)
        audio_output_input.insert(0, audio_output_path)

    def extract_audio():
        video_path = video_input.get()
        audio_output_path = audio_output_input.get()
        command = ['ffmpeg', '-i', video_path, '-vn', '-acodec', 'pcm_s16le', audio_output_path]
        subprocess.call(command)
        messagebox.showinfo('Extraction completed', 'Audio extraction completed!')

    root.withdraw()

    single_video_window = tk.Toplevel()
    single_video_window.title('single video')
    single_video_window.geometry('350x200')

    video_label = tk.Label(single_video_window, text='Video path:')
    video_label.place(x=32, y=3.5)

    video_input = tk.Entry(single_video_window)
    video_input.place(x=95, y=3.5)

    video_button = tk.Button(single_video_window, text='Choose video', command=choose_single_video, bg="#FFD45E")
    video_button.place(x=248, y=0)

    audio_output_label = tk.Label(single_video_window, text='Audio output path:')
    audio_output_label.place(x=10, y=53.5)

    audio_output_input = tk.Entry(single_video_window)
    audio_output_input.place(x=95, y=53.5)

    audio_output_button = tk.Button(single_video_window, text='Choose path', command=choose_audio_output, bg="#FFCCBE")
    audio_output_button.place(x=248, y=50)

    start_button = tk.Button(single_video_window, text='Start program', command=extract_audio, bg="#499C54")
    start_button.place(x=70, y=130)

    exit_button = tk.Button(single_video_window, text='Exit program', command=root.quit, bg="#C75450")
    exit_button.place(x=220, y=130)


#Multiple files
def multi_video_mode():
    def choose_videos_path():
        videos_path = filedialog.askdirectory()
        videos_path_input.delete(0, tk.END)
        videos_path_input.insert(0, videos_path)

    def choose_audio_output():
        audio_output_path = filedialog.asksaveasfilename()
        audio_output_input.delete(0, tk.END)
        audio_output_input.insert(0, audio_output_path)

    def extract_audio():
        videos_path = videos_path_input.get()
        audio_output_path = audio_output_input.get()
        for file in os.listdir(videos_path):
            if file.endswith('.mp4'):
                video_path = os.path.join(videos_path, file)
                base_name = os.path.splitext(file)[0]
                audio_path = os.path.join(audio_output_path, base_name + '.wav')
                command = ['ffmpeg', '-i', video_path, '-vn', '-acodec', 'pcm_s16le', audio_path]
                subprocess.call(command)

    root.withdraw()

    multi_video_window = tk.Toplevel()
    multi_video_window.title('Multiple videos')
    multi_video_window.geometry('350x200')

    videos_path_label = tk.Label(multi_video_window, text='Video folder path:')
    videos_path_label.place(x=10, y=3.5)

    videos_path_input = tk.Entry(multi_video_window)
    videos_path_input.place(x=105, y=3.5)

    videos_path_button = tk.Button(multi_video_window, text='Choose folder', command=choose_videos_path, bg="#FFD45E")
    videos_path_button.place(x=255, y=0)

    audio_output_label = tk.Label(multi_video_window, text='Audio saving path:')
    audio_output_label.place(x=20, y=53.5)

    audio_output_input = tk.Entry(multi_video_window)
    audio_output_input.place(x=105, y=53.5)

    audio_output_button = tk.Button(multi_video_window, text='Choose path', command=choose_audio_output, bg="#FFCCBE")
    audio_output_button.place(x=255, y=50)

    start_button = tk.Button(multi_video_window, text='Start program', command=extract_audio, bg="#499C54")
    start_button.place(x=70, y=130)

    exit_button = tk.Button(multi_video_window, text='Exit program', command=root.quit, bg="#C75450")
    exit_button.place(x=220, y=130)


root = tk.Tk()
root.title('Welcome to the tool for extracting audio from Lijiang Video')
root.geometry('200x100')
label = tk.Label(root, text="Huanyin uses Lijiang video audio extraction tool", fg="red")
label.place(x=10, y=10)
single_video_button = tk.Button(root, text='single video', command=single_video_mode, bg="#857022")
single_video_button.place(x=25, y=50)

multi_video_button = tk.Button(root, text='Multi Video', command=multi_video_mode, bg="#D3F899")
multi_video_button.place(x=125, y=50)

root.mainloop()

.