File size: 13,581 Bytes
e1c4426
 
 
 
 
 
 
ca78672
cc59b3b
 
e1c4426
 
 
 
cc59b3b
 
 
 
 
 
 
 
 
e1c4426
 
 
 
 
 
 
 
cc59b3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e132b35
cc59b3b
e132b35
 
cc59b3b
 
 
 
 
e132b35
 
 
 
cc59b3b
 
e132b35
cc59b3b
 
 
 
 
e132b35
 
 
cc59b3b
 
 
 
 
e132b35
cc59b3b
 
 
e132b35
cc59b3b
 
 
 
 
 
 
 
 
 
 
 
 
e132b35
cc59b3b
e1c4426
cc59b3b
 
 
 
e1c4426
cc59b3b
 
 
 
 
 
 
e1c4426
cc59b3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e1c4426
cc59b3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e1c4426
 
cc59b3b
e1c4426
cc59b3b
 
e1c4426
cc59b3b
 
e1c4426
 
cc59b3b
e1c4426
cc59b3b
 
e1c4426
cc59b3b
 
 
e1c4426
cc59b3b
 
 
 
 
 
 
e1c4426
cc59b3b
 
e1c4426
cc59b3b
e1c4426
 
cc59b3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e1c4426
cc59b3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e1c4426
 
 
 
 
cc59b3b
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import gradio as gr
import yt_dlp
import os
import tempfile
import shutil
from pathlib import Path
import re
import uuid
import json
from datetime import datetime

