Skip to main content

Caption a video

Source on GitHub: the workflows/caption-video folder, with its README and its workflow file.

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.

StepTask definitionCompute
Fetch the modelswaleio/hf-downloadcpu-4
Extract one frame every seconds_per_frame secondsswaleio/ffmpegcpu-4
Write one caption request per frame, with the frame inlineswaleio/duckdbcpu-4
Caption the framesswaleio/vllm-batcha100-1
Join captions to their times, write the summary requestswaleio/duckdbcpu-4
Summarize the videoswaleio/vllm-batcha100-1
Write the summary out and print itswaleio/duckdbcpu-4

The Compute column gives each step's compute type, the compute its tasks are allocated.

Every step is its own task.

Inputs​

InputDefaultDescription
video_urlthe Sintel trailerURL or workspace path of the video; anything FFmpeg reads.
seconds_per_frame4Seconds between captioned frames.
modelQwen/Qwen2-VL-2B-InstructVision-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 sets them up.

  1. Save the file under The workflow file as workflow.yaml, and add it to a project:

    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 shows.

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

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

    Each input you leave out takes its default from Inputs, and --input <name>=<value> gives it another value.

Create and manage workflows covers the other ways to add a workflow, Start and manage workflow runs how to follow a run, and View and stream task logs how to read its tasks' logs.

The workflow file​

Workflow definition syntax describes each key in it.

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​