我是编程新手,我正在从youtube制作一个简单的mp3下载器。而我的问题是我无法实现如何将filedialog.askdirectory()
目录更改为下载的音乐。我可以在哪里定义目录,请查看以下内容:
from tkinter import *
from tkinter import filedialog
from tkinter import messagebox
import youtube_dl
import sys
root=Tk()
root.resizable(0,0)
def save():
messagebox.showinfo('Wybór', 'Wybierz folder docelowy')
directory=filedialog.askdirectory()
ydl_opts = {
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([e1.get()])
messagebox.showinfo('Zakonczone','Pobieranie zakonczone')
def exit():
sys.exit()
root.title('Youtube downloader')
l1=Label(root,text='Wpisz adres url filmu Youtube',font=20)
l1.pack()
e1=Entry(root,width=50)
e1.pack()
b1=Button(root,text='Pobierz',width=7,height=2,command=save)
b1.pack(side=LEFT)
b2=Button(root,text='Wyjdź',command=exit,width=7,height=2)
b2.pack(side=RIGHT)
root.mainloop()
答案 0 :(得分:0)
要在指定文件夹中下载视频,请使用配置中的outtmpl
。
我创建了一个名为downloaded_music
的文件夹,并将代码更新如下:
from tkinter import *
from tkinter import filedialog
from tkinter import messagebox
import youtube_dl
import sys
root=Tk()
root.resizable(0,0)
def save():
#messagebox.showinfo('Wybór', 'Wybierz folder docelowy')
#messagebox.showinfo('Folder Selection', 'Select download folder')
#directory=filedialog.askdirectory()
directory = 'downloaded_music'
ydl_opts = {
'outtmpl': 'downloaded_music/%(title)s-%(id)s.%(ext)s',
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([e1.get()])
#messagebox.showinfo('Zakonczone','Pobieranie zakonczone')
messagebox.showinfo('Success','Download completed')
def exit():
sys.exit()
if __name__ == '__main__':
root.title('Youtube downloader')
#l1=Label(root,text='Wpisz adres url filmu Youtube',font=20)
l1=Label(root,text='Download MP3 from Youtube video',font=20)
l1.pack()
e1=Entry(root,width=50)
e1.pack()
#b1=Button(root,text='Pobierz',width=7,height=2,command=save)
b1=Button(root,text='Download',width=7,height=2,command=save)
b1.pack(side=LEFT)
#b2=Button(root,text='Wyjdź',command=exit,width=7,height=2)
b2=Button(root,text='Exit',command=exit,width=7,height=2)
b2.pack(side=RIGHT)
root.mainloop()
现在下载的文件存储在downloaded_music
文件夹中。