scene detection quality and caching
This commit is contained in:
@@ -6,9 +6,9 @@ import cv2
|
||||
import os
|
||||
from pathlib import Path
|
||||
from typing import List, Tuple, Optional
|
||||
import subprocess
|
||||
import json
|
||||
import logging
|
||||
import re
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -56,7 +56,8 @@ class FrameExtractor:
|
||||
frame_filename = f"frame_{saved_count:05d}_{timestamp:.2f}s.jpg"
|
||||
frame_path = self.output_dir / frame_filename
|
||||
|
||||
cv2.imwrite(str(frame_path), frame)
|
||||
# Use high quality for text readability (95 = high quality JPEG)
|
||||
cv2.imwrite(str(frame_path), frame, [cv2.IMWRITE_JPEG_QUALITY, 95])
|
||||
frames_info.append((str(frame_path), timestamp))
|
||||
saved_count += 1
|
||||
|
||||
@@ -66,41 +67,51 @@ class FrameExtractor:
|
||||
logger.info(f"Extracted {saved_count} frames at {interval_seconds}s intervals")
|
||||
return frames_info
|
||||
|
||||
def extract_scene_changes(self, threshold: float = 30.0) -> List[Tuple[str, float]]:
|
||||
def extract_scene_changes(self, threshold: float = 15.0) -> List[Tuple[str, float]]:
|
||||
"""
|
||||
Extract frames only on scene changes using FFmpeg.
|
||||
More efficient than interval-based extraction.
|
||||
|
||||
Args:
|
||||
threshold: Scene change detection threshold (0-100, lower = more sensitive)
|
||||
Default: 15.0 (good for clean UIs like Zed)
|
||||
Higher values (20-30) for busy UIs like VS Code
|
||||
Lower values (5-10) for very subtle changes
|
||||
|
||||
Returns:
|
||||
List of (frame_path, timestamp) tuples
|
||||
"""
|
||||
try:
|
||||
import ffmpeg
|
||||
except ImportError:
|
||||
raise ImportError("ffmpeg-python not installed. Run: pip install ffmpeg-python")
|
||||
|
||||
video_name = Path(self.video_path).stem
|
||||
output_pattern = self.output_dir / f"{video_name}_%05d.jpg"
|
||||
|
||||
# Use FFmpeg's scene detection filter
|
||||
cmd = [
|
||||
'ffmpeg',
|
||||
'-i', self.video_path,
|
||||
'-vf', f'select=gt(scene\\,{threshold/100}),showinfo',
|
||||
'-vsync', 'vfr',
|
||||
'-frame_pts', '1',
|
||||
str(output_pattern),
|
||||
'-loglevel', 'info'
|
||||
]
|
||||
|
||||
try:
|
||||
result = subprocess.run(cmd, capture_output=True, text=True, check=True)
|
||||
# Use FFmpeg's scene detection filter with high quality output
|
||||
stream = ffmpeg.input(self.video_path)
|
||||
stream = ffmpeg.filter(stream, 'select', f'gt(scene,{threshold/100})')
|
||||
stream = ffmpeg.filter(stream, 'showinfo')
|
||||
stream = ffmpeg.output(
|
||||
stream,
|
||||
str(output_pattern),
|
||||
vsync='vfr',
|
||||
frame_pts=1,
|
||||
**{'q:v': '2'} # High quality JPEG
|
||||
)
|
||||
|
||||
# Run with stderr capture to get showinfo output
|
||||
_, stderr = ffmpeg.run(stream, capture_stderr=True, overwrite_output=True)
|
||||
stderr = stderr.decode('utf-8')
|
||||
|
||||
# Parse FFmpeg output to get frame timestamps from showinfo filter
|
||||
import re
|
||||
frames_info = []
|
||||
|
||||
# Extract timestamps from stderr (showinfo outputs there)
|
||||
timestamp_pattern = r'pts_time:([\d.]+)'
|
||||
timestamps = re.findall(timestamp_pattern, result.stderr)
|
||||
timestamps = re.findall(timestamp_pattern, stderr)
|
||||
|
||||
# Match frames to timestamps
|
||||
frame_files = sorted(self.output_dir.glob(f"{video_name}_*.jpg"))
|
||||
@@ -113,11 +124,15 @@ class FrameExtractor:
|
||||
logger.info(f"Extracted {len(frames_info)} frames at scene changes")
|
||||
return frames_info
|
||||
|
||||
except subprocess.CalledProcessError as e:
|
||||
logger.error(f"FFmpeg error: {e.stderr}")
|
||||
except ffmpeg.Error as e:
|
||||
logger.error(f"FFmpeg error: {e.stderr.decode() if e.stderr else str(e)}")
|
||||
# Fallback to interval extraction
|
||||
logger.warning("Falling back to interval extraction...")
|
||||
return self.extract_by_interval()
|
||||
except Exception as e:
|
||||
logger.error(f"Unexpected error during scene extraction: {e}")
|
||||
logger.warning("Falling back to interval extraction...")
|
||||
return self.extract_by_interval()
|
||||
|
||||
def get_video_duration(self) -> float:
|
||||
"""Get video duration in seconds."""
|
||||
|
||||
Reference in New Issue
Block a user