Scene Detection
Lightweight scene detection using histogram comparison (no AI/ML dependencies).
SceneDetector
Detects scene changes in videos by comparing color histograms of consecutive frames. When the histogram difference exceeds a threshold, a scene boundary is detected.
Basic Usage (In-Memory)
from videopython.base import Video, SceneDetector
video = Video.from_path("video.mp4")
# Create detector with custom settings
detector = SceneDetector(
threshold=0.3, # 0.0-1.0, lower = more sensitive
min_scene_length=0.5 # minimum scene duration in seconds
)
# Detect scenes
scenes = detector.detect(video)
for scene in scenes:
print(f"Scene: {scene.start:.2f}s - {scene.end:.2f}s ({scene.duration:.2f}s)")
print(f" Frames: {scene.start_frame} - {scene.end_frame}")
Parallel Processing (Recommended for Long Videos)
For long videos, use detect_parallel() which processes the video using multiple CPU cores. This provides ~3.5x speedup on 8-core machines.
from videopython.base import SceneDetector
detector = SceneDetector(threshold=0.3, min_scene_length=0.5)
# Process video file directly with parallel workers
scenes = detector.detect_parallel("long_video.mp4", num_workers=8)
# Or let it auto-detect CPU count
scenes = detector.detect_parallel("long_video.mp4")
Memory-Efficient Streaming
For memory-constrained environments, use detect_streaming() which processes frames one at a time with O(1) memory usage.
from videopython.base import SceneDetector
detector = SceneDetector(threshold=0.3, min_scene_length=0.5)
# Stream frames from file - only 2 frames in memory at any time
scenes = detector.detect_streaming("very_long_video.mp4")
Method Comparison
| Method | Memory | Speed | Best For |
|---|---|---|---|
detect(video) |
O(all frames) | Fast | Short videos already in memory |
detect_parallel(path) |
O(workers) | Fastest | Long videos, multi-core systems |
detect_streaming(path) |
O(1) | Slower | Memory-constrained environments |
How It Works
- Converts each frame to HSV color space
- Calculates normalized histograms for Hue, Saturation, and Value channels
- Compares consecutive frames using histogram correlation
- Marks boundaries where difference exceeds threshold
- Merges scenes shorter than
min_scene_length
Parameters
threshold(float, default=0.3): Sensitivity for scene change detection. Range 0.0 to 1.0. Lower values detect more scene changes.min_scene_length(float, default=0.5): Minimum scene duration in seconds. Scenes shorter than this are merged with adjacent scenes.
SceneDetector
Detects scene changes in videos using histogram comparison.
Scene changes are detected by comparing the color histograms of consecutive frames. When the histogram difference exceeds a threshold, a scene boundary is detected.
This is a lightweight implementation using only OpenCV, suitable for the base module.
Example
from videopython.base import Video, SceneDetector video = Video.from_path("video.mp4") detector = SceneDetector(threshold=0.3, min_scene_length=0.5) scenes = detector.detect(video) for scene in scenes: ... print(f"Scene: {scene.start:.2f}s - {scene.end:.2f}s")
Source code in src/videopython/base/scene.py
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 | |
__init__
Initialize the scene detector.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
threshold
|
float
|
Sensitivity for scene change detection (0.0 to 1.0). Lower values detect more scene changes. Default: 0.3 |
0.3
|
min_scene_length
|
float
|
Minimum scene duration in seconds. Scenes shorter than this will be merged with adjacent scenes. Default: 0.5 |
0.5
|
Source code in src/videopython/base/scene.py
detect
Detect scenes in a video.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
video
|
Video
|
Video object to analyze |
required |
Returns:
| Type | Description |
|---|---|
list[SceneDescription]
|
List of SceneDescription objects representing detected scenes, ordered by time. |
list[SceneDescription]
|
Frame descriptions are not populated - use VideoAnalyzer for full analysis. |
Source code in src/videopython/base/scene.py
detect_streaming
detect_streaming(
path: str | Path,
start_second: float | None = None,
end_second: float | None = None,
) -> list[SceneDescription]
Detect scenes by streaming frames from file.
Memory usage is O(1) - only 2 frames in memory at any time. This is suitable for processing very long videos that would not fit in memory when loaded entirely.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Path to video file |
required |
start_second
|
float | None
|
Optional start time for analysis |
None
|
end_second
|
float | None
|
Optional end time for analysis |
None
|
Returns:
| Type | Description |
|---|---|
list[SceneDescription]
|
List of SceneDescription objects representing detected scenes. |
Example
detector = SceneDetector(threshold=0.3) scenes = detector.detect_streaming("long_video.mp4") for scene in scenes: ... print(f"Scene: {scene.start:.2f}s - {scene.end:.2f}s")
Source code in src/videopython/base/scene.py
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 | |
detect_parallel
detect_parallel(
path: str | Path,
num_workers: int | None = None,
start_second: float | None = None,
end_second: float | None = None,
) -> list[SceneDescription]
Detect scenes using parallel processing.
Splits video into segments and processes them in parallel using multiple CPU cores. Most efficient for long videos on multi-core systems.
Memory usage: O(workers * chunk_size) - each worker processes independently.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Path to video file |
required |
num_workers
|
int | None
|
Number of parallel workers (default: CPU count) |
None
|
start_second
|
float | None
|
Optional start time for analysis |
None
|
end_second
|
float | None
|
Optional end time for analysis |
None
|
Returns:
| Type | Description |
|---|---|
list[SceneDescription]
|
List of SceneDescription objects representing detected scenes. |
Example
detector = SceneDetector(threshold=0.3) scenes = detector.detect_parallel("long_video.mp4", num_workers=4) for scene in scenes: ... print(f"Scene: {scene.start:.2f}s - {scene.end:.2f}s")
Source code in src/videopython/base/scene.py
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 | |
detect_from_path
classmethod
detect_from_path(
path: str | Path,
threshold: float = 0.3,
min_scene_length: float = 0.5,
) -> list[SceneDescription]
Convenience method for one-shot streaming scene detection.
Creates a SceneDetector instance and runs streaming detection. Memory usage is O(1), suitable for very long videos.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Path to video file |
required |
threshold
|
float
|
Scene change threshold (0.0-1.0). Lower values detect more scene changes. Default: 0.3 |
0.3
|
min_scene_length
|
float
|
Minimum scene duration in seconds. Default: 0.5 |
0.5
|
Returns:
| Type | Description |
|---|---|
list[SceneDescription]
|
List of SceneDescription objects representing detected scenes. |
Example
scenes = SceneDetector.detect_from_path("video.mp4", threshold=0.3) print(f"Found {len(scenes)} scenes")
Source code in src/videopython/base/scene.py
SceneDescription
Returned by SceneDetector.detect(). Contains timing and frame information for each detected scene.
SceneDescription
dataclass
A self-contained description of a video scene.
A scene is a continuous segment of video where the visual content remains relatively consistent, bounded by scene changes or transitions. This class combines timing information with visual analysis, transcription, and other metadata.
Attributes:
| Name | Type | Description |
|---|---|---|
start |
float
|
Scene start time in seconds |
end |
float
|
Scene end time in seconds |
start_frame |
int
|
Index of the first frame in this scene |
end_frame |
int
|
Index of the last frame in this scene (exclusive) |
frame_descriptions |
list[FrameDescription]
|
List of descriptions for frames sampled from this scene |
transcription |
Transcription | None
|
Optional transcription of speech within this scene |
summary |
str | None
|
Optional LLM-generated summary of the scene |
scene_type |
str | None
|
Optional classification (e.g., "dialogue", "action", "transition") |
detected_entities |
list[str] | None
|
Optional list of entities/objects detected in the scene |
dominant_colors |
list[tuple[int, int, int]] | None
|
Optional dominant colors aggregated across the scene |
audio_events |
list[AudioEvent] | None
|
Optional list of audio events detected in this scene |
avg_motion_magnitude |
float | None
|
Optional average motion magnitude across the scene (0.0-1.0) |
dominant_motion_type |
str | None
|
Optional most common motion type in the scene |
Source code in src/videopython/base/description.py
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 | |
num_frames_described
property
Number of frames that were described in this scene.
get_frame_indices
Get evenly distributed frame indices from this scene.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_samples
|
int
|
Number of frames to sample from the scene |
3
|
Returns:
| Type | Description |
|---|---|
list[int]
|
List of frame indices evenly distributed throughout the scene |
Source code in src/videopython/base/description.py
get_description_summary
Get a summary of all frame descriptions concatenated.
Returns:
| Type | Description |
|---|---|
str
|
Single string with all frame descriptions joined |
Source code in src/videopython/base/description.py
get_transcription_text
Get the full transcription text for this scene.
Returns:
| Type | Description |
|---|---|
str
|
Concatenated transcription text, or empty string if no transcription |