askrobots / dbbasic-video
Public
Video analysis: download, keyframes, transcripts, thumbnail grids and AI summaries of what's on screen.
analyze_visuals.py · 2.4 KB · 82 lines
| 1 | #!/usr/bin/env python3 |
| 2 | """ |
| 3 | Analyze the thumbnail grid image using OpenAI Vision API |
| 4 | """ |
| 5 | |
| 6 | import base64 |
| 7 | import os |
| 8 | from pathlib import Path |
| 9 | from dotenv import load_dotenv |
| 10 | from openai import OpenAI |
| 11 | |
| 12 | |
| 13 | def encode_image(image_path): |
| 14 | """Encode image to base64.""" |
| 15 | with open(image_path, "rb") as image_file: |
| 16 | return base64.b64encode(image_file.read()).decode('utf-8') |
| 17 | |
| 18 | |
| 19 | def analyze_grid(image_path, output_file="grid_analysis.txt"): |
| 20 | """Analyze the thumbnail grid image.""" |
| 21 | load_dotenv() |
| 22 | |
| 23 | api_key = os.getenv("OPENAI_API_KEY") |
| 24 | if not api_key: |
| 25 | raise ValueError("OPENAI_API_KEY not found in environment or .env file") |
| 26 | |
| 27 | client = OpenAI(api_key=api_key) |
| 28 | |
| 29 | print(f"Analyzing grid image: {image_path}") |
| 30 | base64_image = encode_image(image_path) |
| 31 | |
| 32 | prompt = """This is a grid of 193 video keyframes from scene changes in a video. |
| 33 | |
| 34 | Please analyze this thumbnail sheet and provide: |
| 35 | |
| 36 | 1. **Overall Summary**: What type of video is this? What's the main content? |
| 37 | 2. **Main Themes/Topics**: What are the key themes or topics covered? |
| 38 | 3. **Visual Patterns**: What visual patterns do you notice across the scenes? |
| 39 | 4. **Scene Categories**: Group the scenes into major categories (e.g., interviews, outdoor shots, graphics, etc.) |
| 40 | 5. **Notable Moments**: Any particularly interesting or significant frames? |
| 41 | |
| 42 | Be detailed and thorough in your analysis.""" |
| 43 | |
| 44 | response = client.chat.completions.create( |
| 45 | model="gpt-4o", |
| 46 | messages=[ |
| 47 | { |
| 48 | "role": "user", |
| 49 | "content": [ |
| 50 | {"type": "text", "text": prompt}, |
| 51 | { |
| 52 | "type": "image_url", |
| 53 | "image_url": { |
| 54 | "url": f"data:image/jpeg;base64,{base64_image}" |
| 55 | } |
| 56 | } |
| 57 | ] |
| 58 | } |
| 59 | ], |
| 60 | max_tokens=2000 |
| 61 | ) |
| 62 | |
| 63 | analysis = response.choices[0].message.content |
| 64 | |
| 65 | # Save results |
| 66 | with open(output_file, 'w') as f: |
| 67 | f.write(analysis) |
| 68 | |
| 69 | print(f"\nAnalysis saved to {output_file}\n") |
| 70 | print("=" * 80) |
| 71 | print(analysis) |
| 72 | print("=" * 80) |
| 73 | |
| 74 | return analysis |
| 75 | |
| 76 | |
| 77 | if __name__ == "__main__": |
| 78 | import sys |
| 79 | image_path = sys.argv[1] if len(sys.argv) > 1 else "thumbnail_sheet.jpg" |
| 80 | output_file = sys.argv[2] if len(sys.argv) > 2 else "grid_analysis.txt" |
| 81 | |
| 82 | analyze_grid(image_path, output_file) |