|
| 1 | +# MP4 to MP3 Converter |
| 2 | + |
| 3 | +# imported necessary library |
| 4 | +from tkinter import Tk, Button, Text, END |
| 5 | +import tkinter as tk |
| 6 | +from tkinter import filedialog |
| 7 | +import tkinter.messagebox as mbox |
| 8 | +from PIL import Image, ImageTk |
| 9 | +import moviepy |
| 10 | +import moviepy.editor |
| 11 | +import os |
| 12 | + |
| 13 | +# created a main window |
| 14 | +window = Tk() |
| 15 | +window.title('MP4 to MP3 Converter') |
| 16 | +window.geometry("1000x700") |
| 17 | + |
| 18 | +# top label |
| 19 | +start1 = tk.Label(text="MP4 to MP3 Converter", |
| 20 | + font=("Arial", 50), |
| 21 | + fg="magenta") |
| 22 | +start1.place(x=130, y=10) |
| 23 | + |
| 24 | +# image on the main window |
| 25 | +path = "Images/convert.jpg" |
| 26 | +# Creates a Tkinter-compatible photo image |
| 27 | +# which can be used everywhere Tkinter expects an image object. |
| 28 | +img1 = ImageTk.PhotoImage(Image.open(path)) |
| 29 | +# The Label widget is a standard Tkinter widget used to display |
| 30 | +# a text or image on the screen. |
| 31 | +panel = tk.Label(window, image=img1) |
| 32 | +panel.place(x=170, y=120) |
| 33 | + |
| 34 | + |
| 35 | +def mp4_choose(): |
| 36 | + global filename, onlyfilename |
| 37 | + filename = filedialog.askopenfilename(initialdir="/", |
| 38 | + title="Choose MP4", |
| 39 | + filetypes=(("Text files", "*.Mp4*"), |
| 40 | + ("all files", "*.*"))) |
| 41 | + # Change label contents |
| 42 | + # label_file_explorer.configure(text="File : " + filename) |
| 43 | + onlyfilename = os.path.basename(filename) |
| 44 | + fname.delete('1.0', END) |
| 45 | + fname.insert(END, onlyfilename) |
| 46 | + |
| 47 | + |
| 48 | +# select label |
| 49 | +select1 = tk.Label(text="Select MP4 File : ", font=("Arial", 30), fg="brown") |
| 50 | +select1.place(x=50, y=500) |
| 51 | + |
| 52 | +# textarea for file name only |
| 53 | +fname = Text(window, |
| 54 | + height=1, |
| 55 | + width=23, |
| 56 | + font=("Arial", 25), |
| 57 | + bg="light yellow", |
| 58 | + fg="brown", |
| 59 | + borderwidth=2, |
| 60 | + relief="solid") |
| 61 | +fname.place(x=360, y=505) |
| 62 | + |
| 63 | +# created a choose button , to choose the image from the local system |
| 64 | +chooseb = Button(window, |
| 65 | + text='SELECT', |
| 66 | + command=mp4_choose, |
| 67 | + font=("Arial", 17), |
| 68 | + bg="light green", |
| 69 | + fg="blue", |
| 70 | + borderwidth=3, |
| 71 | + relief="raised") |
| 72 | +chooseb.place(x=800, y=500) |
| 73 | + |
| 74 | + |
| 75 | +# Function for convert Mp4 to Mp3 |
| 76 | +def convert(): |
| 77 | + video = moviepy.editor.VideoFileClip(filename) |
| 78 | + # Convert video to audio |
| 79 | + audio = video.audio |
| 80 | + |
| 81 | + aud_fname = "" |
| 82 | + for i in onlyfilename: |
| 83 | + if i == '.': |
| 84 | + break |
| 85 | + else: |
| 86 | + aud_fname = aud_fname + i |
| 87 | + print(aud_fname) |
| 88 | + audio.write_audiofile(f'{aud_fname}.mp3') |
| 89 | + mbox.showinfo("Success", |
| 90 | + "Video converted to Audio.\n\nAudio Saved Successfully") |
| 91 | + |
| 92 | + |
| 93 | +# created a choose button , to choose the image from the local system |
| 94 | +convertb = Button(window, |
| 95 | + text='CONVERT MP4 To MP3', |
| 96 | + command=convert, font=("Arial", 20), |
| 97 | + bg="light green", |
| 98 | + fg="blue", |
| 99 | + borderwidth=3, |
| 100 | + relief="raised") |
| 101 | +convertb.place(x=150, y=600) |
| 102 | + |
| 103 | + |
| 104 | +# defined exit_win function, to show a exit dialog box when tried to exit |
| 105 | +def exit_win(): |
| 106 | + if mbox.askokcancel("Exit", "Do you want to exit?"): |
| 107 | + window.destroy() |
| 108 | + |
| 109 | + |
| 110 | +# creating an exit button |
| 111 | +exitB = Button(window, |
| 112 | + text='EXIT', |
| 113 | + command=exit_win, |
| 114 | + font=("Arial", 20), |
| 115 | + bg="red", |
| 116 | + fg="blue", |
| 117 | + borderwidth=3, |
| 118 | + relief="raised") |
| 119 | +exitB.place(x=750, y=600) |
| 120 | + |
| 121 | +# this is done to show the exit dialog box when tried to exit from |
| 122 | +# main window using the top-roght close button of titlebar |
| 123 | +window.protocol("WM_DELETE_WINDOW", exit_win) |
| 124 | +window.mainloop() |
0 commit comments