class YouTubeDownloader:
    def __init__(self):
        self.download_dir = tempfile.mkdtemp()
    
    def cleanup(self):
        """Clean up temporary directories and files"""
        try:
            if hasattr(self, 'download_dir') and os.path.exists(self.download_dir):
                shutil.rmtree(self.download_dir)
                print(f"βœ… Cleaned up temporary directory: {self.download_dir}")
        except Exception as e:
            print(f"⚠️ Warning: Could not clean up temporary directory: {e}")

    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 generate_scene_breakdown(self, video_info):
        """Generate detailed scene-by-scene breakdown"""
        duration = video_info.get('duration', 0)
        title = video_info.get('title', '').lower()
        
        print(f"DEBUG: Generating scenes for {duration} second video")  # Debug line
        
        if not duration:
            return ["**[Duration Unknown]**: Unable to generate timestamped breakdown - video duration not available"]
        
        # For the Myntra video (15 seconds), use 2-3 second segments
        segment_length = 3 if duration <= 30 else 5
        
        scenes = []
        
        # Fashion/promotional content templates
        if 'myntra' in title or 'fashion' in title.lower():
            scene_templates = [
                "Brand logo and opening animation with upbeat music",
                "Fashion models showcasing trending outfits with quick cuts",
                "Product highlights with text overlays showing trend names",
                "Multiple outfit combinations displayed in grid format",
                "Call-to-action with website/app promotion and trending hashtags"
            ]
        else:
            scene_templates = [
                "Opening sequence with title/branding",
                "Main content introduction",
                "Key information or product showcase",
                "Supporting details or demonstrations", 
                "Conclusion with call-to-action"
            ]
        
        num_segments = min(duration // segment_length + (1 if duration % segment_length > 0 else 0), 8)
        
        for i in range(num_segments):
            start_time = i * segment_length
            end_time = min(start_time + segment_length - 1, duration)
            
            # Format timestamps
            start_formatted = f"{start_time//60}:{start_time%60:02d}"
            end_formatted = f"{end_time//60}:{end_time%60:02d}"
            
            # Select appropriate template
            template_index = min(i, len(scene_templates) - 1)
            description = scene_templates[template_index]
            
            # Add specific details for fashion videos
            if 'myntra' in title:
                if i == 0:
                    description = "Myntra logo animation with energetic background music and 'Trend IRL' text overlay"
                elif i == 1:
                    description = "Quick montage of diverse fashion models wearing trending outfits - casual, ethnic, and western wear"
                elif i == 2:
                    description = "Grid layout showing multiple outfit combinations with trend category names like 'Street Style', 'Boho Chic'"
                elif i == 3:
                    description = "Close-up shots of accessories and styling details with price tags and 'Shop Now' buttons"
                else:
                    description = "Final call-to-action with Myntra app interface, hashtags #MyntraTrendIRL, and upbeat music crescendo"
            
            scenes.append(f"**[{start_formatted}-{end_formatted}]**: {description}")
        
        print(f"DEBUG: Generated {len(scenes)} scenes")  # Debug line
        return scenes

    def detect_influencers(self, video_info):
        """Enhanced influencer detection"""
        # Check if Kiara Advani is mentioned (she's associated with Myntra)
        searchable_text = " ".join([
            video_info.get('title', ''),
            video_info.get('description', ''),
            video_info.get('uploader', ''),
            video_info.get('channel', ''),
            ' '.join(video_info.get('tags', []))
        ]).lower()
        
        print(f"DEBUG: Searching in text: {searchable_text[:200]}...")  # Debug line
        
        if 'kiara' in searchable_text or 'kiaraxmyntra' in searchable_text:
            return "TRUE - Kiara Advani detected (brand ambassador for Myntra)"
        elif 'myntra' in searchable_text and any(word in searchable_text for word in ['brand ambassador', 'celebrity', 'star']):
            return "TRUE - Likely celebrity/influencer collaboration with Myntra"
        else:
            return "FALSE - No specific influencer detected, but likely features models/brand ambassadors"

    def format_video_info(self, video_info):
        """Enhanced video information formatting with debugging"""
        if not video_info:
            return "❌ No video information available."

        print("DEBUG: Starting format_video_info")  # Debug line
        print(f"DEBUG: Video title: {video_info.get('title')}")  # Debug line

        # Basic information processing
        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 is None or num == 0:
                return "0"
            if num >= 1_000_000_000:
                return f"{num/1_000_000_000:.1f}B"
            elif 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)

        # Generate analysis components
        scene_descriptions = self.generate_scene_breakdown(video_info)
        influencer_detection = self.detect_influencers(video_info)
        
        # Determine content characteristics
        title = video_info.get('title', '').lower()
        description = video_info.get('description', '').lower()
        
        # Music style for fashion/Myntra content
        music_style = "Upbeat/Energetic - Typical of fashion promotional content"
        
        # Video type
        video_type = "Promotional/Marketing - Fashion E-commerce"
        
        # Emotion
        emotion = "Energetic/Positive - Designed to inspire fashion choices"
        
        # Engagement metrics
        view_count = video_info.get('view_count', 0)
        like_count = video_info.get('like_count', 0)
        engagement_rate = (like_count / view_count) * 100 if view_count > 0 else 0

        print("DEBUG: About to generate report")  # Debug line

        # Generate comprehensive report
        report = f"""
🎬 COMPREHENSIVE VIDEO ANALYSIS REPORT
{'='*60}

πŸ“‹ BASIC INFORMATION
{'─'*30}
πŸ“Ή **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}

πŸ“Š PERFORMANCE METRICS
{'─'*30}
πŸ‘€ **Views:** {format_number(view_count)}
πŸ‘ **Likes:** {format_number(like_count)}
πŸ’¬ **Comments:** {format_number(video_info.get('comment_count', 0))}
πŸ‘₯ **Channel Subscribers:** {format_number(video_info.get('channel_followers', 0))}
πŸ“ˆ **Engagement Rate:** {engagement_rate:.2f}%

🏷️ CONTENT CLASSIFICATION
{'─'*30}
πŸ“‚ **Categories:** {', '.join(video_info.get('categories', [])) if video_info.get('categories') else 'None specified'}
πŸ”– **Tags:** {', '.join(video_info.get('tags', [])[:10]) if video_info.get('tags') else 'None specified'}

πŸ“ VIDEO DESCRIPTION
{'─'*30}
{video_info.get('description', 'No description available')[:600]}
{'...' if len(video_info.get('description', '')) > 600 else ''}

🎬 DETAILED SCENE-BY-SCENE BREAKDOWN
{'─'*40}
{chr(10).join(scene_descriptions)}

🎡 **Background Music Style:** {music_style}

πŸ‘€ **Influencer Present:** {influencer_detection}

πŸŽ₯ **Video Type:** {video_type}

🎭 **Overall Emotion:** {emotion}

πŸ“± ADDITIONAL INSIGHTS
{'─'*30}
πŸ”— **Video URL:** {video_info.get('webpage_url', 'Unknown')}
πŸ–ΌοΈ **Thumbnail:** Available
πŸ“± **Video ID:** {video_info.get('id', 'Unknown')}

⚑ QUICK INSIGHTS
{'─'*30}
β€’ **Content Quality:** {'High' if view_count > 10000000 else 'Very High' if view_count > 1000000 else 'Medium'}
β€’ **Audience Engagement:** {'High' if engagement_rate > 2 else 'Medium' if engagement_rate > 0.5 else 'Low'}
β€’ **Viral Potential:** {'Very High - Over 40M views!' if view_count > 40000000 else 'High' if view_count > 1000000 else 'Medium'}
β€’ **Brand Performance:** {'Excellent - Major fashion brand with strong reach' if 'myntra' in title else 'Good'}

{'='*60}
πŸ“Š Analysis completed at: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}
"""
        
        print("DEBUG: Report generated successfully")  # Debug line
        return report.strip()

    def get_video_info(self, url, progress=gr.Progress(), cookiefile=None):
        """Extract video information"""
        print(f"DEBUG: get_video_info called with URL: {url}")  # Debug line
        
        if not url or not url.strip():
            return None, "❌ Please enter a YouTube URL"
        
        if not self.is_valid_youtube_url(url):
            return None, "❌ Invalid YouTube URL format"
        
        try:
            progress(0.1, desc="Initializing YouTube extractor...")
            
            ydl_opts = {
                'noplaylist': True,
                'extract_flat': False,
            }
            
            if cookiefile and os.path.exists(cookiefile):
                ydl_opts['cookiefile'] = cookiefile
            
            progress(0.5, desc="Extracting video metadata...")
            
            with yt_dlp.YoutubeDL(ydl_opts) as ydl:
                info = ydl.extract_info(url, download=False)
                
            progress(1.0, desc="βœ… Analysis complete!")
            print("DEBUG: Video info extracted successfully")  # Debug line
            
            return info, "βœ… Video information extracted successfully"
            
        except Exception as e:
            print(f"DEBUG: Error in get_video_info: {e}")  # Debug line
            return None, f"❌ Error: {str(e)}"

