|
| 1 | +from tkinter import * |
| 2 | +from moviepy.editor import VideoFileClip |
| 3 | +from moviepy.editor import AudioFileClip |
| 4 | +from tkinter import filedialog |
| 5 | +from tkinter import messagebox |
| 6 | +from tkinter import ttk |
| 7 | +import threading |
| 8 | + |
| 9 | +# Variables for video and audio files |
| 10 | +video_file = '' |
| 11 | +audio_file = '' |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +def get_video_file(): |
| 16 | + """ |
| 17 | + Function that gets the video file that needs to be converted |
| 18 | + """ |
| 19 | + global video_filepath, video_file |
| 20 | + video_filepath.set(filedialog.askopenfilename(title="Select your video file", filetypes=[ |
| 21 | + ('MP4 (mp4, m4a, m4v, f4v, f4a, m4b, m4r, f4b, mov)','*.mp4 *.m4a *.m4v *.f4v *.f4a *.m4b *.m4r *.f4b *.mov'), |
| 22 | + ('3GP (3gp, 3gp2, 3g2, 3gpp, 3gpp2)','*.3gp *.3gp2 *.3g2 *.3gpp *.3gpp2'), |
| 23 | + ('OGG (ogg, oga, ogv, ogx)','*.ogg *.oga *.ogv *.ogx'), |
| 24 | + ('WMV (wmv, wma)','*.wmv *.wma'), |
| 25 | + ('FLV','*.flv'), ('AVI','*.avi'), ('MPEG-1 (mpg, mp2, mpeg, mpe, mpv )','*.mpg *.mp2 *.mpeg *.mpe *.mpv'), |
| 26 | + ('MPEG-2','*.mpg *.mpeg *.m2v')])) |
| 27 | + video_file = VideoFileClip(str(video_filepath.get())) |
| 28 | + |
| 29 | +def save_audio_file(): |
| 30 | + """ |
| 31 | + Function that converts video file into audio file in a path that the user chooses |
| 32 | + """ |
| 33 | + global audio_filepath, audio_file, progress_bar |
| 34 | + audio_filepath.set(filedialog.asksaveasfilename(defaultextension='.mp3', |
| 35 | + title="Select your audio file directory", filetypes=[ |
| 36 | + ('MP3 File','*.mp3'), ('Wave File','*.wav')])) |
| 37 | + try: |
| 38 | + audio_file = video_file.audio |
| 39 | + audio_file.write_audiofile(str(audio_filepath.get())) |
| 40 | + video_file.close() |
| 41 | + audio_file.close() |
| 42 | + messagebox.showinfo(message="File converted successfully") |
| 43 | + except: |
| 44 | + messagebox.showerror(message="File could not be converted", title="File Error") |
| 45 | + # Resetting the video and audio paths |
| 46 | + video_filepath.set('') |
| 47 | + audio_filepath.set('') |
| 48 | + # Resetting the progressbar after function execution |
| 49 | + progress_bar['value'] = 0 |
| 50 | + progress_bar.stop() |
| 51 | + |
| 52 | +def run_program(): |
| 53 | + """ |
| 54 | + Function that runs the process of conversion and loading bar concurrently |
| 55 | + """ |
| 56 | + global progress_bar |
| 57 | + t1 = threading.Thread(target=progress_bar.start) |
| 58 | + t2 = threading.Thread(target=save_audio_file) |
| 59 | + t2.start() |
| 60 | + t1.start() |
| 61 | + |
| 62 | +# Intializing main program settings |
| 63 | +main_prog = Tk() |
| 64 | +main_prog.title("Video to Audio Converter") |
| 65 | +main_prog.maxsize(800, 400) |
| 66 | +main_prog.minsize(500,200) |
| 67 | +main_prog.config(bg="ivory") |
| 68 | + |
| 69 | + |
| 70 | +# Variables for file paths |
| 71 | +video_filepath = StringVar() |
| 72 | +audio_filepath = StringVar() |
| 73 | + |
| 74 | +# Creating UI Frame |
| 75 | +UI_frame = Frame(main_prog, width=500, height=500, bg="ivory") |
| 76 | +UI_frame.grid(row=0, column=0) |
| 77 | + |
| 78 | +# Labels and buttons of the program |
| 79 | +Label(UI_frame, text="Choose your video file", bg="ivory").grid(row=1, column=1, padx=5, pady=5, sticky=W) |
| 80 | +Button(UI_frame, text="Browse", command=get_video_file, bg="grey").grid(row=1, column=2, padx=5, pady=5) |
| 81 | +Button(UI_frame, text="Convert", command=run_program, bg="green").grid(row=2, column=2, padx=5, pady=5) |
| 82 | +Label(UI_frame, textvariable=video_filepath, bg="ivory").grid(row=1, column=3, padx=5, pady=5, sticky=W) |
| 83 | + |
| 84 | + |
| 85 | +progress_bar = ttk.Progressbar(main_prog, orient=HORIZONTAL, mode='indeterminate', length=500) |
| 86 | +progress_bar.grid(padx=25,pady=25) |
| 87 | + |
| 88 | +# Calling main program |
| 89 | +main_prog.mainloop() |
0 commit comments