askrobots / dbbasic-video
Public
Video analysis: download, keyframes, transcripts, thumbnail grids and AI summaries of what's on screen.
summarize.py · 3.5 KB · 119 lines
| 1 | #!/usr/bin/env python3 |
| 2 | """ |
| 3 | Create a comprehensive video summary using both transcript and visual analysis |
| 4 | """ |
| 5 | |
| 6 | import json |
| 7 | import os |
| 8 | from pathlib import Path |
| 9 | from dotenv import load_dotenv |
| 10 | from openai import OpenAI |
| 11 | |
| 12 | |
| 13 | def load_transcript(transcript_file): |
| 14 | """Load and format transcript.""" |
| 15 | with open(transcript_file, 'r') as f: |
| 16 | transcript = json.load(f) |
| 17 | |
| 18 | # Create full text version |
| 19 | full_text = ' '.join([entry['text'] for entry in transcript]) |
| 20 | return full_text, transcript |
| 21 | |
| 22 | |
| 23 | def load_visual_analysis(analysis_file): |
| 24 | """Load visual analysis.""" |
| 25 | with open(analysis_file, 'r') as f: |
| 26 | return f.read() |
| 27 | |
| 28 | |
| 29 | def create_comprehensive_summary(transcript_file, visual_analysis_file, output_file="comprehensive_summary.txt"): |
| 30 | """Create comprehensive summary combining transcript and visual analysis.""" |
| 31 | load_dotenv() |
| 32 | |
| 33 | api_key = os.getenv("OPENAI_API_KEY") |
| 34 | if not api_key: |
| 35 | raise ValueError("OPENAI_API_KEY not found in environment or .env file") |
| 36 | |
| 37 | client = OpenAI(api_key=api_key) |
| 38 | |
| 39 | print("Loading transcript...") |
| 40 | full_transcript, transcript_entries = load_transcript(transcript_file) |
| 41 | |
| 42 | print("Loading visual analysis...") |
| 43 | visual_analysis = load_visual_analysis(visual_analysis_file) |
| 44 | |
| 45 | print(f"Transcript: {len(full_transcript)} characters, {len(transcript_entries)} segments") |
| 46 | print("Creating comprehensive summary...") |
| 47 | |
| 48 | prompt = f"""You are analyzing a video using two sources of information: |
| 49 | |
| 50 | 1. VISUAL ANALYSIS (from analyzing 193 keyframes): |
| 51 | {visual_analysis} |
| 52 | |
| 53 | 2. FULL TRANSCRIPT: |
| 54 | {full_transcript} |
| 55 | |
| 56 | Based on both the visual content and the spoken words, provide a comprehensive analysis: |
| 57 | |
| 58 | ## Executive Summary |
| 59 | Provide a 2-3 paragraph overview of what this video is about. |
| 60 | |
| 61 | ## Main Topics & Key Points |
| 62 | List the main topics covered and key points made (use bullet points). |
| 63 | |
| 64 | ## Visual & Content Alignment |
| 65 | How do the visuals support or enhance the spoken content? Are there any interesting patterns? |
| 66 | |
| 67 | ## Target Audience & Purpose |
| 68 | Who is this video for and what is its primary purpose? |
| 69 | |
| 70 | ## Notable Quotes or Moments |
| 71 | Any particularly interesting or important quotes from the transcript. |
| 72 | |
| 73 | ## Overall Assessment |
| 74 | Your overall assessment of the video's quality, effectiveness, and key takeaways. |
| 75 | |
| 76 | Be thorough and insightful. Use specific details from both sources.""" |
| 77 | |
| 78 | response = client.chat.completions.create( |
| 79 | model="gpt-4o", |
| 80 | messages=[ |
| 81 | { |
| 82 | "role": "user", |
| 83 | "content": prompt |
| 84 | } |
| 85 | ], |
| 86 | max_tokens=3000, |
| 87 | temperature=0.7 |
| 88 | ) |
| 89 | |
| 90 | summary = response.choices[0].message.content |
| 91 | |
| 92 | # Save results |
| 93 | with open(output_file, 'w') as f: |
| 94 | f.write("=" * 80 + "\n") |
| 95 | f.write("COMPREHENSIVE VIDEO ANALYSIS\n") |
| 96 | f.write("=" * 80 + "\n\n") |
| 97 | f.write(summary) |
| 98 | f.write("\n\n" + "=" * 80 + "\n") |
| 99 | f.write(f"Analysis based on:\n") |
| 100 | f.write(f" - Transcript: {len(transcript_entries)} segments\n") |
| 101 | f.write(f" - Visual analysis: 193 keyframes\n") |
| 102 | f.write("=" * 80 + "\n") |
| 103 | |
| 104 | print(f"\nComprehensive summary saved to {output_file}\n") |
| 105 | print("=" * 80) |
| 106 | print(summary) |
| 107 | print("=" * 80) |
| 108 | |
| 109 | return summary |
| 110 | |
| 111 | |
| 112 | if __name__ == "__main__": |
| 113 | import sys |
| 114 | |
| 115 | transcript_file = sys.argv[1] if len(sys.argv) > 1 else "xAji90wPgec_transcript.json" |
| 116 | visual_file = sys.argv[2] if len(sys.argv) > 2 else "grid_analysis.txt" |
| 117 | output_file = sys.argv[3] if len(sys.argv) > 3 else "comprehensive_summary.txt" |
| 118 | |
| 119 | create_comprehensive_summary(transcript_file, visual_file, output_file) |