import streamlit as st import yt_dlp import os # Set up the Streamlit app title and description st.title("YouTube Video Downloader") st.write(""" This app allows you to download YouTube videos by providing the video URL. Please use this app responsibly and only download videos that you have permission to. """) # Input for YouTube Video URL video_url = st.text_input('YouTube Video URL', placeholder="Enter the URL of the YouTube video...") # Input for Download Format download_format = st.selectbox( 'Download Format', ['mp4', 'mp3'] ) # Input for Quality Preferences (only show for mp4 format) quality_pref = None if download_format == 'mp4': quality_pref = st.selectbox( 'Video Quality', ['Highest', '720p', '480p', '360p', '240p'] ) # Input for Cookies File (optional) cookies_file = st.file_uploader("Upload cookies.txt (optional)", type=["txt"]) # Button to download video if st.button('Download'): if not video_url: st.error("Please provide a YouTube video URL.") else: try: # Define the output template and options for yt-dlp ydl_opts = { 'outtmpl': '%(title)s.%(ext)s', 'quiet': True, 'noplaylist': True, # Ensure only a single video is downloaded } # Set format based on user selection if download_format == 'mp3': ydl_opts.update({ 'format': 'bestaudio/best', 'postprocessors': [{ 'key': 'FFmpegExtractAudio', 'preferredcodec': 'mp3', 'preferredquality': '192', }], }) else: if quality_pref == 'Highest': ydl_opts['format'] = 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best' else: height = quality_pref[:-1] # Remove 'p' from quality ydl_opts['format'] = f'bestvideo[height<={height}][ext=mp4]+bestaudio[ext=m4a]/best[height<={height}][ext=mp4]/best' # Add cookies if provided if cookies_file is not None: # Save the uploaded cookies file temporarily cookies_path = "cookies.txt" with open(cookies_path, "wb") as f: f.write(cookies_file.getvalue()) ydl_opts['cookiefile'] = cookies_path # Download the video with yt_dlp.YoutubeDL(ydl_opts) as ydl: info_dict = ydl.extract_info(video_url, download=True) if download_format == 'mp3': file_path = info_dict['title'] + '.mp3' else: file_path = ydl.prepare_filename(info_dict) st.success(f"Video downloaded successfully: {file_path}") # Provide a download link with open(file_path, "rb") as file: st.download_button( label=f"Download {download_format.upper()} file", data=file, file_name=os.path.basename(file_path), mime="video/mp4" if download_format.lower() == "mp4" else "audio/mp3" ) # Clean up the downloaded file and cookies file (if any) os.remove(file_path) if cookies_file is not None: os.remove(cookies_path) except yt_dlp.utils.DownloadError as e: st.error(f"Error downloading video: {str(e)}. The video might be age-restricted, private, or the requested format isn't available.") except Exception as e: st.error(f"Unexpected error: {str(e)}") # Additional UI/UX features st.markdown(""" """, unsafe_allow_html=True) # Footer with copyright and year st.markdown("---") st.markdown('
© 2024 VidFlex by Engr. Sumayyea Salahuddin. All rights reserved.
', unsafe_allow_html=True)