yt-download / app.py
slimshadow's picture
Update app.py
11553f4 verified
import streamlit as st
import os
import yt_dlp
from google.oauth2 import service_account
from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload
# Streamlit UI setup
st.title("YouTube Video Downloader 8K supported")
# Authenticate using service account credentials
creds = service_account.Credentials.from_service_account_file(
'apiuploaderslimshadow-41d2e11608f5.json',
scopes=['https://www.googleapis.com/auth/drive']
)
# Build the Drive API service
drive_service = build('drive', 'v3', credentials=creds)
# Streamlit input fields
url = st.text_input("Enter the YouTube URL:")
quality = st.selectbox("Select the desired quality:", ['4320', '2160', '1440', '1080', '720', '480'])
save_file_name = st.text_input("Enter the save file name (without extension):")
# Convert quality to the appropriate format string for yt-dlp
quality_format = f"bestvideo[height<={quality}]+bestaudio/best[height<={quality}]"
# Define download options based on user input
ydl_opts = {
'format': quality_format, # Download the specified quality
'outtmpl': f'{save_file_name}.%(ext)s', # Save the video with the specified name
}
if st.button("Download"):
# Download the video
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
ydl.download([url])
# Path to the downloaded video file
file_path = f'{save_file_name}.webm'
# Check if the file exists
if os.path.exists(file_path):
# File metadata for Google Drive
file_metadata = {
'name': f'{save_file_name}.webm', # Change the file name and extension accordingly
'parents': ['1WbQdOPwSXkNhIppYBHf2D0mdbeUAW12d'], # ID of the folder where you want to upload
'mimeType': 'video/mp4' # Specify the correct MIME type for the video file
}
# Upload the file to Google Drive
media = MediaFileUpload(file_path, mimetype='video/mp4')
file = drive_service.files().create(body=file_metadata, media_body=media, fields='id').execute()
# Print the URL for direct download
file_id = file.get('id')
download_url = f'https://www.googleapis.com/drive/v3/files/{file_id}?alt=media&key=AIzaSyCHbHbqiqdJDOMrGc8-f-55dIz6L4nRLiM'
st.success('Download URL:')
st.write(download_url)
else:
st.error("File not found. Please make sure the download was successful.")