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 create preview thumbnails from video files at specific
timestamps or intervals.
Problem
You have video files that need preview thumbnails for galleries, search
results, or video players. Manually extracting frames doesn’t scale.
Solution
What’s in this recipe:
- Extract thumbnail at a specific timestamp
- Generate multiple thumbnails per video
- Resize thumbnails to standard dimensions
You add computed columns that extract frames from videos. Thumbnails are
generated automatically when you insert new videos.
Setup
%pip install -qU pixeltable
import pixeltable as pxt
from pixeltable.functions import video as video_fn
Load videos
# Create a fresh directory
pxt.drop_dir('thumbnail_demo', force=True)
pxt.create_dir('thumbnail_demo')
Connected to Pixeltable database at: postgresql+psycopg://postgres:@/pixeltable?host=/Users/pjlb/.pixeltable/pgdata
Created directory ‘thumbnail_demo’.
<pixeltable.catalog.dir.Dir at 0x14d3a1990>
# Create table for videos
videos = pxt.create_table('thumbnail_demo.videos', {'video': pxt.Video})
Created table ‘videos’.
# Insert sample videos from public S3 bucket
s3_prefix = 's3://multimedia-commons/'
video_paths = [
'data/videos/mp4/ffe/ffb/ffeffbef41bbc269810b2a1a888de.mp4',
'data/videos/mp4/ffe/feb/ffefebb41485539f964760e6115fbc44.mp4',
]
videos.insert([{'video': s3_prefix + path} for path in video_paths])
Inserting rows into `videos`: 2 rows [00:00, 382.20 rows/s]
Inserted 2 rows with 0 errors.
2 rows inserted, 4 values computed.
# View videos
videos.collect()
Extract a single frame at a specific time (e.g., 1 second into the
video):
# Extract frame at 1 second as thumbnail
videos.add_computed_column(
thumbnail=video_fn.extract_frame(videos.video, timestamp=1.0)
)
Added 2 column values with 0 errors.
2 rows updated, 2 values computed.
# View thumbnails
videos.select(videos.video, videos.thumbnail).collect()
Resize thumbnails
Create standard-sized thumbnails for consistent display:
# Resize thumbnail to 320x180 (16:9 aspect ratio)
videos.add_computed_column(
thumbnail_small=videos.thumbnail.resize((320, 180))
)
Added 2 column values with 0 errors.
2 rows updated, 2 values computed.
# View resized thumbnails with dimensions
videos.select(
videos.thumbnail_small,
videos.thumbnail_small.width,
videos.thumbnail_small.height
).collect()
Multiple thumbnails with FrameIterator
For preview strips or timeline thumbnails, use FrameIterator to extract
multiple frames:
from pixeltable.iterators import FrameIterator
# Create a view with frames extracted at 0.5 fps (one frame every 2 seconds)
frames = pxt.create_view(
'thumbnail_demo.frames',
videos,
iterator=FrameIterator.create(video=videos.video, fps=0.5)
)
Inserting rows into `frames`: 17 rows [00:00, 9736.88 rows/s]
# View extracted frames (multiple per video)
frames.select(frames.frame, frames.pos).head(10)
Explanation
Thumbnail extraction methods:
Common thumbnail sizes:
See also