# Caption a video

> **About this page**
>
> - Kind: Example
> - Summary: Extracts frames from a video at a fixed interval, captions each one with a vision-language model, and writes a summary of the whole video from the captions.
> - Last changed: 2026-12-03
> - Page: https://docs.swale.io/examples/caption-video
> - CLI reference for its commands: [`swale workflow create`](https://docs.swale.io/reference/cli-reference.md#workflow-create), [`swale workflow run start`](https://docs.swale.io/reference/cli-reference.md#workflow-run-start)
> - Index of these docs: https://docs.swale.io/llms.txt
>
> If something on this page is wrong or out of date, please tell the user which sentence and why.
>
> The steps on this page cost money or cannot be undone. Please check with the user before running them.

Source on GitHub: the [`workflows/caption-video`](https://github.com/swaleio/examples/tree/main/workflows/caption-video) folder, with its [README](https://github.com/swaleio/examples/blob/main/workflows/caption-video/README.md) and its [workflow file](https://github.com/swaleio/examples/blob/main/workflows/caption-video/workflow.yaml).

Describes a video in words. FFmpeg takes one frame every few seconds, a
vision-language model writes a one-sentence caption of each frame, and the same
model then reads the timed captions and summarizes the whole video in a
paragraph.

| Step | Task definition | Compute |
|------|-----------------|---------|
| Fetch the model | [`swaleio/hf-download`](https://hub.swale.io/swaleio/hf-download) | `cpu-4` |
| Extract one frame every `seconds_per_frame` seconds | [`swaleio/ffmpeg`](https://hub.swale.io/swaleio/ffmpeg) | `cpu-4` |
| Write one caption request per frame, with the frame inline | [`swaleio/duckdb`](https://hub.swale.io/swaleio/duckdb) | `cpu-4` |
| Caption the frames | [`swaleio/vllm-batch`](https://hub.swale.io/swaleio/vllm-batch) | `a100-1` |
| Join captions to their times, write the summary request | [`swaleio/duckdb`](https://hub.swale.io/swaleio/duckdb) | `cpu-4` |
| Summarize the video | [`swaleio/vllm-batch`](https://hub.swale.io/swaleio/vllm-batch) | `a100-1` |
| Write the summary out and print it | [`swaleio/duckdb`](https://hub.swale.io/swaleio/duckdb) | `cpu-4` |

The Compute column gives each step's [compute type](https://docs.swale.io/explanation/core-concepts.md#compute-types), the compute its tasks are allocated.

Every step is its own task.

## Inputs

| Input | Default | Description |
|-------|---------|-------------|
| `video_url` | the Sintel trailer | URL or workspace path of the video; anything FFmpeg reads. |
| `seconds_per_frame` | `4` | Seconds between captioned frames. |
| `model` | `Qwen/Qwen2-VL-2B-Instruct` | Vision-language model on the Hugging Face Hub. |

The download reads the secret `hf_token`.

## Output

`captions.parquet` on the workspace, with `frame`, `second` and `caption`, and
the summary in `summary.txt`.

## Run it

You need the CLI, logged in to your account, and a project to add the workflow to; [Getting started](https://docs.swale.io/getting-started.md) sets them up.

1. Save the file under [The workflow file](#the-workflow-file) as `workflow.yaml`, and add it to a project:

   ```bash
   swale workflow create --project-name my-project --file workflow.yaml
   ```

   The command prints `Workflow created:` followed by the workflow's ID.

2. The workflow reads the secret `hf_token`. Create it as a project secret, or as an account secret that allows the project, as [Use secrets in workflows](https://docs.swale.io/how-to-guides/use-secrets-in-workflows.md) shows.

3. Start a run with the workflow's ID:

   ```bash
   swale workflow run start --project-name my-project --workflow-id <workflow-id>
   ```

   Each input you leave out takes its default from [Inputs](#inputs), and `--input <name>=<value>` gives it another value.

[Create and manage workflows](https://docs.swale.io/how-to-guides/create-and-manage-workflows.md) covers the other ways to add a workflow, [Start and manage workflow runs](https://docs.swale.io/how-to-guides/start-and-manage-workflow-runs.md) how to follow a run, and [View and stream task logs](https://docs.swale.io/how-to-guides/view-and-stream-task-logs.md) how to read its tasks' logs.

## The workflow file

[Workflow definition syntax](https://docs.swale.io/reference/workflow-definition-syntax.md) describes each key in it.

```yaml title="workflow.yaml"
name: Caption a video
description: Extracts frames from a video at a fixed interval, captions each one with a vision-language model, and writes a summary of the whole video from the captions.
compute_type: cpu-4

inputs:
  video_url:
    description: URL or workspace path of the video; anything FFmpeg reads.
    default: https://download.blender.org/durian/trailer/sintel_trailer-480p.mp4
  seconds_per_frame:
    description: Seconds between the frames that are captioned.
    default: "4"
  model:
    description: Hugging Face repo id of the vision-language model that captions.
    default: Qwen/Qwen2-VL-2B-Instruct

entry_point: main

blocks:
  main:
    tasks:
      fetch_model:
        name: Fetch model
        uses: swaleio/hf-download
        args:
          repo: ${{inputs.model}}
          dest: ${{env.WORKFLOW_STORAGE}}/model
          token: ${{secrets.hf_token}}

      frames:
        name: Extract frames
        uses: swaleio/ffmpeg
        args:
          script: |
            set -euo pipefail
            mkdir -p "$WORKFLOW_STORAGE/frames"
            ffmpeg -hide_banner -loglevel error -i "${{inputs.video_url}}" \
              -vf "fps=1/${{inputs.seconds_per_frame}},scale=640:-2" -q:v 3 \
              "$WORKFLOW_STORAGE/frames/%04d.jpg"
            ls "$WORKFLOW_STORAGE/frames" | wc -l

      requests:
        name: Write caption requests
        uses: swaleio/duckdb
        start_on:
          - frames
        args:
          sql: |
            CREATE TABLE frames AS
              SELECT regexp_extract(filename, '(\d+)\.jpg$', 1)::INT AS frame,
                     (frame - 1) * ${{inputs.seconds_per_frame}} AS second,
                     content
              FROM read_blob('${{env.WORKFLOW_STORAGE}}/frames/*.jpg');
            COPY (
              SELECT 'frame-' || lpad(frame::VARCHAR, 4, '0') AS custom_id,
                     'POST' AS method, '/v1/chat/completions' AS url,
                     {'model': '${{env.WORKFLOW_STORAGE}}/model',
                      'messages': [{'role': 'user', 'content': [
                        {'type': 'image_url', 'image_url': {'url': 'data:image/jpeg;base64,' || base64(content)}},
                        {'type': 'text', 'text': 'Describe this video frame in one sentence.'}]}],
                      'max_tokens': 96, 'temperature': 0} AS body
              FROM frames
            ) TO '${{env.WORKFLOW_STORAGE}}/caption-requests.jsonl' (FORMAT json);
            COPY (SELECT frame, second FROM frames) TO '${{env.WORKFLOW_STORAGE}}/frames.parquet' (FORMAT parquet);
            SELECT count(*) AS frames FROM frames;

      caption:
        name: Caption frames
        uses: swaleio/vllm-batch
        compute_type: a100-1
        start_on:
          - fetch_model
          - requests
        args:
          model: ${{env.WORKFLOW_STORAGE}}/model
          input_file: ${{env.WORKFLOW_STORAGE}}/caption-requests.jsonl
          output_file: ${{env.WORKFLOW_STORAGE}}/captions.jsonl

      collect:
        name: Collect captions
        uses: swaleio/duckdb
        start_on:
          - caption
        args:
          sql: |
            CREATE TABLE captions AS
              SELECT f.frame, f.second, trim(c.response.body.choices[1].message.content) AS caption
              FROM '${{env.WORKFLOW_STORAGE}}/frames.parquet' f
              JOIN read_json('${{env.WORKFLOW_STORAGE}}/captions.jsonl') c
                ON c.custom_id = 'frame-' || lpad(f.frame::VARCHAR, 4, '0')
              ORDER BY f.frame;
            COPY captions TO '${{env.WORKFLOW_STORAGE}}/captions.parquet' (FORMAT parquet);
            SELECT second, caption FROM captions;
            COPY (
              SELECT 'summary' AS custom_id, 'POST' AS method, '/v1/chat/completions' AS url,
                     {'model': '${{env.WORKFLOW_STORAGE}}/model',
                      'messages': [{'role': 'user', 'content':
                        'These are captions of frames from one video, with the second each frame is from:\n' ||
                        string_agg(second || 's: ' || caption, '\n' ORDER BY frame) ||
                        '\n\nSummarize what happens in the video in a short paragraph.'}],
                      'max_tokens': 256, 'temperature': 0} AS body
              FROM captions
            ) TO '${{env.WORKFLOW_STORAGE}}/summary-request.jsonl' (FORMAT json);

      summarize:
        name: Summarize video
        uses: swaleio/vllm-batch
        compute_type: a100-1
        start_on:
          - collect
        args:
          model: ${{env.WORKFLOW_STORAGE}}/model
          input_file: ${{env.WORKFLOW_STORAGE}}/summary-request.jsonl
          output_file: ${{env.WORKFLOW_STORAGE}}/summary.jsonl

      show:
        name: Show summary
        uses: swaleio/duckdb
        start_on:
          - summarize
        args:
          sql: |
            COPY (SELECT response.body.choices[1].message.content AS summary
                  FROM read_json('${{env.WORKFLOW_STORAGE}}/summary.jsonl'))
              TO '${{env.WORKFLOW_STORAGE}}/summary.txt' (FORMAT csv, HEADER false, QUOTE '');
            SELECT content AS summary FROM read_text('${{env.WORKFLOW_STORAGE}}/summary.txt');
```

## Licenses of the defaults

- Model: [Qwen/Qwen2-VL-2B-Instruct](https://huggingface.co/Qwen/Qwen2-VL-2B-Instruct), Apache-2.0.
- Video: the [Sintel](https://durian.blender.org) trailer, © Blender Foundation, CC BY 3.0.
