Video to Audio Converter in Python with Source Code | Moviepy in Python | Only 3 line code

Hello friends how are you, Today in is this post “Video to Audio Converter in Python” I am going to teach you how you can build your own Video to Audio Converter using 3 lines of code. In this project we will upload a video and then extract the audio from it. The most interesting thing in this project is that you can convert Video file into Audio without internet connection means we can say it offline converter. 

Several times we need to get the audio from a video file , by keeping it in my mind I have just made this post for you. If you are a computer science student then it may be an interesting small project for you because in this post you will get a GUI based Video to Audio Converter.
 
Now i am going to explain it step by step If you don't know how to run Python Program then click here
  
Install library (moviepy) : Before doing code for this we have to first, install the library which is used to extract audio from video file. If you are using Pycharm IDE then To install this library just click on Terminal which will be in Left bottom of screen , you will get a console screen and in this console screen you have to type pip install moviepy and then press enter. After some seconds you will get a message like library is installed successfully. Still facing problem then click here to watch video, i have explained everything.  But if you are using Python IDLE or Command window then type the below command 

pip install moviepy

Python Code: Now its time to write the code. Here in the code in the first line i have imported a python package moviepy which is used to handle video in Python. In the next line just passed the video file that i want to convert into Audio file, and in the last line i have provided a name (krazy.mp3) for generated audio file but you can type any name for your converted audio file. 

#import library
import moviepy.editor as movie
#get video file
videoClip = movie.VideoFileClip("video file name with extension")
#convert it to audio file and save
#here the generated audio filename is krazy
videoClip.audio.write_audiofile("krazy.mp3")

How to Run: Just type the above code your python file or you can copy this code for your personal use. Store a Video file in the same directory where you have created your python file then pass the name of video file in the second line inside the function VideoClipFile(). When you will run this code you will get an audio file of your uploaded video file and the name of audio file will be krazy.mp3 if you don't change in the above code. When you will run the above code you will get output screen like below screenshot

Video to Audio Converter in Python with Source Code


GUI Based Video to Audio Converter: Wait wait wait I have created something interesting for you actually i have created a GUI based Application in which you can upload Video file and then you can convert it to Audio file. Here is the complete code for GUI Application. 

#import library
from fpdf import FPDF
from tkinter import*
from tkinter import filedialog
import moviepy.editor as movie

#converting video into audio
def convert():
    clip = movie.VideoFileClip(fname)
    #generated audio filename is krazy
    clip.audio.write_audiofile("krazy.mp3")
    msgForGenerate.set("Audio file generated successfully")
#uploading Video File
def UploadAction():
    #uploading file
    filename = filedialog.askopenfilename(filetypes =[('Mp4 Files', '*.mp4'),('WMV Files', '*.wmv')])
    #showing message for uploaded file
    msgForUpload.set("Uploaded:"+filename)
    #declaring global variable
    global fname
    #passing uploaded filename into fname
    fname=filename
#creating GUI for Converter
root =Tk()
#setting window height and width
root.geometry("600x200")
#setting window background color
root["bg"]="#0B6E69"
#setting title of window
root.title("Krazy:Video to Audio Converter")
#creating variable for messages
global msgForUpload
global msgForGenerate
msgForUpload=StringVar()
msgForGenerate=StringVar()
#label for uploading message
Label(root,text="Hello",textvariable=msgForUpload,font="Arial 12",bg="#0B6E69",fg="white").place(x=80,y=30)
#button for upload file
Button(root, text='Upload Video', command=UploadAction,font="Arial 12 bold",width="12",bg="#AB1C0D",fg="white").place(x=80,y=70)
#button for convert
Button(root,text="Convert",font="Arial 12 bold",width="10",command=convert,bg="#AB1C0D",fg="white").place(x=350,y=70)
#label for generated/success message
Label(root,text="Hello",textvariable=msgForGenerate,font="Arial 12",bg="#0B6E69",fg="white").place(x=80,y=120)
root.mainloop()

When you will run this code you will get screen like below

Video to Audio Converter in Python with Source Code

Here click on Upload video button to upload video file and then click on Convert button to convert it into Audio file and save it into your system. When you will upload file and click on button then after some second you will get a message like below screenshot

Video to Audio Converter in Python with Source Code

and output console scree will look like below screenshot

Video to Audio Converter in Python with Source Code


I hope Now you can create Video to Audio Converter


Request:-If you found this post helpful then let me know by your comment and share it with your friend. 

If you want to ask a question or want to suggest then type your question or suggestion in comment box so that we could do something new for you all. 

If you have not subscribed my website then please subscribe my website. Try to learn something new and teach something new to other. 

Thanks.😊

Post a Comment

0 Comments