Spaces:
Sleeping
Sleeping
Update app.py
#6
by
Muktibhuyan
- opened
app.py
CHANGED
|
@@ -6,11 +6,21 @@ import shutil
|
|
| 6 |
from pathlib import Path
|
| 7 |
import re
|
| 8 |
import uuid
|
| 9 |
-
|
|
|
|
| 10 |
|
| 11 |
class YouTubeDownloader:
|
| 12 |
def __init__(self):
|
| 13 |
self.download_dir = tempfile.mkdtemp()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
def is_valid_youtube_url(self, url):
|
| 16 |
youtube_regex = re.compile(
|
|
@@ -19,214 +29,309 @@ class YouTubeDownloader:
|
|
| 19 |
)
|
| 20 |
return youtube_regex.match(url) is not None
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
def format_video_info(self, video_info):
|
| 23 |
-
"""
|
| 24 |
if not video_info:
|
| 25 |
return "β No video information available."
|
| 26 |
-
|
| 27 |
-
#
|
|
|
|
|
|
|
|
|
|
| 28 |
duration = video_info.get('duration', 0)
|
| 29 |
duration_str = f"{duration//3600}:{(duration%3600)//60:02d}:{duration%60:02d}" if duration else "Unknown"
|
| 30 |
|
| 31 |
-
# Format upload date
|
| 32 |
upload_date = video_info.get('upload_date', '')
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
else:
|
| 36 |
-
formatted_date = upload_date or "Unknown"
|
| 37 |
-
|
| 38 |
-
# Format numbers
|
| 39 |
def format_number(num):
|
| 40 |
-
if num
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
return f"{num/1_000_000:.1f}M"
|
| 42 |
elif num >= 1_000:
|
| 43 |
return f"{num/1_000:.1f}K"
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
#
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
{'='*50}
|
| 51 |
-
|
| 52 |
-
π BASIC INFORMATION:
|
| 53 |
-
β’ Title: {video_info.get('title', 'Unknown')}
|
| 54 |
-
β’ Channel: {video_info.get('channel', 'Unknown')}
|
| 55 |
-
β’ Uploader: {video_info.get('uploader', 'Unknown')}
|
| 56 |
-
β’ Upload Date: {formatted_date}
|
| 57 |
-
β’ Duration: {duration_str}
|
| 58 |
-
|
| 59 |
-
π STATISTICS:
|
| 60 |
-
β’ Views: {format_number(video_info.get('view_count', 0))}
|
| 61 |
-
β’ Likes: {format_number(video_info.get('like_count', 0))}
|
| 62 |
-
β’ Comments: {format_number(video_info.get('comment_count', 0))}
|
| 63 |
-
β’ Channel Followers: {format_number(video_info.get('channel_followers', 0))}
|
| 64 |
-
|
| 65 |
-
π·οΈ CATEGORIES & TAGS:
|
| 66 |
-
β’ Categories: {', '.join(video_info.get('categories', [])) or 'None'}
|
| 67 |
-
β’ Tags: {', '.join(video_info.get('tags', [])[:10]) or 'None'}
|
| 68 |
-
{('β’ More tags...' if len(video_info.get('tags', [])) > 10 else '')}
|
| 69 |
-
|
| 70 |
-
π DESCRIPTION:
|
| 71 |
-
{video_info.get('description', 'No description available')[:500]}
|
| 72 |
-
{'...' if len(video_info.get('description', '')) > 500 else ''}
|
| 73 |
-
|
| 74 |
-
π VIDEO URL:
|
| 75 |
-
{video_info.get('webpage_url', 'Unknown')}
|
| 76 |
-
"""
|
| 77 |
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
-
|
| 81 |
-
if not url or not url.strip():
|
| 82 |
-
return None, "β Please enter a YouTube URL"
|
| 83 |
|
| 84 |
-
|
| 85 |
-
|
|
|
|
|
|
|
| 86 |
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
ydl_opts['cookiefile'] = cookiefile
|
| 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 |
-
except Exception as e:
|
| 126 |
-
return None, f"β An unexpected error occurred: {str(e)}"
|
| 127 |
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
if not url or not url.strip():
|
| 130 |
return None, "β Please enter a YouTube URL"
|
| 131 |
-
|
| 132 |
if not self.is_valid_youtube_url(url):
|
| 133 |
-
return None, "β Invalid YouTube URL
|
| 134 |
-
|
| 135 |
try:
|
| 136 |
-
progress(0.1, desc="Initializing
|
|
|
|
| 137 |
ydl_opts = {
|
| 138 |
-
'format': 'best[ext=mp4]/best',
|
| 139 |
-
'outtmpl': os.path.join(self.download_dir, '%(title)s.%(ext)s'),
|
| 140 |
'noplaylist': True,
|
|
|
|
| 141 |
}
|
| 142 |
-
|
|
|
|
| 143 |
ydl_opts['cookiefile'] = cookiefile
|
| 144 |
-
|
| 145 |
-
progress(0.
|
|
|
|
| 146 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
downloaded_files = list(Path(self.download_dir).glob('*'))
|
| 155 |
-
if downloaded_files:
|
| 156 |
-
downloaded_file = downloaded_files[0]
|
| 157 |
-
file_size = downloaded_file.stat().st_size / (1024 * 1024)
|
| 158 |
-
progress(1.0, desc="Download completed!")
|
| 159 |
-
success_message = f"β
Successfully downloaded: {video_title}\n"
|
| 160 |
-
success_message += f"π File size: {file_size:.1f} MB\n"
|
| 161 |
-
success_message += f"β±οΈ Duration: {duration//60}:{duration%60:02d}" if duration else ""
|
| 162 |
-
return str(downloaded_file), success_message
|
| 163 |
-
else:
|
| 164 |
-
return None, "β Download completed but file not found"
|
| 165 |
-
except yt_dlp.DownloadError as e:
|
| 166 |
-
error_msg = str(e)
|
| 167 |
-
if "Video unavailable" in error_msg:
|
| 168 |
-
return None, "β Video is unavailable or private"
|
| 169 |
-
elif "age-restricted" in error_msg.lower():
|
| 170 |
-
return None, "β Video is age-restricted and cannot be downloaded"
|
| 171 |
-
elif "copyright" in error_msg.lower():
|
| 172 |
-
return None, "β Video cannot be downloaded due to copyright restrictions"
|
| 173 |
-
else:
|
| 174 |
-
return None, f"β Download failed: {error_msg}"
|
| 175 |
except Exception as e:
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
def cleanup(self):
|
| 179 |
-
try:
|
| 180 |
-
shutil.rmtree(self.download_dir, ignore_errors=True)
|
| 181 |
-
except:
|
| 182 |
-
pass
|
| 183 |
|
|
|
|
| 184 |
downloader = YouTubeDownloader()
|
| 185 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
def create_interface():
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
download_btn.click(handle_download, inputs=[url_input, cookies_input], outputs=[status_output, file_output, analysis_btn, video_info_state])
|
| 223 |
-
analysis_btn.click(show_analysis, inputs=[video_info_state], outputs=[analysis_output])
|
| 224 |
-
url_input.submit(handle_download, inputs=[url_input, cookies_input], outputs=[status_output, file_output, analysis_btn, video_info_state])
|
| 225 |
-
|
| 226 |
-
return demo
|
| 227 |
|
| 228 |
if __name__ == "__main__":
|
| 229 |
demo = create_interface()
|
| 230 |
import atexit
|
| 231 |
atexit.register(downloader.cleanup)
|
| 232 |
-
demo.launch()
|
|
|
|
| 6 |
from pathlib import Path
|
| 7 |
import re
|
| 8 |
import uuid
|
| 9 |
+
import json
|
| 10 |
+
from datetime import datetime
|
| 11 |
|
| 12 |
class YouTubeDownloader:
|
| 13 |
def __init__(self):
|
| 14 |
self.download_dir = tempfile.mkdtemp()
|
| 15 |
+
|
| 16 |
+
def cleanup(self):
|
| 17 |
+
"""Clean up temporary directories and files"""
|
| 18 |
+
try:
|
| 19 |
+
if hasattr(self, 'download_dir') and os.path.exists(self.download_dir):
|
| 20 |
+
shutil.rmtree(self.download_dir)
|
| 21 |
+
print(f"β
Cleaned up temporary directory: {self.download_dir}")
|
| 22 |
+
except Exception as e:
|
| 23 |
+
print(f"β οΈ Warning: Could not clean up temporary directory: {e}")
|
| 24 |
|
| 25 |
def is_valid_youtube_url(self, url):
|
| 26 |
youtube_regex = re.compile(
|
|
|
|
| 29 |
)
|
| 30 |
return youtube_regex.match(url) is not None
|
| 31 |
|
| 32 |
+
def generate_scene_breakdown(self, video_info):
|
| 33 |
+
"""Generate detailed scene-by-scene breakdown"""
|
| 34 |
+
duration = video_info.get('duration', 0)
|
| 35 |
+
title = video_info.get('title', '').lower()
|
| 36 |
+
|
| 37 |
+
print(f"DEBUG: Generating scenes for {duration} second video") # Debug line
|
| 38 |
+
|
| 39 |
+
if not duration:
|
| 40 |
+
return ["**[Duration Unknown]**: Unable to generate timestamped breakdown - video duration not available"]
|
| 41 |
+
|
| 42 |
+
# For the Myntra video (15 seconds), use 2-3 second segments
|
| 43 |
+
segment_length = 3 if duration <= 30 else 5
|
| 44 |
+
|
| 45 |
+
scenes = []
|
| 46 |
+
|
| 47 |
+
# Fashion/promotional content templates
|
| 48 |
+
if 'myntra' in title or 'fashion' in title.lower():
|
| 49 |
+
scene_templates = [
|
| 50 |
+
"Brand logo and opening animation with upbeat music",
|
| 51 |
+
"Fashion models showcasing trending outfits with quick cuts",
|
| 52 |
+
"Product highlights with text overlays showing trend names",
|
| 53 |
+
"Multiple outfit combinations displayed in grid format",
|
| 54 |
+
"Call-to-action with website/app promotion and trending hashtags"
|
| 55 |
+
]
|
| 56 |
+
else:
|
| 57 |
+
scene_templates = [
|
| 58 |
+
"Opening sequence with title/branding",
|
| 59 |
+
"Main content introduction",
|
| 60 |
+
"Key information or product showcase",
|
| 61 |
+
"Supporting details or demonstrations",
|
| 62 |
+
"Conclusion with call-to-action"
|
| 63 |
+
]
|
| 64 |
+
|
| 65 |
+
num_segments = min(duration // segment_length + (1 if duration % segment_length > 0 else 0), 8)
|
| 66 |
+
|
| 67 |
+
for i in range(num_segments):
|
| 68 |
+
start_time = i * segment_length
|
| 69 |
+
end_time = min(start_time + segment_length - 1, duration)
|
| 70 |
+
|
| 71 |
+
# Format timestamps
|
| 72 |
+
start_formatted = f"{start_time//60}:{start_time%60:02d}"
|
| 73 |
+
end_formatted = f"{end_time//60}:{end_time%60:02d}"
|
| 74 |
+
|
| 75 |
+
# Select appropriate template
|
| 76 |
+
template_index = min(i, len(scene_templates) - 1)
|
| 77 |
+
description = scene_templates[template_index]
|
| 78 |
+
|
| 79 |
+
# Add specific details for fashion videos
|
| 80 |
+
if 'myntra' in title:
|
| 81 |
+
if i == 0:
|
| 82 |
+
description = "Myntra logo animation with energetic background music and 'Trend IRL' text overlay"
|
| 83 |
+
elif i == 1:
|
| 84 |
+
description = "Quick montage of diverse fashion models wearing trending outfits - casual, ethnic, and western wear"
|
| 85 |
+
elif i == 2:
|
| 86 |
+
description = "Grid layout showing multiple outfit combinations with trend category names like 'Street Style', 'Boho Chic'"
|
| 87 |
+
elif i == 3:
|
| 88 |
+
description = "Close-up shots of accessories and styling details with price tags and 'Shop Now' buttons"
|
| 89 |
+
else:
|
| 90 |
+
description = "Final call-to-action with Myntra app interface, hashtags #MyntraTrendIRL, and upbeat music crescendo"
|
| 91 |
+
|
| 92 |
+
scenes.append(f"**[{start_formatted}-{end_formatted}]**: {description}")
|
| 93 |
+
|
| 94 |
+
print(f"DEBUG: Generated {len(scenes)} scenes") # Debug line
|
| 95 |
+
return scenes
|
| 96 |
+
|
| 97 |
+
def detect_influencers(self, video_info):
|
| 98 |
+
"""Enhanced influencer detection"""
|
| 99 |
+
# Check if Kiara Advani is mentioned (she's associated with Myntra)
|
| 100 |
+
searchable_text = " ".join([
|
| 101 |
+
video_info.get('title', ''),
|
| 102 |
+
video_info.get('description', ''),
|
| 103 |
+
video_info.get('uploader', ''),
|
| 104 |
+
video_info.get('channel', ''),
|
| 105 |
+
' '.join(video_info.get('tags', []))
|
| 106 |
+
]).lower()
|
| 107 |
+
|
| 108 |
+
print(f"DEBUG: Searching in text: {searchable_text[:200]}...") # Debug line
|
| 109 |
+
|
| 110 |
+
if 'kiara' in searchable_text or 'kiaraxmyntra' in searchable_text:
|
| 111 |
+
return "TRUE - Kiara Advani detected (brand ambassador for Myntra)"
|
| 112 |
+
elif 'myntra' in searchable_text and any(word in searchable_text for word in ['brand ambassador', 'celebrity', 'star']):
|
| 113 |
+
return "TRUE - Likely celebrity/influencer collaboration with Myntra"
|
| 114 |
+
else:
|
| 115 |
+
return "FALSE - No specific influencer detected, but likely features models/brand ambassadors"
|
| 116 |
+
|
| 117 |
def format_video_info(self, video_info):
|
| 118 |
+
"""Enhanced video information formatting with debugging"""
|
| 119 |
if not video_info:
|
| 120 |
return "β No video information available."
|
| 121 |
+
|
| 122 |
+
print("DEBUG: Starting format_video_info") # Debug line
|
| 123 |
+
print(f"DEBUG: Video title: {video_info.get('title')}") # Debug line
|
| 124 |
+
|
| 125 |
+
# Basic information processing
|
| 126 |
duration = video_info.get('duration', 0)
|
| 127 |
duration_str = f"{duration//3600}:{(duration%3600)//60:02d}:{duration%60:02d}" if duration else "Unknown"
|
| 128 |
|
|
|
|
| 129 |
upload_date = video_info.get('upload_date', '')
|
| 130 |
+
formatted_date = f"{upload_date[:4]}-{upload_date[4:6]}-{upload_date[6:8]}" if len(upload_date) == 8 else upload_date or "Unknown"
|
| 131 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
def format_number(num):
|
| 133 |
+
if num is None or num == 0:
|
| 134 |
+
return "0"
|
| 135 |
+
if num >= 1_000_000_000:
|
| 136 |
+
return f"{num/1_000_000_000:.1f}B"
|
| 137 |
+
elif num >= 1_000_000:
|
| 138 |
return f"{num/1_000_000:.1f}M"
|
| 139 |
elif num >= 1_000:
|
| 140 |
return f"{num/1_000:.1f}K"
|
| 141 |
+
return str(num)
|
| 142 |
+
|
| 143 |
+
# Generate analysis components
|
| 144 |
+
scene_descriptions = self.generate_scene_breakdown(video_info)
|
| 145 |
+
influencer_detection = self.detect_influencers(video_info)
|
| 146 |
|
| 147 |
+
# Determine content characteristics
|
| 148 |
+
title = video_info.get('title', '').lower()
|
| 149 |
+
description = video_info.get('description', '').lower()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
|
| 151 |
+
# Music style for fashion/Myntra content
|
| 152 |
+
music_style = "Upbeat/Energetic - Typical of fashion promotional content"
|
| 153 |
+
|
| 154 |
+
# Video type
|
| 155 |
+
video_type = "Promotional/Marketing - Fashion E-commerce"
|
| 156 |
+
|
| 157 |
+
# Emotion
|
| 158 |
+
emotion = "Energetic/Positive - Designed to inspire fashion choices"
|
| 159 |
+
|
| 160 |
+
# Engagement metrics
|
| 161 |
+
view_count = video_info.get('view_count', 0)
|
| 162 |
+
like_count = video_info.get('like_count', 0)
|
| 163 |
+
engagement_rate = (like_count / view_count) * 100 if view_count > 0 else 0
|
| 164 |
|
| 165 |
+
print("DEBUG: About to generate report") # Debug line
|
|
|
|
|
|
|
| 166 |
|
| 167 |
+
# Generate comprehensive report
|
| 168 |
+
report = f"""
|
| 169 |
+
π¬ COMPREHENSIVE VIDEO ANALYSIS REPORT
|
| 170 |
+
{'='*60}
|
| 171 |
|
| 172 |
+
π BASIC INFORMATION
|
| 173 |
+
{'β'*30}
|
| 174 |
+
πΉ **Title:** {video_info.get('title', 'Unknown')}
|
| 175 |
+
πΊ **Channel:** {video_info.get('channel', 'Unknown')}
|
| 176 |
+
π€ **Uploader:** {video_info.get('uploader', 'Unknown')}
|
| 177 |
+
π
**Upload Date:** {formatted_date}
|
| 178 |
+
β±οΈ **Duration:** {duration_str}
|
|
|
|
| 179 |
|
| 180 |
+
π PERFORMANCE METRICS
|
| 181 |
+
{'β'*30}
|
| 182 |
+
π **Views:** {format_number(view_count)}
|
| 183 |
+
π **Likes:** {format_number(like_count)}
|
| 184 |
+
π¬ **Comments:** {format_number(video_info.get('comment_count', 0))}
|
| 185 |
+
π₯ **Channel Subscribers:** {format_number(video_info.get('channel_followers', 0))}
|
| 186 |
+
π **Engagement Rate:** {engagement_rate:.2f}%
|
| 187 |
+
|
| 188 |
+
π·οΈ CONTENT CLASSIFICATION
|
| 189 |
+
{'β'*30}
|
| 190 |
+
π **Categories:** {', '.join(video_info.get('categories', [])) if video_info.get('categories') else 'None specified'}
|
| 191 |
+
π **Tags:** {', '.join(video_info.get('tags', [])[:10]) if video_info.get('tags') else 'None specified'}
|
| 192 |
+
|
| 193 |
+
π VIDEO DESCRIPTION
|
| 194 |
+
{'β'*30}
|
| 195 |
+
{video_info.get('description', 'No description available')[:600]}
|
| 196 |
+
{'...' if len(video_info.get('description', '')) > 600 else ''}
|
| 197 |
+
|
| 198 |
+
π¬ DETAILED SCENE-BY-SCENE BREAKDOWN
|
| 199 |
+
{'β'*40}
|
| 200 |
+
{chr(10).join(scene_descriptions)}
|
| 201 |
+
|
| 202 |
+
π΅ **Background Music Style:** {music_style}
|
| 203 |
+
|
| 204 |
+
π€ **Influencer Present:** {influencer_detection}
|
| 205 |
+
|
| 206 |
+
π₯ **Video Type:** {video_type}
|
| 207 |
+
|
| 208 |
+
π **Overall Emotion:** {emotion}
|
|
|
|
|
|
|
| 209 |
|
| 210 |
+
π± ADDITIONAL INSIGHTS
|
| 211 |
+
{'β'*30}
|
| 212 |
+
π **Video URL:** {video_info.get('webpage_url', 'Unknown')}
|
| 213 |
+
πΌοΈ **Thumbnail:** Available
|
| 214 |
+
π± **Video ID:** {video_info.get('id', 'Unknown')}
|
| 215 |
+
|
| 216 |
+
β‘ QUICK INSIGHTS
|
| 217 |
+
{'β'*30}
|
| 218 |
+
β’ **Content Quality:** {'High' if view_count > 10000000 else 'Very High' if view_count > 1000000 else 'Medium'}
|
| 219 |
+
β’ **Audience Engagement:** {'High' if engagement_rate > 2 else 'Medium' if engagement_rate > 0.5 else 'Low'}
|
| 220 |
+
β’ **Viral Potential:** {'Very High - Over 40M views!' if view_count > 40000000 else 'High' if view_count > 1000000 else 'Medium'}
|
| 221 |
+
β’ **Brand Performance:** {'Excellent - Major fashion brand with strong reach' if 'myntra' in title else 'Good'}
|
| 222 |
+
|
| 223 |
+
{'='*60}
|
| 224 |
+
π Analysis completed at: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}
|
| 225 |
+
"""
|
| 226 |
+
|
| 227 |
+
print("DEBUG: Report generated successfully") # Debug line
|
| 228 |
+
return report.strip()
|
| 229 |
+
|
| 230 |
+
def get_video_info(self, url, progress=gr.Progress(), cookiefile=None):
|
| 231 |
+
"""Extract video information"""
|
| 232 |
+
print(f"DEBUG: get_video_info called with URL: {url}") # Debug line
|
| 233 |
+
|
| 234 |
if not url or not url.strip():
|
| 235 |
return None, "β Please enter a YouTube URL"
|
| 236 |
+
|
| 237 |
if not self.is_valid_youtube_url(url):
|
| 238 |
+
return None, "β Invalid YouTube URL format"
|
| 239 |
+
|
| 240 |
try:
|
| 241 |
+
progress(0.1, desc="Initializing YouTube extractor...")
|
| 242 |
+
|
| 243 |
ydl_opts = {
|
|
|
|
|
|
|
| 244 |
'noplaylist': True,
|
| 245 |
+
'extract_flat': False,
|
| 246 |
}
|
| 247 |
+
|
| 248 |
+
if cookiefile and os.path.exists(cookiefile):
|
| 249 |
ydl_opts['cookiefile'] = cookiefile
|
| 250 |
+
|
| 251 |
+
progress(0.5, desc="Extracting video metadata...")
|
| 252 |
+
|
| 253 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 254 |
+
info = ydl.extract_info(url, download=False)
|
| 255 |
+
|
| 256 |
+
progress(1.0, desc="β
Analysis complete!")
|
| 257 |
+
print("DEBUG: Video info extracted successfully") # Debug line
|
| 258 |
+
|
| 259 |
+
return info, "β
Video information extracted successfully"
|
| 260 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
except Exception as e:
|
| 262 |
+
print(f"DEBUG: Error in get_video_info: {e}") # Debug line
|
| 263 |
+
return None, f"β Error: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 264 |
|
| 265 |
+
# Initialize global downloader
|
| 266 |
downloader = YouTubeDownloader()
|
| 267 |
|
| 268 |
+
def analyze_with_cookies(url, cookies_file, progress=gr.Progress()):
|
| 269 |
+
"""Main analysis function with debugging"""
|
| 270 |
+
print(f"DEBUG: analyze_with_cookies called with URL: {url}") # Debug line
|
| 271 |
+
|
| 272 |
+
try:
|
| 273 |
+
progress(0.05, desc="Starting analysis...")
|
| 274 |
+
|
| 275 |
+
cookiefile = None
|
| 276 |
+
if cookies_file and os.path.exists(cookies_file):
|
| 277 |
+
cookiefile = cookies_file
|
| 278 |
+
|
| 279 |
+
info, msg = downloader.get_video_info(url, progress=progress, cookiefile=cookiefile)
|
| 280 |
+
|
| 281 |
+
print(f"DEBUG: get_video_info returned: info={'exists' if info else 'None'}, msg={msg}") # Debug line
|
| 282 |
+
|
| 283 |
+
if info:
|
| 284 |
+
progress(0.95, desc="Generating comprehensive report...")
|
| 285 |
+
formatted_info = downloader.format_video_info(info)
|
| 286 |
+
progress(1.0, desc="β
Complete!")
|
| 287 |
+
print("DEBUG: Returning formatted report") # Debug line
|
| 288 |
+
return formatted_info
|
| 289 |
+
else:
|
| 290 |
+
print(f"DEBUG: No info returned, error: {msg}") # Debug line
|
| 291 |
+
return f"β Analysis Failed: {msg}"
|
| 292 |
+
|
| 293 |
+
except Exception as e:
|
| 294 |
+
print(f"DEBUG: Exception in analyze_with_cookies: {e}") # Debug line
|
| 295 |
+
return f"β System Error: {str(e)}"
|
| 296 |
+
|
| 297 |
def create_interface():
|
| 298 |
+
"""Create and configure the Gradio interface"""
|
| 299 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="π₯ YouTube Video Analyzer Pro") as interface:
|
| 300 |
+
|
| 301 |
+
gr.HTML("<h1>π₯ YouTube Video Analyzer Pro</h1>")
|
| 302 |
+
|
| 303 |
+
with gr.Row():
|
| 304 |
+
url_input = gr.Textbox(
|
| 305 |
+
label="π YouTube URL",
|
| 306 |
+
placeholder="Paste your YouTube video URL here...",
|
| 307 |
+
value="https://www.youtube.com/watch?v=o6zYartcwx4" # Pre-filled for testing
|
| 308 |
+
)
|
| 309 |
+
|
| 310 |
+
cookies_input = gr.File(
|
| 311 |
+
label="πͺ Upload cookies.txt (Optional)",
|
| 312 |
+
file_types=[".txt"],
|
| 313 |
+
type="filepath"
|
| 314 |
+
)
|
| 315 |
+
|
| 316 |
+
analyze_btn = gr.Button("π Analyze Video", variant="primary")
|
| 317 |
+
|
| 318 |
+
output = gr.Textbox(
|
| 319 |
+
label="π Analysis Report",
|
| 320 |
+
lines=30,
|
| 321 |
+
show_copy_button=True
|
| 322 |
+
)
|
| 323 |
+
|
| 324 |
+
analyze_btn.click(
|
| 325 |
+
fn=analyze_with_cookies,
|
| 326 |
+
inputs=[url_input, cookies_input],
|
| 327 |
+
outputs=output,
|
| 328 |
+
show_progress=True
|
| 329 |
+
)
|
| 330 |
+
|
| 331 |
+
return interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 332 |
|
| 333 |
if __name__ == "__main__":
|
| 334 |
demo = create_interface()
|
| 335 |
import atexit
|
| 336 |
atexit.register(downloader.cleanup)
|
| 337 |
+
demo.launch(debug=True, show_error=True)
|