Update app.py
Browse files12234567899101111121314151623 unmodified lines40414242434445464743444950515253545556575859454647484950515253545556575859606162636461626364656566676869686970707172735 unmodified lines7980818282838485import streamlit as stfrom yt_dlp import YoutubeDLfrom pytube import YouTubefrom phi.agent import Agentfrom phi.model.google import Geminifrom phi.tools.duckduckgo import DuckDuckGofrom google.generativeai import upload_file, get_fileimport google.generativeai as genai
import timeimport osfrom pathlib import Pathimport tempfilefrom dotenv import load_dotenvimport osimport tempfile
# Load environment variablesload_dotenv()23 unmodified lines# Initialize the agentmultimodal_Agent = initialize_agent()
import tempfilefrom yt_dlp import YoutubeDL
import browser_cookie3from yt_dlp import YoutubeDL
# Function to download YouTube video using pytubedef download_youtube_video(youtube_url): # Extract cookies from browser (Chrome, Firefox, etc.) cookies = browser_cookie3.load()
# yt-dlp options ydl_opts = { 'format': 'bestvideo+bestaudio/best', # Download best video and audio 'outtmpl': 'downloaded_video.mp4', # Output file path 'cookies': cookies, # Use cookies directly from browser 'quiet': True, # Suppress yt-dlp output }
""" Downloads a YouTube video using pytube. Parameters: - youtube_url: The URL of the YouTube video. Returns: - The path to the downloaded video file. """ # Initialize YouTube object using pytube yt = YouTube(youtube_url)
# Get the highest resolution stream available video_stream = yt.streams.get_highest_resolution()
# Temporary file for the video with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as temp_video: temp_video_path = temp_video.name try: # Download the video with YoutubeDL(ydl_opts) as ydl: ydl.download([youtube_url])
print("Download successful!") # Download the video to the temporary file video_stream.download(output_path=temp_video_path)
return temp_video_path # Return the video file path except Exception as e: print(f"An error occurred: {str(e)}")
raise RuntimeError(f"An error occurred while downloading the video: {str(e)}")
# YouTube video URL inputyoutube_url = st.text_input(5 unmodified linesif youtube_url: try: with st.spinner("Downloading and processing the YouTube video..."): # Download video using yt-dlp # Download video using pytube video_path = download_youtube_video(youtube_url)
# Display videoapp.py+29-29