Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何让用户在youtube dl中选择质量?_Python_Tkinter_Youtube Dl - Fatal编程技术网

Python 如何让用户在youtube dl中选择质量?

Python 如何让用户在youtube dl中选择质量?,python,tkinter,youtube-dl,Python,Tkinter,Youtube Dl,我正在构建一个简单的GUI应用程序(在Tkinter中),它使用youtube dl下载youtube视频。我知道如何让它下载最高分辨率,但我想包括一个下拉列表,允许用户选择他们想要下载视频的分辨率。我知道如何在GUI中创建实际列表,但不知道如何让youtube dl使用该分辨率(我只知道我可以使用'format':'best')。这里使用这个: import youtube_dl as yt from tkinter import * from tkinter import Tk, font,

我正在构建一个简单的GUI应用程序(在Tkinter中),它使用youtube dl下载youtube视频。我知道如何让它下载最高分辨率,但我想包括一个下拉列表,允许用户选择他们想要下载视频的分辨率。我知道如何在GUI中创建实际列表,但不知道如何让youtube dl使用该分辨率(我只知道我可以使用
'format':'best'
)。

这里使用这个:

import youtube_dl as yt
from tkinter import *
from tkinter import Tk, font, ttk


def display_options(*event):

    hide()
    
    ydl_opts = {}
    resolutions = []

    url = link.get()
    
    valid = valid_link(url)  # check the validity of link
 
    if valid:
        with yt.YoutubeDL(ydl_opts) as ydl:
            meta = ydl.extract_info(url, download=False) 
            formats = meta.get('formats', meta) 
            vid_title = meta.get('title', meta) # gets the title

        title.config(text=f'Title: {vid_title}')
        title.place(x=220, y=70)

        for f in formats:
            resolutions.append(f['format']) #filter to only formats

        options['values'] = resolutions # sets combobox values to available resolutions

    else:
        error.config(text='Invalid link', fg='red')
        error.place(x=150, y=210)

def valid_link(link): # function to check validity of the link
   
    extractors = yt.extractor.gen_extractors()
    for e in extractors:
        if e.suitable(link) and e.IE_NAME != 'generic':
            return True
    return False


def download():
    hide()
    res = option_lst.get()

    if res !=  'resolution':
        res = int(res[:res.find('-')].strip())
        
        ydl_opts = {'format': f'{res}', 
                    'outtmpl': r'C:\yourPath\%(title)s.%(ext)s'}  # specify your download path here

        url = link.get()
        
        with yt.YoutubeDL(ydl_opts) as ydl: 
            ydl.download([url])

    else:
        error.config(text='Invalid resolution', fg='red')
        error.place(x=150, y=210)
    
def hide():
    title.place_forget()
    error.place_forget()


root = Tk()
root.geometry('700x250')
root.title('YouTube Downloader')

link = StringVar()
option_lst = StringVar()

link.trace('w', display_options)
option_lst.set('resolution')

link_here = Label(root, text="Paste Here", font=(30))
link_here.place(x=220,y=40)

title = Label(root, text='')
title.place(x=220, y=230)

error = Label(root, text='')
error.place(x=220, y=210)

options = ttk.Combobox(root, textvariable=option_lst, state="readonly") 
options.place(x=300, y=210)

pasted = Entry(root,width=60,textvariable=link)
pasted.place(x=80,y=120)

Button(root,text="Download Video", width=20, bg="black", fg="gray", command=download).place(x=250,y=170)

root.mainloop()
如果需要,您可以进一步改进此功能。

此处使用此功能:

import youtube_dl as yt
from tkinter import *
from tkinter import Tk, font, ttk


def display_options(*event):

    hide()
    
    ydl_opts = {}
    resolutions = []

    url = link.get()
    
    valid = valid_link(url)  # check the validity of link
 
    if valid:
        with yt.YoutubeDL(ydl_opts) as ydl:
            meta = ydl.extract_info(url, download=False) 
            formats = meta.get('formats', meta) 
            vid_title = meta.get('title', meta) # gets the title

        title.config(text=f'Title: {vid_title}')
        title.place(x=220, y=70)

        for f in formats:
            resolutions.append(f['format']) #filter to only formats

        options['values'] = resolutions # sets combobox values to available resolutions

    else:
        error.config(text='Invalid link', fg='red')
        error.place(x=150, y=210)

def valid_link(link): # function to check validity of the link
   
    extractors = yt.extractor.gen_extractors()
    for e in extractors:
        if e.suitable(link) and e.IE_NAME != 'generic':
            return True
    return False


def download():
    hide()
    res = option_lst.get()

    if res !=  'resolution':
        res = int(res[:res.find('-')].strip())
        
        ydl_opts = {'format': f'{res}', 
                    'outtmpl': r'C:\yourPath\%(title)s.%(ext)s'}  # specify your download path here

        url = link.get()
        
        with yt.YoutubeDL(ydl_opts) as ydl: 
            ydl.download([url])

    else:
        error.config(text='Invalid resolution', fg='red')
        error.place(x=150, y=210)
    
def hide():
    title.place_forget()
    error.place_forget()


root = Tk()
root.geometry('700x250')
root.title('YouTube Downloader')

link = StringVar()
option_lst = StringVar()

link.trace('w', display_options)
option_lst.set('resolution')

link_here = Label(root, text="Paste Here", font=(30))
link_here.place(x=220,y=40)

title = Label(root, text='')
title.place(x=220, y=230)

error = Label(root, text='')
error.place(x=220, y=210)

options = ttk.Combobox(root, textvariable=option_lst, state="readonly") 
options.place(x=300, y=210)

pasted = Entry(root,width=60,textvariable=link)
pasted.place(x=80,y=120)

Button(root,text="Download Video", width=20, bg="black", fg="gray", command=download).place(x=250,y=170)

root.mainloop()
如果你愿意,你可以进一步改进