Python PyQT5 Project
MP4 TO MP3 USING PYTHON
- In this tutorial, you will learn how to convert an mp4 video file to an mp3 audio file using a python application with step by step examples.
- This is done by using the python library named moviepy
- This project is implemented using python GUI library pyqt5
Installing library moviepy
- The library moviepy has to be installed in order to convert mp4 video to mp3 file.
- Use the syntax below to install moviepy
- pip install moviepy
Example 1 (Installing moviepy via pip command in windows)
Example 2
Used Classes
1. VideoFileClip
- This is the built-in class which is used to load the input video file from the specified path
Used Instance Property
1. audio
- This is the built-in instance property which is used to convert the video into audio clip
- Returned type: AudioFileClip
Used Methods
1. write_audiofile(path)
- This method is used to write an audio file (mp3 file) from the AudioClip in the specified location
- This method will take multiple arguments where the first argument is the path of the filename. Atleast one argument needs to be given
- Return type: Any
Implementation Steps
- Load the required library named moviepy
- Load the input video file path into the constructor of VideoFileClip() class
- Convert video to audio using the special instance property of VideoClip class called audio.
- Save the converted mp3 file into your target location using the built-in method named write_audiofile(str)
CODE EXAMPLE – CONVERSION OF MP4 TO MP3 USING PYTHON GUI
1. Source Code
# load the moviepy module
from moviepy.editor import *
# import the pyqt5 modules
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
# import os module for file exists test
import os.path
# create an object for QApplication
obj=QApplication([])
# create a main window
w=QMainWindow()
# set the window title and its width and height
w.setWindowTitle(“Python MP4 to MP3 Converter”)
# disable the maximize button of window using built-in method setFixedSize()
w.setFixedSize(990,340)
# browse button code for loading input video file
def loadMP4():
# start the file dialog
fd=QFileDialog.getOpenFileName(w,”Open MP4 Video”,”c://”,”MP4 Video File(*.MP4);;All Files (*.*)”)
# check the returned tuple is not null for getting the video path
if len(fd)!=0:
# get the selected path of video file
path=str(fd[0])
# display the path in the text box
tb.setPlainText(path)
# convert button code
def convertMP3():
# get the path of input file in the text box
vpath=tb.toPlainText()
# attach the path of video file to VideoFileClip class
vc=VideoFileClip(vpath)
# convert video to audio using the special property audio
ac=vc.audio
# store or write the audio file in the current directory using the built-in method write_audiofile()
ac.write_audiofile(“file.mp3”)
# check the newly converted mp3 file exists or not using its path with exists() in if condition
if os.path.exists(“file.mp3”):
rs.setText(“MP3 File is generated successfully…”)
# create a Label Object
lb=QLabel(w)
lb.setText(“Mp4 to Mp3 Converter”)
# place the label in window
lb.setGeometry(350,10,500,40)
# apply the CSS theme to label using built-in method setStyleSheet()
lb.setStyleSheet(“color:green; font-size:27px; font-style:bold”)
# create another label
msg=QLabel(w)
# add the message to label
msg.setText(“Enter the Video File: “)
msg.setGeometry(20,70,300,40)
msg.setStyleSheet(“font-size:21px; font-style:bold”)
# create text box widget
tb=QTextEdit(w)
# place the text box in windows layout
tb.setGeometry(230,70,490,40)
# set the CSS properties
tb.setStyleSheet(“font-size:21px; font-style:bold”)
# create button widget
bt=QPushButton(“Browse”,w)
bt.setStyleSheet(“font-size:21px; font-style:bold; background-color:orange;color:black”)
# set the size of button
bt.setGeometry(725,70,140,40)
# set the event handler to browse button
bt.clicked.connect(loadMP4)
# add next button for converting MP4 to MP3
ct=QPushButton(“Convert”,w)
# set the CSS style to convert button
ct.setStyleSheet(“font-size:21px;background-color:red;color:white;font-style:bold”)
# place the button
ct.setGeometry(400,115,140,40)
# add the event handler to button
ct.clicked.connect(convertMP3)
# create a label for displaying the success message
rs=QLabel(w)
rs.setStyleSheet(“font-size:20px; color:#0000e6”)
rs.setGeometry(230,150,500,45)
# display the window
w.show()
# run the PyQt5 application using built in method exec()
obj.exec()
2. Result
2.1 Submission of Input Video File via File Dialog
2.2 Displaying the path of Selected Input Video in Text Box
2.3 Conversion Result
2.4 Verification of Newly Created MP3 File in Target Location