-1

I created a simple program to convert video to audio, using the moviepy library and the program as follows:

import moviepy.editor as mp
from tkinter import *
from tkinter import ttk
from tkinter.filedialog import asksaveasfile, askopenfile
from tkinter.messagebox import showerror

def Open():
    video_path = askopenfile(initialdir = "Video", title = "Open Video", filetypes = (("Video", "*.MP4;*.AVI;*.WAV;*.MKV;*.webm;*.TS;*.ASF;*OOG"), ("All files", "*.*")))

    try:
        open(video_path.name, "r")
        ent_mp4.delete(0, END)
        ent_mp4.insert(0, video_path.name)
    except:
        pass

def save_in():
    if not(ent_mp4.get() == ""):
        saveas = asksaveasfile(initialdir = "Music", title = "Save in", filetype = [("Aduio", "*.MP3;*.FLAC;*.CD;*.WMV"), ("All filest", "*.*")])

        ent_mp3.delete(0, END)
        ent_mp3.insert(0, saveas.name)
    else:
        showerror("Error: Empty Entry", "Pleas Enter Your Video Path")

def convert():
    video = ent_mp4.get()
    music = ent_mp3.get()

    if not(music == ""):
        root.destroy()

        clip = mp.VideoFileClip(video)
        clip.audio.write_audiofile(music)

        main()
    else:
        showerror("Error: Empty Entry", "Pleas Enter Your Music Path")

ent_mp4, ent_mp3, root = None, None, None
bu1, bu2, bu3 = None, None, None
def main():
    global ent_mp3, ent_mp4, root, bu1, bu2, bu3, sv
    root = Tk()
    root.title("Convert Video to Audio")

    ent_mp4 = ttk.Entry(root, width = 50)
    ent_mp4.grid(row = 0, column = 0)

    ent_mp3 = ttk.Entry(root, width = 50)
    ent_mp3.grid(row = 1, column = 0)

    ttk.Button(root, text = "Open video", command = lambda: Open()).grid(row = 0, column = 1)

    ttk.Button(root, text = "Save in", command = lambda: save_in()).grid(row = 1, column = 1)

    f = ttk.Frame(root)
    f.grid(row = 2, column = 0, columnspan = 2)

    ttk.Button(f, text = "Convert", command = lambda: convert()).pack(side = LEFT)

    ttk.Button(f, text = "Exit", command = lambda: exit()).pack(side = RIGHT)

    root.mainloop()


main()

The program is successful but I just want to add something simple, which is that instead of removing the window when the root.destroy () switching process is running, an animated GIF is added that indicates that the process is in progress

I suspect that the solution is to apply two things at the same time for the process to succeed

But I didn't find how to try the "with" command but it didn't work out

So what is the solution

  • 1
    You probably want to run one of the command in async. – alter123 Dec 19 '19 at 17:15
  • 1
    First you have to understand [Event-driven programming](https://stackoverflow.com/a/9343402/7414759) and [use threads to preventing main event loop from “freezing”](https://stackoverflow.com/a/16747734/7414759) – stovfl Dec 19 '19 at 17:23

1 Answers1

0

Threading will alow two or more functions to run simultaneously.

from threading import Thread

def func1():
    print('Working')

def func2():
    print('Working')

if __name__ == '__main__':
    Thread(target = func1).start()
    Thread(target = func2).start()
Grigorian
  • 62
  • 1
  • 11