File size: 1,880 Bytes
561e61e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
597d109
 
561e61e
 
597d109
561e61e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import streamlit as st
import yt_dlp
import os
import tempfile

# Set the title of the app
st.title("YouTube Video Downloader")

# Input field for YouTube video URL
video_url = st.text_input("Enter YouTube Video URL:")

# Function to download video
def download_video(url):
    # Create a temporary directory to store the downloaded video
    with tempfile.TemporaryDirectory() as tmpdirname:
        # Define download options
        ydl_opts = {
            'format': 'bestvideo+bestaudio/best',
            'outtmpl': os.path.join(tmpdirname, '%(title)s.%(ext)s'),
            'merge_output_format': 'webm',
            'user_agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36',
        }


        # Download the video using yt-dlp
        with yt_dlp.YoutubeDL(ydl_opts) as ydl:
            # Use a progress hook to update the progress bar
            def progress_hook(d):
                if d['status'] == 'downloading':
                    st.progress(d['downloaded_bytes'] / d['total_bytes'])
                elif d['status'] == 'finished':
                    st.success("Download completed!")

            ydl.params['progress_hooks'] = [progress_hook]
            ydl.download([url])
        
        # Return the path of the downloaded video
        return os.path.join(tmpdirname, f"{ydl.prepare_filename(ydl.extract_info(url))}")

# Download button
if st.button("Download Video"):
    if video_url:
        # Display progress bar
        st.write("Downloading video...")
        downloaded_file_path = download_video(video_url)

        # Provide a download link
        st.write("Download completed! Click below to download your video:")
        st.markdown(f"[Download Video]({downloaded_file_path})", unsafe_allow_html=True)
    else:
        st.error("Please enter a valid YouTube URL.")