2026-11-19 · 6 min read

How to Add Captions to Videos in Rust

A film strip of video production scenes, the middle frame showing the captioned word RUST
This image was generated with AI.

Rust shows up wherever video pipelines get serious: transcode workers, media services, edge tooling. Sooner or later those pipelines need captions burned in, and Rust has no native text-on-video story. You have two realistic paths: shell out to ffmpeg and fight its text renderer, or hand the video to an API that returns a finished, captioned file.

Option 1: ffmpeg drawtext from Rust

The classic approach is spawning ffmpeg with a drawtext filter. It works, but the text side is fragile: drawtext does no real line wrapping, font discovery differs across Linux images, and styling beyond a plain line of text means hand-rolling filter graphs. Fine for a timestamp overlay, painful for word-timed captions.

let output = std::process::Command::new("ffmpeg")
    .args(["-i", "input.mp4", "-vf",
           "drawtext=text='hello':x=(w-text_w)/2:y=h-120",
           "-codec:a", "copy", "out.mp4"])
    .status()?;

Option 2: an API that returns the finished file

The other path treats captioning as a service call: POST the video URL, poll the job, download the result. With caption.sh the body is a source_url plus an options object for the caption look.

let client = reqwest::blocking::Client::new();
let job: serde_json::Value = client
    .post("https://api.caption.sh/v1/videos")
    .bearer_auth(&api_key)
    .json(&serde_json::json!({
        "source_url": video_url,
        "options": { "position": "bottom", "max_words": 4 }
    }))
    .send()?
    .json()?;
let job_id = job["job_id"].as_str().unwrap().to_string();

Poll with three exits: done, error, and a timeout. Jobs move through queued, awaiting_upload, running, done, error.

let start = std::time::Instant::now();
loop {
    let st: serde_json::Value = client
        .get(format!("https://api.caption.sh/v1/jobs/{job_id}"))
        .bearer_auth(&api_key)
        .send()?
        .json()?;
    match st["status"].as_str() {
        Some("done") => break,
        Some("error") => anyhow::bail!("captioning failed"),
        _ if start.elapsed() > std::time::Duration::from_secs(600) => {
            anyhow::bail!("timeout")
        }
        _ => std::thread::sleep(std::time::Duration::from_secs(5)),
    }
}

The result endpoint answers with a redirect to the file, so build a client that does not follow redirects and read the Location header. Results are stored for 24 hours, then deleted, so download promptly.

let dl = reqwest::blocking::Client::builder()
    .redirect(reqwest::redirect::Policy::none())
    .build()?;
let resp = dl
    .get(format!("https://api.caption.sh/v1/jobs/{job_id}/result"))
    .bearer_auth(&api_key)
    .send()?;
let file_url = resp.headers()["Location"].to_str()?.to_string();

Costs and limits to plan around

  • Videos up to 10 minutes per job; clip or segment longer footage first.
  • Pricing is $0.10 per minute of video, prepaid.
  • A 429 means the transcription throughput ceiling, not a per-key limit: wait and retry.
  • Burned-in text cannot be toggled, resized, or translated by the viewer. For accessibility, keep platform caption tracks enabled alongside the burn.

For a Rust service already shipping video, the API path keeps text shaping, timing, and styling out of your filter graphs and behind one POST.

Back to all articles