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.
Pull frames from video files at specified intervals for analysis,
thumbnails, or training data.
Problem
You have video files and need to extract frames for:
- Object detection on video content
- Creating thumbnails or previews
- Building training datasets
- Scene analysis and classification
Solution
What’s in this recipe:
- Extract frames at a fixed rate (FPS)
- Extract a specific number of frames
- Extract only keyframes for efficiency
You create a view with a frame_iterator that automatically extracts
frames from each video. New videos are processed without extra code.
Setup
Load videos
Connected to Pixeltable database at: postgresql+psycopg://postgres:@/pixeltable?host=/Users/pjlb/.pixeltable/pgdata
Created directory ‘video_demo’.
<pixeltable.catalog.dir.Dir at 0x1695d2650>
Created table ‘videos’.
Inserting rows into `videos`: 1 rows [00:00, 212.90 rows/s]
Inserted 1 row with 0 errors.
1 row inserted, 2 values computed.
Create a view that extracts 1 frame per second:
Inserting rows into `frames`: 19 rows [00:00, 8687.65 rows/s]
For faster processing, extract only keyframes (I-frames):
Inserting rows into `keyframes`: 7 rows [00:00, 3277.53 rows/s]
Explanation
Extraction options:
Only one of fps, num_frames, or keyframes_only can be specified.
When to use keyframes:
- Quick video scanning and thumbnails
- Initial content classification
- Processing very long videos
Frame metadata:
Each frame includes:
frame: The extracted image
pos: Frame position in the video
pts: Presentation timestamp
See also