PySceneDetect

0.6.7.1 · active · verified Sat Apr 11

PySceneDetect is a Python library and command-line tool for automatic scene detection in videos. It can detect hard cuts, gradual transitions (fades, dissolves), and more using various algorithms. The current version is 0.6.7.1, and the project maintains an active release cadence with frequent minor updates and occasional major versions introducing significant features or breaking changes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize PySceneDetect, add a ContentDetector (the most common type), process a video, and save the detected scenes to a CSV file. Remember to replace 'path/to/your/video.mp4' with a valid video file path. For full functionality and broader video format support, it's recommended to install with optional backends like OpenCV and PyAV (`pip install scenedetect[opencv,pyav]`).

import os
from scenedetect import VideoManager
from scenedetect import SceneManager
from scenedetect.detectors import ContentDetector
from scenedetect.stats_manager import StatsManager
from scenedetect.scene_manager import save_csv

# IMPORTANT: Replace 'path/to/your/video.mp4' with an actual video file.
# For local testing without providing a video, this will create a dummy file,
# but actual scene detection requires a valid video.
VIDEO_PATH = os.environ.get('SCENEDETECT_TEST_VIDEO', 'dummy_video.mp4')
OUTPUT_DIR = '.' # Current directory for output CSV

# Create a dummy file if the video path doesn't exist, to make code runnable
# without a real video for initial syntax checks. 
# A real video is required for meaningful scene detection.
if not os.path.exists(VIDEO_PATH) or not os.path.getsize(VIDEO_PATH) > 0:
    print(f"Warning: Video file '{VIDEO_PATH}' not found or empty. Creating a dummy file.")
    print("For actual scene detection, set SCENEDETECT_TEST_VIDEO or provide a real video.")
    with open(VIDEO_PATH, 'w') as f:
        f.write("This is a dummy file. Replace with a real video for scene detection.")

video_manager = VideoManager([VIDEO_PATH])
stats_manager = StatsManager()
scene_manager = SceneManager(stats_manager)

# Add ContentDetector (default detector)
scene_manager.add_detector(ContentDetector())

try:
    # Set downscale factor for faster processing (e.g., 1/2 resolution)
    video_manager.set_downscale_factor()

    # Start video processing (loads frames or prepares for frame-by-frame)
    video_manager.start()

    # Perform scene detection
    scene_manager.detect_scenes(frame_source=video_manager)

    # Obtain list of scenes
    scene_list = scene_manager.get_scene_list()
    print(f'Detected {len(scene_list)} scenes.')

    # Example: Save detected scenes to a CSV file
    output_csv_path = os.path.join(OUTPUT_DIR, 'scenes.csv')
    save_csv(output_csv_path, scene_list, output_dir=OUTPUT_DIR) # output_dir argument added for clarity
    print(f'Scene list saved to {output_csv_path}')

except Exception as e:
    print(f"An error occurred during scene detection: {e}")
finally:
    video_manager.release()

view raw JSON →