File size: 1,888 Bytes
149dc47
 
 
07e567b
 
149dc47
07e567b
 
149dc47
07e567b
 
 
149dc47
07e567b
 
 
 
 
 
 
149dc47
 
 
07e567b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
149dc47
07e567b
 
149dc47
07e567b
 
 
 
 
 
 
 
149dc47
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
53
54
55
56
57
58
59
import streamlit as st
import yt_dlp

# Function to get video info (formats, quality, etc.)
def get_video_info(url):
    ydl_opts = {
        'quiet': True,
        'noplaylist': True,
    }
    with yt_dlp.YoutubeDL(ydl_opts) as ydl:
        info = ydl.extract_info(url, download=False)
    return info

# Function to download video
def download_video(url, format):
    ydl_opts = {
        'format': format,
        'outtmpl': '%(title)s.%(ext)s',  # Name the file based on the video title
        'noplaylist': True,
    }
    with yt_dlp.YoutubeDL(ydl_opts) as ydl:
        ydl.download([url])

# Streamlit layout
st.title("Video Downloader App")

# User input for video URL
url = st.text_input('Enter Video URL')

if url:
    try:
        # Get video information (formats)
        video_info = get_video_info(url)
        formats = video_info.get('formats', [])

        # Prepare list of available formats (e.g., 'best', 'mp4', 'webm')
        format_options = []
        for f in formats:
            # Ensure format has 'format_note' and 'ext' to avoid key errors
            if 'format_note' in f and 'ext' in f and 'filesize' in f:
                format_options.append(f"{f['format_note']} - {f['ext']} - {f['filesize']} bytes")

        if format_options:
            # User selects a format
            selected_format = st.selectbox('Select Video Quality', format_options)

            # Convert selected format to yt-dlp's format identifier
            selected_format_id = formats[format_options.index(selected_format)]['format_id']

            if st.button('Download'):
                with st.spinner('Downloading...'):
                    download_video(url, selected_format_id)
                st.success('Download complete!')
        else:
            st.error("No formats available.")
    except Exception as e:
        st.error(f"Error fetching video info: {e}")