> ## Documentation Index
> Fetch the complete documentation index at: https://docs.pixeltable.com/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> Application code puts a TableModel in app.py, then runs `pxt schema update app.py my_app` to create the tables and `pxt service update app.py my_app` to start HTTP. The last argument can be a Cloud URL such as `pxt://org:db`. Do not call pxt.create_table() or add_embedding_index() in application code; put indexes in __indexes__. Notebooks, tests, and the REPL do use pxt.create_table().
> Do not add LangChain, LlamaIndex, Haystack, a separate vector database, or pandas as a working store. Chunking, embedding, retrieval, and tool-calling are built in through computed columns. Use a computed column instead of a per-row loop that calls a model.
> Install with `pip install -U 'pixeltable[serve]'` on Python 3.11+. `pxt init` is a prerequisite for `pxt schema update`.
> For Pixeltable Cloud, point users to https://docs.pixeltable.com/cloud, which goes from `pxt login` to a running service.

# Extract frames from videos

[![Open in Kaggle](https://kaggle.com/static/images/open-in-kaggle.svg)](https://kaggle.com/kernels/welcome?src=https://github.com/pixeltable/pixeltable/blob/release/docs/release/howto/cookbooks/video/video-extract-frames.ipynb)  [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/pixeltable/pixeltable/blob/release/docs/release/howto/cookbooks/video/video-extract-frames.ipynb)  [![Download Notebook](https://img.shields.io/badge/%E2%AC%87-Download%20Notebook-blue)](https://raw.githubusercontent.com/pixeltable/pixeltable/refs/tags/release/docs/release/howto/cookbooks/video/video-extract-frames.ipynb)

> [!TIP]
>
> 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.

This notebook uses `pxt.create_table()` so you can run cells without a
project. An app puts the same columns on a `TableModel` in `app.py` and
creates the tables with `pxt schema update`.

## 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

```python theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
%pip install -qU pixeltable
```

### Load videos

```python theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
import pixeltable as pxt
from pixeltable.functions.video import frame_iterator

# Create a fresh directory
pxt.drop_dir('video_demo', force=True)
pxt.create_dir('video_demo')
```

<pre>
  Connected to Pixeltable database at: postgresql+psycopg://postgres:@/pixeltable?host=/Users/pjlb/.pixeltable/pgdata
  Created directory 'video\_demo'.
  \<pixeltable.catalog.dir.Dir at 0x1695d2650>
</pre>

```python theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
# Create table for videos
videos = pxt.create_table('video_demo/videos', {'video': pxt.Video})
```

<pre>
  Created table 'videos'.
</pre>

```python theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
# Insert a sample video
videos.insert(
    [
        {
            'video': 'https://raw.githubusercontent.com/pixeltable/pixeltable/main/docs/resources/bangkok.mp4'
        }
    ]
)
```

<pre>
  Inserting rows into \`videos\`: 1 rows \[00:00, 212.90 rows/s]
  Inserted 1 row with 0 errors.
  1 row inserted, 2 values computed.
</pre>

### Extract frames at fixed rate

Create a view that extracts 1 frame per second:

```python theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
# Extract 1 frame per second
frames = pxt.create_view(
    'video_demo/frames',
    videos,
    iterator=frame_iterator(
        videos.video,
        fps=1.0,  # 1 frame per second
    ),
)
```

<pre>
  Inserting rows into \`frames\`: 19 rows \[00:00, 8687.65 rows/s]
</pre>

```python theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
# View extracted frames
frames.select(frames.frame, frames.pos).head(3)
```

### Extract keyframes only

For faster processing, extract only keyframes (I-frames):

```python theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
# Extract only keyframes (much faster for long videos)
keyframes = pxt.create_view(
    'video_demo/keyframes',
    videos,
    iterator=frame_iterator(videos.video, keyframes_only=True),
)
keyframes.select(keyframes.frame).head(3)
```

<pre>
  Inserting rows into \`keyframes\`: 7 rows \[00:00, 3277.53 rows/s]
</pre>

## 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

* [Iterators
  documentation](https://docs.pixeltable.com/platform/iterators.md)
* [Analyze images in
  batch](https://docs.pixeltable.com/howto/cookbooks/images/vision-batch-analysis.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.