In addition to being used from the command line, or through the GUI, PySceneDetect can be used in Python directly - allowing easy integration into other applications/scripts, or interactive use through a Python REPL/notebook.
In the code example below, we create a function find_scenes() which will
load a video, detect the scenes, and return a list of tuples containing the
(start, end) timecodes of each detected scene. Note that you can modify
the threshold argument to modify the sensitivity of the ContentDetector.
# Standard PySceneDetect imports:
from scenedetect import VideoManager
from scenedetect import SceneManager
# For content-aware scene detection:
from scenedetect.detectors import ContentDetector
def find_scenes(video_path, threshold=30.0):
# Create our video & scene managers, then add the detector.
video_manager = VideoManager([video_path])
scene_manager = SceneManager()
scene_manager.add_detector(
ContentDetector(threshold=threshold))
# Improve processing speed by downscaling before processing.
video_manager.set_downscale_factor()
# Start the video manager and perform the scene detection.
video_manager.start()
scene_manager.detect_scenes(frame_source=video_manager)
# Each returned scene is a tuple of the (start, end) timecode.
return scene_manager.get_scene_list()To get started, try printing the return value of find_scenes on a small video clip:
scenes = find_scenes('video.mp4')
print(scenes)A more advanced usage example can be found in the API reference manual.
PySceneDetect can be used interactively as well. One way to get familiar with this is to type the above example into a Python REPL line by line, viewing the output as you run through the code and making sure you understand the output/results. In the future, functions may be added to preview the scene boundaries graphically using OpenCV's GUI functionality, to allow interactive use of PySceneDetect from the command-line without launching the full GUI.