Video analysis: download, keyframes, transcripts, thumbnail grids and AI summaries of what's on screen.

0 downloads

dbbasic-video/src/dbbasic_video/analyze_visuals.py
Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-video.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-video.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-video.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 Update README with live PyPI link c32f6df 1 year, 1 month ago History
analyze_visuals.py · 2.4 KB · 82 lines
1#!/usr/bin/env python3
2"""
3Analyze the thumbnail grid image using OpenAI Vision API
4"""
5
6import base64
7import os
8from pathlib import Path
9from dotenv import load_dotenv
10from openai import OpenAI
11
12
13def 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
19def 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
34Please analyze this thumbnail sheet and provide:
35
361. **Overall Summary**: What type of video is this? What's the main content?
372. **Main Themes/Topics**: What are the key themes or topics covered?
383. **Visual Patterns**: What visual patterns do you notice across the scenes?
394. **Scene Categories**: Group the scenes into major categories (e.g., interviews, outdoor shots, graphics, etc.)
405. **Notable Moments**: Any particularly interesting or significant frames?
41
42Be 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
77if __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)