Spaces:
Runtime error
Runtime error
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.") | |