Python Video to Audio Converter – Turn Visuals into Soundscapes

FREE Online Courses: Click, Learn, Succeed, Start Now!

A video to audio converter is an application that transforms video files into audio files. The user is presented with a GUI Window in which he can select a video file to convert using the button on the GUI Window. This video is converted to audio and stored in the system. Let us begin by constructing the video to audio converter project using Python libraries.

About Python Video to Audio Converter

We will use Python’s Tkinter and moviepy.editor libraries to create this application. We construct the GUI that accepts user input as a ‘.mp4’ file, extracts the audio from it, and saves it as a ‘.mp3’ file at a location given by the user.

Prerequisites for Video to Audio Converter Using Python

  • Basic knowledge of the Python programming language and how functions are defined in it.
  • How the window is made in Tkinter GUI and how the button is made and function is assigned to it.
  • How to work with the moviepy.editor library.

Download Python Video to Audio Converter Project

Please download the source code of Python Video to Audio Converter Project: Python Video to Audio Converter Project Code

Steps to Create Video to Audio converter Using Python

Following are the steps for developing the Python Video to Audio converter Project:

Step 1: Importing the necessary modules
Step 2: Making a window for our project
Step 3: Functions
Step 4: Making Frames and Mapping the Buttons to Their Functionalities

Step 1: Importing the necessary modules

To use Tkinter, we need to import the Tkinter module. We are also going to import the moviepy.editor library to get audio from the video file.
Code:

#import packages
from tkinter import *
from tkinter import filedialog
from moviepy.editor import *
from tkinter import messagebox

Step 2: Making a window for our project

This code sets the title of the window as ‘ProjectGurukul’, and sets the dimensions ‘width x length’.
Code:

root = Tk()
# give title and Set the size
root.geometry("300x300")
root.title("ProjectGurukul")

Step 3: Functions

This function extracts the audio from the input video file.
Code:

def convert():
   vid = filedialog.askopenfilename(title="Select Video", filetypes=[("Video Files", "*.mp4;*.mkv;*.avi")])
   if vid:
       video = VideoFileClip(vid)
       aud = filedialog.asksaveasfilename(defaultextension=".mp3", filetypes=[("Audio Files", "*.mp3")])
       if aud:
           audio = video.audio
           audio.write_audiofile(aud)
           messagebox.showinfo("Congrats", "Audio file is saved!")

Explanation:

The command ‘Video.audio’ transforms a video file to an audio file.
‘write_audiofile()’ The audio file is saved in the system using this approach.

Step 4: Making Frames and Mapping the Buttons to Their Functionalities

We make a label to give heading to the window “VIDEO TO AUDIO”, we create a button widget and assign the convert function to it.
Code:

Label(root, text="VIDEO TO AUDIO", bg='Lightblue', font=('Arial 13')).pack()
convert_button = Button(root, text="Convert Video to Audio", command=convert)
convert_button.pack(pady=20)
root.mainloop()

Full Code:

from tkinter import *
from tkinter import filedialog
from moviepy.editor import *
from tkinter import messagebox




def convert():
   vid = filedialog.askopenfilename(title="Select Video", filetypes=[("Video Files", "*.mp4;*.mkv;*.avi")])
   if vid:
       video = VideoFileClip(vid)
       aud = filedialog.asksaveasfilename(defaultextension=".mp3", filetypes=[("Audio Files", "*.mp3")])
       if aud:
           audio = video.audio
           audio.write_audiofile(aud)
           messagebox.showinfo("Congrats", "Audio file is saved!")


root = Tk()
# give title and Set the size
root.geometry("300x300")
root.title("ProjectGurukul")
Label(root, text="VIDEO TO AUDIO", bg='Lightblue', font=('Arial 13')).pack()
convert_button = Button(root, text="Convert Video to Audio", command=convert)
convert_button.pack(pady=20)


root.mainloop()

Final screens of the Video to Audio converter:

video to audio converter output

python video to audio converter output

Summary:

We completed the Python Video to Audio Converter Project successfully. We learned about many modules and functions in them, such as the filedialog and moviepy.editor libraries.

Leave a Reply

Your email address will not be published. Required fields are marked *