Spaces:
Sleeping
Sleeping
import streamlit as st | |
from pytube import YouTube | |
import os | |
import base64 | |
# Function to download YouTube video and return the path of the downloaded file | |
def download_video(url, selected_quality): | |
try: | |
yt = YouTube(url) | |
st.write("Video Title:", yt.title) | |
st.write("Downloading...") | |
stream = yt.streams.filter(progressive=True, file_extension='mp4').get_by_resolution(selected_quality) | |
download_path = os.path.join(os.path.expanduser("~"), "Downloads") | |
downloaded_file_path = stream.download(output_path=download_path) | |
st.success("Download completed successfully!") | |
return downloaded_file_path | |
except Exception as e: | |
st.error(f"An error occurred: {e}") | |
return None | |
# Function to create a download link | |
def create_download_link(file_path): | |
file_name = os.path.basename(file_path) | |
with open(file_path, "rb") as f: | |
bytes = f.read() | |
b64 = base64.b64encode(bytes).decode() | |
href = f'<a href="data:file/mp4;base64,{b64}" download="{file_name}">Download {file_name}</a>' | |
return href | |
# Streamlit UI | |
st.title("YouTube Video Downloader") | |
url = st.text_input("Enter YouTube Video URL:", "") | |
quality_options = ["1080p", "720p", "480p", "360p"] | |
selected_quality = st.selectbox("Select Video Quality:", quality_options) | |
if st.button("Download"): | |
if url.strip() == "": | |
st.warning("Please enter a YouTube video URL.") | |
else: | |
downloaded_file_path = download_video(url, selected_quality) | |
if downloaded_file_path: | |
download_link = create_download_link(downloaded_file_path) | |
st.markdown(download_link, unsafe_allow_html=True) | |