File size: 1,858 Bytes
bc39353
20a7e87
e43bdc1
bc39353
20a7e87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e43bdc1
20a7e87
088f881
e188bb1
20a7e87
 
 
 
728322d
e188bb1
7924003
 
20a7e87
728322d
7924003
 
bc39353
2e18f1a
7924003
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 gradio as gr
from pytube import YouTube
import os

def download_youtube_videos(urls):
    """Downloads multiple YouTube videos and returns a list of downloaded file paths."""
    downloaded_files = []
    for url in urls:
        try:
            yt = YouTube(url)
            video = yt.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first()
            filename = f"{yt.title}.mp4"
            video.download(filename=filename)
            downloaded_files.append(filename)
        except Exception as e:
            print(f"Error downloading {url}: {str(e)}")
    
    return downloaded_files

def app(urls):
    # Split the input string into a list of URLs
    url_list = [url.strip() for url in urls.split('\n') if url.strip()]
    
    if not url_list:
        return None, "No valid URLs provided. Please enter at least one YouTube URL."
    
    downloaded_files = download_youtube_videos(url_list)
    
    if downloaded_files:
        # Create a list of (filename, file path) tuples for Gradio File component
        file_tuples = [(f, f) for f in downloaded_files if os.path.exists(f)]
        message = f"Successfully downloaded {len(file_tuples)} videos."
        return file_tuples, message
    else:
        return None, "Download failed. Please check the URLs and try again."

with gr.Blocks() as interface:
    gr.Markdown("## YouTube Batch Video Downloader")
    urls_input = gr.Textbox(label="Enter YouTube links (one per line) 🔗", lines=5)
    download_button = gr.Button("Download Videos")
    output = gr.File(label="Downloaded Videos", file_count="multiple")
    message = gr.Markdown()

    download_button.click(
        fn=app,
        inputs=urls_input,
        outputs=[output, message],
        api_name="download"
    )

if __name__ == "__main__":
    interface.launch(debug=True)