0

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()
mGz1337
  • 27
  • 4
  • Hello. I am trying to make it so you choose the filename's name, and the converter converts that file so you don't have to change the file that it converts through the code, pardon me if I didn't understand what you exactly said, I am new to python, moviepy and tkinter – mGz1337 Nov 10 '20 at 12:24
  • Why do you involve tkinter into this question and make it more complex than necessary? Your problem is unrelated to tkinter? What's the value of `video`? Add a breakpoint and check it. Tipp: It's empty. Find out why it's empty. Tipp: Fix the indentation. Do you get the error message before you click the button? How could it be possible? – Thomas Sablik Nov 10 '20 at 12:24
  • It is related to tkinter, the input is from tkinter. i am trying to make a converter with UI that uses moviepy to convert the file, and tkinter for UI – mGz1337 Nov 10 '20 at 12:29
  • What happens after you click the button and what happens before you click the button? Do you read `video = e.get()` before or after you click the button? Do you create `VideoFileClip(mp4_file)` before or after you click the button? – Thomas Sablik Nov 10 '20 at 12:31
  • @ThomasSablik It happens when I try to run the file – mGz1337 Nov 10 '20 at 12:32
  • Why is it and how could it be possible? How should it be? When should the variable be read and when should the object be created? – Thomas Sablik Nov 10 '20 at 12:32
  • @ThomasSablik I get the error when I open the file – mGz1337 Nov 10 '20 at 12:33
  • You haven't chosen the the filename but you are getting an error. How could it be? – Thomas Sablik Nov 10 '20 at 12:34
  • @ThomasSablik So, in the input you give the file's name, you press convert, moviepy looks for the file that has the same name as the input, and it converts it to mp3. When I try to execute the file, it gives me the errors that I have put before the code – mGz1337 Nov 10 '20 at 12:38
  • Because you start the conversion before you click. The logic is not part of the click function. But it should be. – Thomas Sablik Nov 10 '20 at 12:39

1 Answers1

1

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()
Thomas Sablik
  • 16,127
  • 7
  • 34
  • 62