如何将 Tkinter 输入与 Moviepy 一起使用?

How can I use Tkinter input with Moviepy?

我做了一个终端 mp4 到 mp3 转换器。我正在尝试为它制作一个 UI 版本,但它不起作用。我做了一个 tkinter 输入,所以你把视频的名字放到输入中,它应该会转换它。但是要进行 UI 输入,我必须使用 tkinter,但是如果我尝试将 tkinter 输入用于 moviepy 代码,它将输入作为文件名。错误称为:OSError: MoviePy error: the file could not be found! Please check that you entered the correct path.知道如何解决这个问题吗?

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()

您必须将逻辑移动到函数中:

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()