File size: 13,009 Bytes
0a512d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import google.generativeai as genai
import os
import json
from typing import Dict, List, Optional
import cv2
from datetime import datetime
from dotenv import load_dotenv
import base64
import tempfile
from PIL import Image
import numpy as np

# Load environment variables from .env file
load_dotenv()

class AIMetadataGenerator:
    def __init__(self, api_key: Optional[str] = None):
        """Initialize the AI Metadata Generator with Gemini 2.0 Flash"""
        self.api_key = api_key or os.getenv('GEMINI_API_KEY')
        if not self.api_key:
            raise ValueError("Gemini API key not found. Please set GEMINI_API_KEY in .env file or pass it as parameter.")
        
        genai.configure(api_key=self.api_key) # type: ignore
        self.model = genai.GenerativeModel('gemini-2.0-flash-exp') # type: ignore
    
    def extract_video_frames(self, video_path: str, num_frames: int = 3) -> List[str]:
        """Extract representative frames from video for AI analysis"""
        try:
            cap = cv2.VideoCapture(video_path)
            frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
            frames = []
            
            # Extract frames at regular intervals
            for i in range(num_frames):
                frame_number = int((i + 1) * frame_count / (num_frames + 1))
                cap.set(cv2.CAP_PROP_POS_FRAMES, frame_number)
                ret, frame = cap.read()
                
                if ret:
                    # Convert BGR to RGB
                    frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
                    # Convert to PIL Image
                    pil_image = Image.fromarray(frame_rgb)
                    # Resize for efficient processing
                    pil_image = pil_image.resize((512, 512), Image.Resampling.LANCZOS)
                    frames.append(pil_image)
            
            cap.release()
            return frames
            
        except Exception as e:
            print(f"Error extracting frames: {e}")
            return []
    
    def analyze_video_content(self, video_path: str) -> str:
        """Analyze video content using AI vision to understand what's in the video frames"""
        try:
            # Extract video frames
            frames = self.extract_video_frames(video_path, 3)  # Increased to 3 frames for better coverage
            if not frames:
                return "Unable to analyze video content"
            
            # Analyze frames for text and visual content
            combined_analysis = []
            
            for i, frame in enumerate(frames):
                prompt = f"""

                Analyze this video frame {i+1} and describe what you see in detail. Focus on:

                1. Any visible text in the frame

                2. Main subject/person and their actions

                3. Setting/location 

                4. Key objects or activities visible

                5. Overall mood and style

                

                Extract any text that appears in the image if present.

                Provide detailed analysis of what's shown in the frame.

                """
                
                response = self.model.generate_content([prompt, frame])
                combined_analysis.append(response.text.strip())
            
            # Combine analyses from all frames
            final_prompt = f"""

            Based on the following analyses of different frames from a video, provide a comprehensive understanding of what the video is about:

            

            FRAME ANALYSES:

            {' '.join(combined_analysis)}

            

            Create a concise summary that captures the essence of this video, focusing especially on any text that appears in the frames.

            """
            
            final_response = self.model.generate_content(final_prompt)
            return final_response.text.strip()
            
        except Exception as e:
            print(f"Error analyzing video content: {e}")
            return "Video content analysis unavailable"
    
    def generate_title(self, video_analysis: str) -> str:
        """Generate engaging YouTube shorts title with tranding hashtags to viral a shorts and acc. to video """
        prompt = f"""

        Based on this video analysis, create a catchy YouTube Shorts title:

        

        VIDEO CONTENT: {video_analysis}

        

        Requirements:

        - Make it extremely engaging, click-worthy and optimized for high CTR

        - Keep it under 99 characters including hashtags

        - Include tranding relevant hashtags related to short video directly in the title

        - Focus on the most intriguing aspect of the video, especially any text that appears in the video

        - Use trending language patterns popular in viral shorts

        - Consider using emojis strategically

        - Make it provocative but not clickbait

        

        The title should follow formats that are proven to work for viral shorts.

        Return only the title with hashtags, nothing else.

        """
        
        try:
            response = self.model.generate_content(prompt)
            return response.text.strip().replace('"', '').replace("'", "")
        except Exception as e:
            print(f"Error generating title: {e}")
            return "πŸ”₯ Viral Moment You Won't Believe! #shorts #viral #trending"
    
    def generate_description(self, video_analysis: str) -> str:
        """Generate YouTube shorts description optimized for virality based on text and visual content"""
        prompt = f"""

        Create a YouTube Shorts description based on this video analysis:

        

        VIDEO CONTENT: {video_analysis}

        

        Structure the description exactly like this:

        

         Write exactly 3-4 lines of engaging, high-emotion description that creates curiosity

         Add a line break then write "Keywords:" followed by exactly 40-50 trending keywords line by line

         Add a line break then write "Hashtags:" followed by exactly 60-70 viral hashtags (MUST include #shorts, #viral, #trending #shortvideo #shortsfeed #ytshorts #viralvideo more like this and other relevant ones)

         Add a line break then add this call-to-action: "πŸ‘‰ subscribe for more content like this! πŸ”” Turn on notifications!"

         Add a line break then make copyright disclaimer to protect from strikes And copyright holder.

        

        Make the description extremely engaging and optimized for Shorts algorithm with high-emotion language patterns.

        Focus on keywords and hashtags that are currently trending for short-form viral content.

        If there was any text in the video, incorporate it into the description.

        """
        
        try:
            response = self.model.generate_content(prompt)
            return response.text.strip()
        except Exception as e:
            print(f"Error generating description: {e}")
            return self._fallback_description()
    
    def generate_tags_and_keywords(self, video_analysis: str) -> Dict:
        """Generate optimized tags and keywords specifically for viral shorts"""
        prompt = f"""

        Based on this video analysis, generate optimized tags and keywords for a viral YouTube Short:

        

        VIDEO CONTENT: {video_analysis}

        

        Provide your response as JSON with these exact fields:

        1. "tags": List of 30 tags optimized for YouTube search algorithm (keep each under 30 characters)

        2. "keywords": List of 40 relevant keywords (single words or short phrases) related to the content

        3. "trending_keywords": List of 15 currently trending keywords related to the content

        

        Tags should include general category terms, specific content descriptors, and trending terms.

        Format the response as valid JSON only - no explanation or other text.

        """
        
        try:
            response = self.model.generate_content(prompt)
            result = json.loads(response.text.strip())
            return result
        except Exception as e:
            print(f"Error generating tags and keywords: {e}")
            return {
                "tags": ["shorts", "viral", "trending"],
                "keywords": ["viral video", "trending content", "shorts"],
                "trending_keywords": ["viral shorts", "trending now"]
            }
    
    def _fallback_description(self) -> str:
        """Fallback description when AI generation fails"""
        return """Amazing video content that will keep you entertained!

Watch this incredible moment captured on video.

Perfect for sharing with friends and family.

Don't forget to like and subscribe for more content.

This video showcases some really cool stuff.

You won't believe what happens in this video.

Make sure to watch until the end.

Comment below what you think about this.

Share this video if you enjoyed it.

Thanks for watching our content!



Keywords: viral video, trending content, amazing moments, entertainment, social media, short video, funny clips, must watch, incredible, awesome, cool stuff, viral clips, trending now, popular video, engaging content, shareable, entertaining, video content, social sharing, watch now



Hashtags: #shorts #viral #trending #amazing #entertainment #video #content #socialmedia #funny #cool #awesome #mustwatch #incredible #popular #engaging #shareable #entertaining #videooftheday #trend #viral2024 #shortsvideo #viralshorts #trendingshorts #amazingvideo #viralcontent #shortsfeed #explore #fyp #foryou #viralmoment



⚠️ Copyright Disclaimer: This content is used for educational and entertainment purposes. All rights belong to their respective owners. If you are the owner and want this removed, please contact us."""
    
    def generate_complete_metadata(self, video_path: str, **kwargs) -> Dict:
        """Generate complete metadata package based on video frame analysis"""
        
        print("πŸ€– Analyzing video frames with AI...")
        video_analysis = self.analyze_video_content(video_path)
        print(f"πŸ“Ή Video analysis complete")
        
        print("🎯 Generating viral shorts title with hashtags...")
        title = self.generate_title(video_analysis)
        
        print("πŸ“ Generating description optimized for shorts...")
        description = self.generate_description(video_analysis)
        
        print("🏷️ Generating optimized tags and keywords...")
        tags_keywords = self.generate_tags_and_keywords(video_analysis)
        
        # Extract hashtags from description
        description_lines = description.split('\n')
        hashtags = []
        keywords = []
        
        for line in description_lines:
            if line.startswith('Hashtags:'):
                hashtags_text = line.replace('Hashtags:', '').strip()
                hashtags = [tag.strip() for tag in hashtags_text.split() if tag.startswith('#')]
            elif line.startswith('Keywords:'):
                keywords_text = line.replace('Keywords:', '').strip()
                keywords = [kw.strip() for kw in keywords_text.split(',')]
        
        metadata = {
            "video_analysis": video_analysis,
            "title": title,
            "description": description,
            "tags": tags_keywords.get("tags", []),
            "hashtags": hashtags,
            "keywords": tags_keywords.get("keywords", []),
            "trending_keywords": tags_keywords.get("trending_keywords", []),
            "generated_at": datetime.now().isoformat()
        }
        
        return metadata
    
    def save_metadata(self, metadata: Dict, output_path: str):
        """Save metadata to JSON file"""
        try:
            with open(output_path, 'w', encoding='utf-8') as f:
                json.dump(metadata, f, indent=2, ensure_ascii=False)
            print(f"Metadata saved to: {output_path}")
        except Exception as e:
            print(f"Error saving metadata: {e}")

# Example usage
if __name__ == "__main__":
    # Initialize the generator (API key will be loaded from .env)
    generator = AIMetadataGenerator()
    
    # Example video processing
    video_path = r"c:\Users\DELL\OneDrive\Desktop\youtube automation ai\sample_video.mp4"
    
    # Generate complete metadata
    metadata = generator.generate_complete_metadata(video_path=video_path)
    
    # Print results
    print("Generated Metadata:")
    print("-" * 50)
    print(f"Title: {metadata['title']}")
    print(f"\nDescription:\n{metadata['description']}")
    
    # Save to file
    output_file = r"c:\Users\DELL\OneDrive\Desktop\youtube automation ai\metadata_output.json"
    generator.save_metadata(metadata, output_file)