我如何使用 python 下载特定路径(下载文件夹)中的 YouTube 视频

时间:2021-04-22 23:22:11

标签: python youtube-dl

我正在使用 python 网络应用程序下载 YouTube 视频,当我选择下载按钮时,我希望将视频下载到我的“下载文件夹”中

import streamlit as st
import youtube_dl
st.title(" YouTube Downloader")
#Enter the URL
link = st.text_input("Enter the link here")
options = {
        "format": "bestvideo+bestaudio"
    }
 submit = st.button("download")
def download(link):
try:
    with youtube_dl.YoutubeDL(options) as ydl:
        ydl.download([link])
    if submit:
        st.success("successfully downloaded")

except youtube_dl.utils.DownloadError:
    raise st.error('this URL is invalid')

 if __name__ == '__main__':
download(link)

picture of the web page

terminal

2 个答案:

答案 0 :(得分:0)

您的提交按钮除了打印“已成功下载”消息外什么也不做。我相信您想要做的是在点击提交按钮时下载视频。在这种情况下,您的代码应该是:

def download(link):
    try:
        if submit:
            with youtube_dl.YoutubeDL(options) as ydl:
                ydl.download([link])

            st.success("successfully downloaded")
    except youtube_dl.utils.DownloadError:
        raise st.error('this URL is invalid')

答案 1 :(得分:-1)

您是否尝试检查您的工作目录? 如果这不起作用,您可以尝试使用 pytube (pip install pytube)

from pytube import YouTube
link = st.text_input('Enter a YouTube URL to download')
yt = YouTube(link)
out_file=yt.streams.filter(progressive=True,file_extension='mp4').order_by('resolution').desc().first().download('sample')