Spaces:
Build error
Build error
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) |