This documentation page is also available as an interactive notebook. You can launch the notebook in
Kaggle or Colab, or download it for use with an IDE or local Jupyter installation, by clicking one of the
above links.
Automatically find scene cuts, transitions, and fades in video files.
Problem
You have video files and need to identify scene boundaries for:
Solution
What’s in this recipe:
- Detect hard cuts with
scene_detect_content()
- Find fade transitions with
scene_detect_threshold()
- Use adaptive detection with
scene_detect_adaptive()
Three built-in detection methods handle different transition types using
PySceneDetect.
Setup
%pip install -qU pixeltable scenedetect
# Create a fresh directory
pxt.drop_dir('scene_demo', force=True)
pxt.create_dir('scene_demo')
Connected to Pixeltable database at: postgresql+psycopg://postgres:@/pixeltable?host=/Users/pjlb/.pixeltable/pgdata
Created directory ‘scene_demo’.
<pixeltable.catalog.dir.Dir at 0x13f816bd0>
Load sample videos
# Create a video table
videos = pxt.create_table(
'scene_demo.videos',
{'video': pxt.Video, 'title': pxt.String}
)
# Insert sample videos from S3
videos.insert([
{
'video': 's3://multimedia-commons/data/videos/mp4/ffe/ffb/ffeffbef41bbc269810b2a1a888de.mp4',
'title': 'Sample video 1'
}
])
Created table ‘videos’.
Inserting rows into `videos`: 1 rows [00:00, 200.53 rows/s]
Inserted 1 row with 0 errors.
1 row inserted, 3 values computed.
Detect scenes with content-based detection
# Detect scenes using content-based detection (best for hard cuts)
videos.add_computed_column(
scenes_content=videos.video.scene_detect_content(
threshold=27.0, # Lower = more sensitive
min_scene_len=15 # Minimum frames between cuts
)
)
# View detected scenes
videos.select(videos.title, videos.scenes_content).collect()
Added 1 column value with 0 errors.
Detect fade transitions
# Detect fade-to-black/white transitions
videos.add_computed_column(
scenes_fade=videos.video.scene_detect_threshold(
threshold=12.0, # Brightness threshold for fades
min_scene_len=15
)
)
# View fade-detected scenes
videos.select(videos.title, videos.scenes_fade).collect()
Added 1 column value with 0 errors.
Adaptive detection for complex videos
# Adaptive detection adjusts to video content dynamically
videos.add_computed_column(
scenes_adaptive=videos.video.scene_detect_adaptive(
adaptive_threshold=3.0, # Lower = more scenes detected
min_scene_len=15,
fps=2.0 # Analyze at 2 FPS for speed
)
)
# View adaptively-detected scenes
videos.select(videos.title, videos.scenes_adaptive).collect()
Added 1 column value with 0 errors.
Explanation
Detection methods:
Output format:
Each method returns a list of scene dictionaries:
{
'start_time': 5.2, # Scene start in seconds
'start_pts': 156, # Presentation timestamp
'duration': 3.8 # Scene duration in seconds
}
Tuning tips:
See also