2026-11-25 · 4 min read
How to Caption Videos in Bun
Bun ships a fast fetch and runs TypeScript directly, which makes it a pleasant place for small API scripts. Captioning a video with caption.sh is three HTTPS calls and no dependencies.
Submit the job
POST the video URL to the API. Documented styling options go in the nested options object.
const KEY = Bun.env.CAPTION_API_KEY!;
const API = "https://api.caption.sh";
async function submit(videoUrl: string) {
const res = await fetch(API + "/v1/videos", {
method: "POST",
headers: {
Authorization: "Bearer " + KEY,
"Content-Type": "application/json",
},
body: JSON.stringify({
source_url: videoUrl,
options: { caps: true, max_words: 4, position: "bottom" },
}),
});
return (await res.json()).job_id;
}One job accepts a video up to 10 minutes long; split longer sources into segments and submit one job per segment.
Poll on a deadline
Jobs move through queued, awaiting_upload, and running, then land on done or error. Poll with a deadline and cover all three exits.
async function waitForJob(jobId: string, budgetMs = 600_000) {
const deadline = Date.now() + budgetMs;
while (Date.now() < deadline) {
const st = await fetch(API + "/v1/jobs/" + jobId, {
headers: { Authorization: "Bearer " + KEY },
}).then((r) => r.json());
if (st.status === "done") return;
if (st.status === "error") throw new Error("caption job failed");
await Bun.sleep(10_000);
}
throw new Error("timed out waiting for " + jobId);
}A 429 response means the transcription throughput ceiling is saturated. Wait and retry; it clears on its own.
Fetch the finished video
The result endpoint answers with a redirect to the file. Use redirect: "manual" and read the Location header yourself.
async function download(jobId: string, out: string) {
const res = await fetch(API + "/v1/jobs/" + jobId + "/result", {
headers: { Authorization: "Bearer " + KEY },
redirect: "manual",
});
const url = res.headers.get("Location")!;
await Bun.write(out, await fetch(url).then((r) => r.arrayBuffer()));
}Results are stored for 24 hours, then deleted, so download the file in the same run. Pricing is prepaid at $0.10 per minute of video.
Where this fits
- One-file scripts you run with bun run, no tsconfig and no node_modules.
- Local tooling that captions a folder of clips on demand.
- Side utilities next to a Bun-served app.
Burned-in captions cannot be toggled, resized, or translated by the viewer. Where accessibility matters, keep the platform's own caption track enabled alongside the burn-in.