PySceneDetect Documentation

repository·main·Indexed 26 days ago

https://github.com/breakthrough/pyscenedetect

A video cut detection and analysis tool for automatically identifying scene changes. It features a high-level Python API and CLI for splitting videos, saving frames, and benchmarking detectors. Supported detectors include ContentDetector, AdaptiveDetector, ThresholdDetector, HistogramDetector, and HashDetector. The tool supports multiple video backends including OpenCV, PyAV, MoviePy, and Concat, and provides a core library called scenedetect-core.

Tokens
14.2K
Snippets
35
Records
117
Agent score
89%

What's inside PySceneDetect

  1. Overview of PySceneDetect usage modes

    main

    PySceneDetect provides two primary ways to interact with the tool:

    1. Command-Line Interface (CLI): Use the scenedetect command for standalone video analysis and processing.
    2. Python API: Import the scenedetect module into your own Python scripts to build custom video analysis workflows.
  2. Explore PySceneDetect in academic literature

    main
    PySceneDetect is frequently used for statistical analysis of video in research. You can find various research articles and papers that either utilize PySceneDetect for analysis or use its implementation as a baseline for proposing new detection algorithms. Examples include work on large-scale video captioning (Panda-70M), esports video description (LoL-V2T), and automatic video segmentation.
  3. Use available video backends in PySceneDetect

    main
    PySceneDetect supports multiple video backends for processing video files. Depending on your installed dependencies, you can use OpenCV, PyAV, MoviePy, or Concat backends. These backends are located in the scenedetect.backends module. Choosing the right backend depends on your performance requirements and which libraries you have installed in your environment.
  4. Understand PySceneDetect detector accuracy and benchmarks

    main

    PySceneDetect detectors are benchmarked against public shot-boundary-detection corpora using the TRECVID-SBD convention. This involves greedy 1-to-1 nearest-neighbor matching for hard cuts (with configurable frame tolerance) and point-in-interval matching for fades.

    Key datasets used for benchmarking include:

    • BBC Planet Earth: Long-form broadcast episodes (hard cuts only).
    • AutoShot: Short-form web/user-generated clips (hard cuts only).
    • ClipShots: Short web clips containing both hard cuts and typed gradual transitions.

    For detailed raw results and the benchmark harness, refer to the benchmark/ directory in the repository.

  5. Quickstart: Detect scenes in a video

    main

    Use the high-level scenedetect.detect() function to quickly analyze a video using a specific detector. This returns a list of FrameTimecode pairs representing the start and end of each detected scene. You can pass show_progress=True to display a progress bar.

    Common detectors include:

    • ContentDetector: Detects fast cuts using weighted average of HSV changes.
    • AdaptiveDetector: Finds fast cuts using rolling average of HSL changes.
    • ThresholdDetector: Finds fades in/out using average pixel intensity changes in RGB.
    • HistogramDetector: Finds fast cuts using HSV histogram changes.
    • HashDetector: Finds fast cuts using perceptual image hashing.
    from scenedetect import detect, ContentDetector
    path = "video.mp4"
    scenes = detect(path, ContentDetector())
    for (scene_start, scene_end) in scenes:
        print(f"{scene_start}-{scene_end}")
  6. Quickstart with PySceneDetect CLI

    main

    Use the scenedetect command to perform common tasks like splitting video, saving images, or skipping video segments.

    • Split video on fast cuts: Uses ffmpeg to split the input.
    • Save scene images: Saves frames from each detected cut.
    • Skip video duration: Skips a specified amount of time at the start of the video.
  7. Use the Perceptual Hash Detector (detect-hash)

    main

    The perceptual hash detector (detect-hash) calculates a hash for each frame (using the phash algorithm from the imagehash library) and compares it to the previous frame. If the hashes differ beyond a defined threshold, a scene change is recorded.

    Important notes:

    • Efficiency: This is generally more computationally efficient than detect-content or detect-adaptive.
    • Color Insensitivity: The algorithm converts frames to grayscale, meaning it is insensitive to color changes if the brightness remains constant.
  8. Optimize detection using statistics files

    main

    If default parameters do not work, generate a statistics file using the -s or --stats flag. This allows you to analyze frame data to find the optimal threshold.

    • For detect-content / detect-adaptive: Examine the content_val column in the stats file. Look for peaks that correspond to known scene changes.
    • For detect-threshold: Examine the delta_rgb column (average pixel intensity). The threshold should be set so that scene changes fall under the threshold value.
  9. Optimize detector parameters based on benchmark results

    main

    Benchmark results suggest that optimal parameters vary depending on the content type. Long-form broadcast content (like BBC) generally performs better with lower thresholds than short web clips (like ClipShots).

    Below are the best-performing parameters found during mean hard-cut F1 sweeps across all datasets:

    DetectorBest mean F1Best parameters
    AdaptiveDetector76.3adaptive_threshold=3.5, window_width=3, min_scene_len=0.6s
    ContentDetector73.4threshold=31, min_scene_len=0.6s
    HashDetector69.8threshold=0.35, size=8
    HistogramDetector66.3threshold=0.20, bins=128

    Note: The v0.7 defaults may differ from these optimal values.

  10. Install PySceneDetect via Docker

    main

    Official container images are available at ghcr.io/breakthrough/pyscenedetect. The image includes the full CLI, optional backends (PyAV, MoviePy), and external tools for video splitting (ffmpeg, mkvmerge).

    To pull the image and verify the installation:

    docker pull ghcr.io/breakthrough/pyscenedetect
    docker run --rm ghcr.io/breakthrough/pyscenedetect version

    To process videos, mount the local directory containing your videos into the container. The image runs as a non-root user, so output files will have regular permissions.

    Tags:

    • latest (default): Most recent recommended build.
    • main: Tracks the development branch.
    • 0.7.1 (or other version numbers): Specific releases.
    # Pull and run version check
    docker pull ghcr.io/breakthrough/pyscenedetect
    docker run --rm ghcr.io/breakthrough/pyscenedetect version
    
    # Process videos by mounting a directory
    docker run --rm -v "$(pwd):/files" ghcr.io/breakthrough/pyscenedetect \
        -i /files/video.mp4 detect-adaptive split-video -o /files