2026-11-22 · 4 min read

How to Caption Videos in Elixir

AI-generated hero image: a dark 35mm film strip with the word ELIXIR glowing in magenta purple neon at the center.
This image was generated with AI.

Elixir backends are often the orchestration layer for media pipelines, and captioning fits that role well. The caption.sh API is plain HTTPS, so Req is all you need to submit a job, watch it finish, and pull down the captioned video.

Submit the job

POST the video URL to the API. Documented styling options go in the nested options map.

defmodule Captions do
  @api "https://api.caption.sh"

  def submit(video_url) do
    Req.post!(@api <> "/v1/videos",
      auth: {:bearer, api_key()},
      json: %{
        source_url: video_url,
        options: %{caps: true, position: "bottom", max_lines: 2}
      }
    ).body
    |> Map.fetch!("job_id")
  end

  defp api_key, do: System.fetch_env!("CAPTION_API_KEY")
end

The response carries a job_id. One job accepts a video up to 10 minutes long; split longer sources into segments first.

Poll on a deadline

Jobs move through queued, awaiting_upload, and running, then land on done or error. Poll with a monotonic deadline and cover all three exits.

def wait(job_id, deadline_ms \\ 600_000) do
  deadline = System.monotonic_time(:millisecond) + deadline_ms
  poll(job_id, deadline)
end

defp poll(job_id, deadline) do
  if System.monotonic_time(:millisecond) > deadline,
    do: raise("timed out waiting for job " <> job_id)

  st = Req.get!(@api <> "/v1/jobs/" <> job_id, auth: {:bearer, api_key()}).body

  case st["status"] do
    "done" -> :ok
    "error" -> raise("caption job failed")
    _ -> Process.sleep(10_000); poll(job_id, deadline)
  end
end

In a real app, prefer an Oban job with scheduled retries over a sleeping loop. A 429 response means the transcription throughput ceiling is saturated; back off and retry, it clears on its own.

Fetch the finished video

The result endpoint answers with a redirect to the file. Disable redirect following so you can read the Location header yourself.

def result_url(job_id) do
  %{headers: headers} =
    Req.get!(@api <> "/v1/jobs/" <> job_id <> "/result",
      auth: {:bearer, api_key()}, redirect: false)

  headers |> Map.fetch!("location") |> List.first()
end

Results are stored for 24 hours, then deleted, so persist the file to your own storage in the same job. Pricing is prepaid at $0.10 per minute of video.

Where this fits

  • A Phoenix app that accepts uploads and returns captioned versions.
  • An Oban pipeline that captions every new video overnight.
  • A LiveView admin with a caption button per clip.

Burned-in captions cannot be toggled, resized, or translated by the viewer. Keep the platform's native caption track enabled alongside the burn-in where accessibility matters.

Back to all articles