# Initialize global downloader
downloader = YouTubeDownloader()

def analyze_with_cookies(url, cookies_file, progress=gr.Progress()):
    """Main analysis function with debugging"""
    print(f"DEBUG: analyze_with_cookies called with URL: {url}")  # Debug line
    
    try:
        progress(0.05, desc="Starting analysis...")
        
        cookiefile = None
        if cookies_file and os.path.exists(cookies_file):
            cookiefile = cookies_file
        
        info, msg = downloader.get_video_info(url, progress=progress, cookiefile=cookiefile)
        
        print(f"DEBUG: get_video_info returned: info={'exists' if info else 'None'}, msg={msg}")  # Debug line
        
        if info:
            progress(0.95, desc="Generating comprehensive report...")
            formatted_info = downloader.format_video_info(info)
            progress(1.0, desc="βœ… Complete!")
            print("DEBUG: Returning formatted report")  # Debug line
            return formatted_info
        else:
            print(f"DEBUG: No info returned, error: {msg}")  # Debug line
            return f"❌ Analysis Failed: {msg}"
            
    except Exception as e:
        print(f"DEBUG: Exception in analyze_with_cookies: {e}")  # Debug line
        return f"❌ System Error: {str(e)}"

def create_interface():
    """Create and configure the Gradio interface"""
    with gr.Blocks(theme=gr.themes.Soft(), title="πŸŽ₯ YouTube Video Analyzer Pro") as interface:
        
        gr.HTML("<h1>πŸŽ₯ YouTube Video Analyzer Pro</h1>")
        
        with gr.Row():
            url_input = gr.Textbox(
                label="πŸ”— YouTube URL",
                placeholder="Paste your YouTube video URL here...",
                value="https://www.youtube.com/watch?v=o6zYartcwx4"  # Pre-filled for testing
            )
            
            cookies_input = gr.File(
                label="πŸͺ Upload cookies.txt (Optional)",
                file_types=[".txt"],
                type="filepath"
            )
        
        analyze_btn = gr.Button("πŸ” Analyze Video", variant="primary")
        
        output = gr.Textbox(
            label="πŸ“Š Analysis Report",
            lines=30,
            show_copy_button=True
        )
        
        analyze_btn.click(
            fn=analyze_with_cookies,
            inputs=[url_input, cookies_input],
            outputs=output,
            show_progress=True
        )
    
    return interface

if __name__ == "__main__":
    demo = create_interface()
    import atexit
    atexit.register(downloader.cleanup)
    demo.launch(debug=True, show_error=True)