Intro
You can convert any Mp4 to Mp3 using this app.I use tkinter to select file and a for GUI interface.If you don't want to use tkinter so don't use.
I didn't not add error handling code
Requirements
tkinter which is pre installed with python
pip install moviepy
Moviepy: MoviePy (full documentation) is a Python library for video editing: cutting, concatenations, title insertions, video compositing (a.k.a. non-linear editing), video processing, and creation of custom effects. See the gallery for some examples of use.
MoviePy can read and write all the most common audio and video formats, including GIF, and runs on Windows/Mac/Linux, with Python 2.7+ and 3 (or only Python 3.4+ from v.1.0).
Let's code
First, You need to add these import statements
import tkinter as tk
from tkinter.filedialog import askopenfile
from tkinter.messagebox import showinfo
from moviepy.editor import VideoFileClip
I am using class you can use function. Create a class Converter inherit the tkinter class, make a constructor, set the window height and width, create a label, create a button to select the mp4 file and give a function to run on pressing the button.
Get more tkinter projects ideas here
class Converter(tk.Tk):
def __init__(self):
super().__init__()
self.geometry("500x250")
convert_label = tk.Label(self, text="Convert to mp3:", font="lucida 40")
convert_label.pack()
select_mp3 = tk.Button(self, text="Select", font="lucida 15", command=self.convert_to_mp3)
select_mp3.pack()
Now create the function which convert the file mp4 to mp3. askopenfile function is used to open a popup window to select the file in reading mode and store the file name using .name and replace the mp4 to mp3 in string to save the file as mp3
Give the mp4 file name in VideoFileClip and store the audio in audio variable and now use the write_audiofile function by giving the mp3 file name then close the video, audio and use the showinfo to display a message when the file is converted.
def convert_to_mp3(self):
file = askopenfile(mode="r", filetypes=[('mp4 file', '*.mp4')])
mp4_file = file.name
mp3_file = mp4_file.replace("mp4", "mp3")
video = VideoFileClip(mp4_file)
audio = video.audio
audio.write_audiofile(mp3_file)
audio.close()
video.close()
showinfo(title="Done", message="Your mp4 file has been converted to mp3\nCheck your directory")
Now finally create the class instace and run the mainloop function
convert = Converter()
convert.mainloop()
The all code look like
print("Importing...")
import tkinter as tk
from tkinter.filedialog import askopenfile
from tkinter.messagebox import showinfo
from moviepy.editor import VideoFileClip
print("Working...")
class Converter(tk.Tk):
def __init__(self):
super().__init__()
self.geometry("500x250")
convert_label = tk.Label(self, text="Convert to mp3:", font="lucida 40")
convert_label.pack()
select_mp3 = tk.Button(self, text="Select", font="lucida 15", command=self.convert_to_mp3)
select_mp3.pack()
def convert_to_mp3(self):
file = askopenfile(mode="r", filetypes=[('mp4 file', '*.mp4')])
mp4_file = file.name
mp3_file = mp4_file.replace("mp4", "mp3")
video = VideoFileClip(mp4_file)
audio = video.audio
audio.write_audiofile(mp3_file)
audio.close()
video.close()
showinfo(title="Done", message="Your mp4 file has been converted to mp3\nCheck your directory")
convert = Converter()
convert.mainloop()
If you are still reading here please like my content
For more projects subscribe to channel and
Get more Python projects here
AFFILIATE DISCLOSER: This post includes affiliate links.I may receive compensation if you purchase products or services from the different links provided in this article.
Top comments (0)