Search code examples
pythontkintermoviepy

How can I use Tkinter input with Moviepy?


I made a terminal mp4 to mp3 converter. I am trying to make a UI version to it, but it doesn't work. I made a tkinter input, so you put the video's name into the input, and it should convert it. But to make a UI input I have to use tkinter, but if I try to use the tkinter input into moviepy code, it takes the input as the name of the file. The error is called : OSError: MoviePy error: the file could not be found! Please check that you entered the correct path. Any idea how to fix this?

from tkinter import *
from moviepy.editor import *

window = Tk()

e = Entry(window, width=50)
e.pack()

def myClick():
  myLabel = Label(window, text="Converting the file named : " + e.get())
  myLabel.pack()
myButton = Button(window, text="Convert", command=myClick)
video = e.get()
myButton.pack()

mp4_file = video
mp3_file = "{}.mp3".format(mp4_file)
videoClip = VideoFileClip(mp4_file)
audioclip = videoClip.audio
audioclip.write_audiofile(mp3_file)
audioclip.close()
videoClip.close()

window.mainloop()

Solution

  • You have to move the logic into the function:

    from tkinter import *
    from moviepy.editor import *
    
    def myClick():
      myLabel = Label(window, text="Converting the file named : " + e.get())
      myLabel.pack()
      video = e.get()
      mp4_file = video
      mp3_file = "{}.mp3".format(mp4_file)
      videoClip = VideoFileClip(mp4_file)
      audioclip = videoClip.audio
      audioclip.write_audiofile(mp3_file)
      audioclip.close()
      videoClip.close()
    
    window = Tk()
    
    e = Entry(window, width=50)
    e.pack()
    
    myButton = Button(window, text="Convert", command=myClick)
    myButton.pack()
    
    window.mainloop()