| PySceneDetect is a Python library for detecting scene changes in videos. It uses various algorithms, including content-aware methods like thresholding, color histogram analysis, and edge detection, to identify scene breaks. It provides tools for analyzing and exporting the detected scenes, and can be integrated into video editing workflows for tasks like creating chapter markers, summarizing videos, or detecting commercials.
To detect scene changes in an MP4 file using Python, you can use the PySceneDetect library. PySceneDetect is specifically designed for detecting scene changes in video files and is relatively easy to set up and use. Installation pip install scenedetect[opencv]
The [opencv] option installs OpenCV as a backend, which is commonly used for video processing tasks. If pip install scenedetect[opencv] doesn’t work, you can try installing PySceneDetect and OpenCV separately: pip install scenedetect
pip install opencv-python
Basic Usage Here's a basic example to detect scene changes: from scenedetect import open_video, SceneManager
from scenedetect.detectors import ContentDetector
# Open the video file
video = open_video('path/to/video.mp4')
# Create a scene manager and add a content detector
scene_manager = SceneManager()
scene_manager.add_detector(ContentDetector())
# Perform scene detection
scene_manager.detect_scenes(video)
# Retrieve the list of detected scenes
scene_list = scene_manager.get_scene_list()
# Print the start and end timecodes of each scene
for i, scene in enumerate(scene_list):
print(f"Scene {i+1}: Start {scene[0].get_timecode()} - End {scene[1].get_timecode()}")
You can adjust the detection threshold to be more or less sensitive by passing a threshold parameter to ContentDetector, such as ContentDetector(threshold=30). You can also set the range before detect_scenes() if you don't need to scan full range # Define the frame range for scene detection (e.g., from frame 5000 to frame 15000).
start_frame = 5000
end_frame = 15000
# Set the duration to the specific range of frames.
video_manager.set_duration(start_time=start_frame / video_manager.frame_rate,
end_time=end_frame / video_manager.frame_rate)
Additional Features PySceneDetect also supports other features, like: - Saving scenes to separate video files.
- Exporting timestamps to text files.
This should give you a solid starting point for detecting scene changes in MP4 files using Python. Saving scenes to separate video files To save the detected scenes as separate video files, you can extend the previous code by using PySceneDetect's split_video function. This function will automatically split the video based on the scenes detected and save each scene as a separate file. Here's the updated code: import os
from scenedetect import open_video, SceneManager
from scenedetect.detectors import ContentDetector
from scenedetect.video_splitter import split_video_ffmpeg
# Path to your video file
video_path = 'path/to/video..mp4'
output_directory = 'output_scenes' # Directory where scenes will be saved
# Open the video file
video = open_video(video_path)
# Create a scene manager and add a content detector
scene_manager = SceneManager()
scene_manager.add_detector(ContentDetector())
# Perform scene detection
scene_manager.detect_scenes(video)
# Get list of detected scenes
scene_list = scene_manager.get_scene_list()
print(f"Detected {len(scene_list)} scenes in {video_path}.")
# Print the start and end timecodes of each scene
for i, scene in enumerate(scene_list):
print(f"Scene {i+1}: Start {scene[0].get_timecode()} - End {scene[1].get_timecode()}")
# Generate a unique filename for each scene
output_file = os.path.join(output_directory, f'{os.path.splitext(os.path.basename(video_path))[0]}-Scene-{i+1}.mp4')
# Split and save the scene
split_video_ffmpeg(
video_path, # Path to the input video file
[(scene[0], scene[1])], # Start and end time for the current scene
output_dir=output_file # Output path for the current scene
)
print(f"Scenes have been saved in the '{output_directory}' directory.")
Above code create each of scences, so it is something uncomfortable, so I tried to find another approach with ffmpeg: import os
import ffmpeg
from scenedetect import open_video, SceneManager
from scenedetect.detectors import ContentDetector
# Path to your video file
video_path = 'snowwhite.mp4'
output_directory = 'output_scenes' # Directory where scenes will be saved
# Open the video file
video = open_video(video_path)
# Create a scene manager and add a content detector
scene_manager = SceneManager()
scene_manager.add_detector(ContentDetector())
# Perform scene detection
scene_manager.detect_scenes(video)
# Get list of detected scenes
scene_list = scene_manager.get_scene_list()
print(f"Detected {len(scene_list)} scenes in {video_path}.")
# Print the start and end timecodes of each scene
for i, scene in enumerate(scene_list):
# Generate a unique filename for each scene
output_file = os.path.join(output_directory, f'{os.path.splitext(os.path.basename(video_path))[0]}-Scene-{i+1:04}.mp4')
print(f"Scene {i+1}: Start {scene[0].get_timecode()} - End {scene[1].get_timecode()} - {output_file}")
# Split and save the scene
ffmpeg.input( video_path, ss=scene[0], to=scene[1]).output(output_file).run(overwrite_output=True)
print(f"Scenes have been saved in the '{output_directory}' directory.")
This function uses ffmpeg-python, which provides an interface to ffmpeg. The function specifies the input video and the time range to trim, then exports the result to the specified output file. Make sure ffmpeg-python is installed: pip install ffmpeg-python
This code will detect scenes in the video and save each scene as a separate MP4 file in the specified directory. |