-2

I'm working on a web app python to download a YouTube video, and when I select the download button I want the video downloaded in my 'Downloads Folder'

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

Lama
  • 3
  • 4

2 Answers2

0

Your submit button is doing nothing but print "successfully downloaded" message. What I believe you wanted to do was download the video on submit button click. In that case, your code should be:

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

JordanKobeWade
  • 338
  • 1
  • 7
-1

Did you try checking in your working directory? If that doesn't work, you can try using 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')