askrobots / dbbasic-video
Public
Video analysis: download, keyframes, transcripts, thumbnail grids and AI summaries of what's on screen.
find_thumbnail.py · 3.1 KB · 112 lines
| 1 | #!/usr/bin/env python3 |
| 2 | """ |
| 3 | Find and display best thumbnail candidates |
| 4 | """ |
| 5 | |
| 6 | import argparse |
| 7 | import subprocess |
| 8 | import sys |
| 9 | from pathlib import Path |
| 10 | |
| 11 | |
| 12 | def find_by_timestamp(keyframes_dir, timestamp, window=5): |
| 13 | """Find keyframes near a timestamp.""" |
| 14 | keyframes_path = Path(keyframes_dir) |
| 15 | metadata_file = keyframes_path / "scenes_metadata.txt" |
| 16 | |
| 17 | if not metadata_file.exists(): |
| 18 | print(f"Error: {metadata_file} not found", file=sys.stderr) |
| 19 | return [] |
| 20 | |
| 21 | matches = [] |
| 22 | with open(metadata_file, 'r') as f: |
| 23 | for line in f: |
| 24 | if '@' in line and 'Scene' in line: |
| 25 | parts = line.split('@') |
| 26 | if len(parts) == 2: |
| 27 | time_str = parts[1].strip().replace('s', '') |
| 28 | try: |
| 29 | time = float(time_str) |
| 30 | if abs(time - timestamp) <= window: |
| 31 | filename = parts[0].split(':')[1].strip() |
| 32 | matches.append((filename, time)) |
| 33 | except ValueError: |
| 34 | continue |
| 35 | |
| 36 | return sorted(matches, key=lambda x: abs(x[1] - timestamp)) |
| 37 | |
| 38 | |
| 39 | def open_images(keyframes_dir, filenames): |
| 40 | """Open images in default viewer.""" |
| 41 | keyframes_path = Path(keyframes_dir) |
| 42 | paths = [str(keyframes_path / f) for f in filenames] |
| 43 | |
| 44 | if sys.platform == 'darwin': # macOS |
| 45 | subprocess.run(['open'] + paths) |
| 46 | elif sys.platform == 'linux': |
| 47 | subprocess.run(['xdg-open'] + paths) |
| 48 | else: # Windows |
| 49 | for p in paths: |
| 50 | subprocess.run(['start', p], shell=True) |
| 51 | |
| 52 | |
| 53 | def main(): |
| 54 | parser = argparse.ArgumentParser( |
| 55 | description="Find and display thumbnail candidates" |
| 56 | ) |
| 57 | parser.add_argument( |
| 58 | "keyframes_dir", |
| 59 | help="Directory containing keyframes" |
| 60 | ) |
| 61 | parser.add_argument( |
| 62 | "-t", "--timestamp", |
| 63 | type=float, |
| 64 | help="Find keyframes near this timestamp (seconds)" |
| 65 | ) |
| 66 | parser.add_argument( |
| 67 | "-w", "--window", |
| 68 | type=float, |
| 69 | default=5.0, |
| 70 | help="Time window in seconds (default: 5)" |
| 71 | ) |
| 72 | parser.add_argument( |
| 73 | "-n", "--count", |
| 74 | type=int, |
| 75 | default=5, |
| 76 | help="Number of matches to show (default: 5)" |
| 77 | ) |
| 78 | parser.add_argument( |
| 79 | "--no-open", |
| 80 | action="store_true", |
| 81 | help="Don't open images, just list them" |
| 82 | ) |
| 83 | |
| 84 | args = parser.parse_args() |
| 85 | |
| 86 | if args.timestamp is None: |
| 87 | print("Error: --timestamp is required", file=sys.stderr) |
| 88 | return 1 |
| 89 | |
| 90 | print(f"Searching for keyframes near {args.timestamp}s (±{args.window}s)...") |
| 91 | matches = find_by_timestamp(args.keyframes_dir, args.timestamp, args.window) |
| 92 | |
| 93 | if not matches: |
| 94 | print("No matches found") |
| 95 | return 1 |
| 96 | |
| 97 | matches = matches[:args.count] |
| 98 | |
| 99 | print(f"\nFound {len(matches)} keyframes:") |
| 100 | for filename, time in matches: |
| 101 | print(f" {filename} @ {time:.2f}s (Δ{abs(time-args.timestamp):.2f}s)") |
| 102 | |
| 103 | if not args.no_open: |
| 104 | filenames = [m[0] for m in matches] |
| 105 | open_images(args.keyframes_dir, filenames) |
| 106 | print(f"\nOpened {len(filenames)} images") |
| 107 | |
| 108 | return 0 |
| 109 | |
| 110 | |
| 111 | if __name__ == "__main__": |
| 112 | sys.exit(main()) |