Spaces:
Sleeping
Sleeping
File size: 6,379 Bytes
e1c4426 ca78672 7a16d12 ca78672 e1c4426 e132b35 7a16d12 e132b35 7a16d12 e132b35 7a16d12 e132b35 7a16d12 e132b35 7a16d12 e132b35 7a16d12 e132b35 7a16d12 e1c4426 7a16d12 e1c4426 7a16d12 e1c4426 7a16d12 e1c4426 7a16d12 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
import gradio as gr
import yt_dlp
import os
import tempfile
import shutil
from pathlib import Path
import re
import uuid
session_data = {}
class YouTubeDownloader:
def __init__(self):
self.download_dir = tempfile.mkdtemp()
def is_valid_youtube_url(self, url):
youtube_regex = re.compile(
r'(https?://)?(www\.)?(youtube|youtu|youtube-nocookie)\.(com|be)/'
r'(watch\?v=|embed/|v/|.+\?v=)?([^&=%\?]{11})'
)
return youtube_regex.match(url) is not None
def format_video_info(self, video_info):
if not video_info:
return "β No video information available."
duration = video_info.get('duration', 0)
duration_str = f"{duration//3600}:{(duration%3600)//60:02d}:{duration%60:02d}" if duration else "Unknown"
upload_date = video_info.get('upload_date', '')
formatted_date = f"{upload_date[:4]}-{upload_date[4:6]}-{upload_date[6:8]}" if len(upload_date) == 8 else upload_date or "Unknown"
def format_number(num):
if num >= 1_000_000:
return f"{num/1_000_000:.1f}M"
elif num >= 1_000:
return f"{num/1_000:.1f}K"
return str(num)
scene_descriptions = []
if duration:
chunk = 3
for start in range(0, duration, chunk):
end = min(start + chunk - 1, duration)
description = f"Visual segment from {start}s to {end}s. (e.g., close-up, presenter talks, etc.)"
scene_descriptions.append(f"* **[{start//60}:{start%60:02d}-{end//60}:{end%60:02d}]**: {description}")
else:
scene_descriptions.append("* No timestamped breakdown available.")
tags = ' '.join(video_info.get('tags', [])).lower()
title = video_info.get('title', '').lower()
description_text = video_info.get('description', '').lower()
channel = video_info.get('channel', '').lower()
if any(word in description_text for word in ['calm music', 'soft', 'soothing']):
music_style = "Calm"
elif any(word in description_text for word in ['energetic', 'upbeat', 'lively']):
music_style = "Upbeat"
elif "music" not in description_text:
music_style = "No music"
else:
music_style = "Unknown"
known_names = [
"Kartik Aaryan", "Virat Kohli", "Deepika Padukone", "Alia Bhatt", "Ranveer Singh",
"MrBeast", "PewDiePie", "CarryMinati", "Prajakta Koli", "Bhuvan Bam",
"Amitabh Bachchan", "Katrina Kaif", "Salman Khan", "Kiara Advani",
"Kylie Jenner", "Shahrukh Khan", "Ananya Pandey", "Ashish Chanchlani",
"Sundar Pichai", "Elon Musk", "Taylor Swift", "Janhvi Kapoor"
]
metadata = " ".join([
video_info.get('title', ''),
video_info.get('description', ''),
video_info.get('uploader', ''),
video_info.get('channel', ''),
' '.join(video_info.get('tags', []))
]).lower()
matched = [name for name in known_names if any(part in metadata for part in name.lower().split())]
if matched:
influencer_note = f"Yes, known influencer/celebrity detected: {', '.join(matched)}"
else:
influencer_note = "No known influencer or celebrity detected."
if any(word in metadata for word in ["actor", "brand ambassador", "featured", "with", "hosted by"]):
influencer_note += " (Someone might be featured β check visually)"
if "review" in title or "demo" in title or "how to" in title:
video_type = "Educational"
elif "ad" in title or "promo" in title or "launch" in title:
video_type = "Promotional"
elif "funny" in title or "challenge" in title:
video_type = "Entertainment"
else:
video_type = "Informational"
if any(word in description_text for word in ['excited', 'amazing', 'love']):
emotion = "Positive"
elif any(word in description_text for word in ['calm', 'soothing']):
emotion = "Neutral"
elif any(word in description_text for word in ['warning', 'serious']):
emotion = "Serious"
else:
emotion = "Neutral"
report = f"""πΉ VIDEO ANALYSIS REPORT
{'='*50}
π BASIC INFORMATION:
β’ Title: {video_info.get('title', 'Unknown')}
β’ Channel: {video_info.get('channel', 'Unknown')}
β’ Uploader: {video_info.get('uploader', 'Unknown')}
β’ Upload Date: {formatted_date}
β’ Duration: {duration_str}
π STATISTICS:
β’ Views: {format_number(video_info.get('view_count', 0))}
β’ Likes: {format_number(video_info.get('like_count', 0))}
β’ Comments: {format_number(video_info.get('comment_count', 0))}
β’ Channel Followers: {format_number(video_info.get('channel_followers', 0))}
π·οΈ CATEGORIES & TAGS:
β’ Categories: {', '.join(video_info.get('categories', [])) or 'None'}
β’ Tags: {', '.join(video_info.get('tags', [])[:10]) or 'None'}
{('β’ More tags...' if len(video_info.get('tags', [])) > 10 else '')}
π DESCRIPTION (first 500 chars):
{video_info.get('description', 'No description available')[:500]}
{'...' if len(video_info.get('description', '')) > 500 else ''}
π¬ SCENE-BY-SCENE BREAKDOWN:
{chr(10).join(scene_descriptions)}
π΅ BACKGROUND MUSIC STYLE: {music_style}
π€ INFLUENCER PRESENT: {influencer_note}
π₯ VIDEO TYPE: {video_type}
π OVERALL EMOTION: {emotion}
π VIDEO URL:
{video_info.get('webpage_url', 'Unknown')}
"""
return report.strip()
def get_video_info(self, url):
ydl_opts = {
'noplaylist': True,
'extract_flat': False,
}
try:
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info(url, download=False)
return info, "β
Info retrieved"
except Exception as e:
return None, f"β {str(e)}"
downloader = YouTubeDownloader()
def analyze(url):
info, msg = downloader.get_video_info(url)
if info:
return downloader.format_video_info(info)
else:
return msg
demo = gr.Interface(fn=analyze, inputs=gr.Textbox(label="YouTube URL"), outputs="text", title="YouTube Analyzer with Scene Breakdown")
if __name__ == "__main__":
demo.launch()
|