← Add Captions to Video
Your token

Add Captions to Video API

Reading subtitle files, timing the words, drawing and recording the captioned video, and every download run free in the browser and need no API. The API is for the one metered step: send the words of a video (each with an id, its text and the second it is said), the places a group must end and the most words on screen at once, and get back where each group of words ends and which words to colour. The reply holds word ids and kinds only; draw the captions from your own copy of the words. The video and its sound are never sent.

Base URL: https://api.skillsafe.ai/v1/app-api. Every response is {"data": ...} or {"error": {"code", "message"}}; read error.code and error.message rather than the HTTP status alone.

The task field

This app has one task. Always send "task": "captions".

taskWhat it doesReply
captionsSplits the words into groups of 1 to max_words words that each read as a phrase (ending at every pause, never on a word like "the"), and picks at most one word per group, and at most max_emphasis in all, to colour, each with a kind.One JSON object

Input fields

Every field is a string; facts and limits hold JSON text. All three are required.

FieldMeaning
task"captions"
factsJSON object: seconds (the video's length), words in time order, up to 400, each {id, text, s} with ids w1, w2... and s the second the word is said, must_break_after (ids of words followed by a pause of 0.6 s or more, or ending a subtitle line), total_words and sent.
limits{max_words, max_emphasis, kinds}: max_words is 1 to 4; max_emphasis is about a third of the groups; kinds is ["number","name","key_word","action","contrast","feeling"].

The reply

job.output.output is text holding one JSON object: breaks_after, the ids of the last word of each group in time order, and emphasis, each {"id", "kind"}. The page checks that every id was sent, adds a break at every must_break_after id the reply missed, splits any group longer than max_words, flags a group that ends on a small word, and keeps at most one coloured word per group, none of them a small word, and at most max_emphasis in all; do the same checks in your code.

Worked example

Input (input.json), from the page's "Cooking tip" example, at most 3 words on screen:

{
  "task": "captions",
  "facts": "{\"seconds\":16.5,\"words\":[{\"id\":\"w1\",\"text\":\"Three\",\"s\":0.6},{\"id\":\"w2\",\"text\":\"tips\",\"s\":1.02},{\"id\":\"w3\",\"text\":\"for\",\"s\":1.26},{\"id\":\"w4\",\"text\":\"fluffier\",\"s\":1.51},{\"id\":\"w5\",\"text\":\"pancakes.\",\"s\":2.25},{\"id\":\"w6\",\"text\":\"First,\",\"s\":3.63},{\"id\":\"w7\",\"text\":\"let\",\"s\":4.12},{\"id\":\"w8\",\"text\":\"the\",\"s\":4.43},{\"id\":\"w9\",\"text\":\"batter\",\"s\":4.54},{\"id\":\"w10\",\"text\":\"rest\",\"s\":5.33},{\"id\":\"w11\",\"text\":\"for\",\"s\":5.8},{\"id\":\"w12\",\"text\":\"ten\",\"s\":5.92},{\"id\":\"w13\",\"text\":\"minutes.\",\"s\":6.26},{\"id\":\"w14\",\"text\":\"Second,\",\"s\":7.6},{\"id\":\"w15\",\"text\":\"keep\",\"s\":8.16},{\"id\":\"w16\",\"text\":\"the\",\"s\":8.6},{\"id\":\"w17\",\"text\":\"pan\",\"s\":8.94},{\"id\":\"w18\",\"text\":\"on\",\"s\":9.37},{\"id\":\"w19\",\"text\":\"medium\",\"s\":9.78},{\"id\":\"w20\",\"text\":\"heat.\",\"s\":10.34},{\"id\":\"w21\",\"text\":\"Third,\",\"s\":11.09},{\"id\":\"w22\",\"text\":\"flip\",\"s\":11.57},{\"id\":\"w23\",\"text\":\"only\",\"s\":11.91},{\"id\":\"w24\",\"text\":\"once,\",\"s\":12.54},{\"id\":\"w25\",\"text\":\"when\",\"s\":13.4},{\"id\":\"w26\",\"text\":\"bubbles\",\"s\":13.88},{\"id\":\"w27\",\"text\":\"pop\",\"s\":14.41},{\"id\":\"w28\",\"text\":\"on\",\"s\":14.84},{\"id\":\"w29\",\"text\":\"top.\",\"s\":15.28}],\"must_break_after\":[\"w5\",\"w13\",\"w20\",\"w24\"],\"total_words\":29,\"sent\":29}",
  "limits": "{\"max_words\":3,\"max_emphasis\":4,\"kinds\":[\"number\",\"name\",\"key_word\",\"action\",\"contrast\",\"feeling\"]}"
}

Reply:

{
 "breaks_after": [
  "w2",
  "w5",
  "w6",
  "w9",
  "w10",
  "w13",
  "w14",
  "w17",
  "w20",
  "w21",
  "w24",
  "w27"
 ],
 "emphasis": [
  {
   "id": "w1",
   "kind": "number"
  },
  {
   "id": "w12",
   "kind": "number"
  },
  {
   "id": "w19",
   "kind": "key_word"
  },
  {
   "id": "w24",
   "kind": "number"
  }
 ]
}

Errors

HTTPWhat to do
401The token is missing, expired or a guest token on a run. Get a personal token on the token page.
402The balance is below what the run reserves. Top up at skillsafe.ai/account/billing.
400The body is not a JSON object of the fields above, or a required field is missing or empty.
429Wait and retry with the same Idempotency-Key.

Step 1 — a tiny client helper

One function that sends JSON and unwraps the {data, error} envelope.

BASE="https://api.skillsafe.ai/v1/app-api"
TOKEN="paste-your-token-here"   # from https://add-captions-to-video.skillsafe.ai/tokens.html
api() { # api METHOD PATH [JSON-BODY]
  curl -s -X "$1" "$BASE$2" -H "Content-Type: application/json" \
    ${TOKEN:+-H "Authorization: Bearer $TOKEN"} ${3:+--data "$3"}
}

Step 2 — a token

A guest token is enough for /me and /estimate. A run is metered, so it needs your personal token: sign in on the token page and copy it.

# a guest token (enough for /me and /estimate); a run needs your personal token from /tokens.html
TOKEN=$(curl -s -X POST "$BASE/guest" -H "Content-Type: application/json" -d '{"slug":"add-captions-to-video"}' | jq -r '.data.token')

Step 3 — who am I, and my balance

subject_type is guest or user; credits is the balance (1 credit = $0.0001).

api GET /me | jq '.data | {subject_type, credits}' 

Step 4 — price a run for free

/estimate charges nothing and starts no job. hold_credits is the amount reserved while a run is in flight, not the price; the run is charged what it uses.

# input.json: the run input object - {"task": "captions", "facts": "{\"seconds\":16.5,\"words\":[{\"id\":\"w1\",\"text\":\"Three\",\"s\":0.6},{\"id\":\"w2\",\"text\":\"tips\",\"s\":1.02},{\"id\":\"w3\",\"text\":\"for\",\"s\":1.26},{\"id\":\"w4\",\"text\":\"fluffier\",\"s\":1.51},{\"id\":\"w5\",\"text\":\"pancakes.\",\"s\":2.25},{\"id\":\"w6\",\"text\":\"First,\",\"s\":3.63},{\"id\":\"w7\",\"text\":\"let\",\"s\":4.12},{\"id\":\"w8\",\"text\":\"the\",\"s\":4.43},{\"id\":\"w9\",\"text\":\"batter\",\"s\":4.54},{\"id\":\"w10\",\"text\":\"rest\",\"s\":5.33},{\"id\":\"w11\",\"text\":\"for\",\"s\":5.8},{\"id\":\"w12\",\"text\":\"ten\",\"s\":5.92},{\"id\":\"w13\",\"text\":\"minutes.\",\"s\":6.26},{\"id\":\"w14\",\"text\":\"Second,\",\"s\":7.6},{\"id\":\"w15\",\"text\":\"keep\",\"s\":8.16},{\"id\":\"w16\",\"text\":\"the\",\"s\":8.6},{\"id\":\"w17\",\"text\":\"pan\",\"s\":8.94},{\"id\":\"w18\",\"text\":\"on\",\"s\":9.37},{\"id\":\"w19\",\"text\":\"medium\",\"s\":9.78},{\"id\":\"w20\",\"text\":\"heat.\",\"s\":10.34},{\"id\":\"w21\",\"text\":\"Third,\",\"s\":11.09},{\"id\":\"w22\",\"text\":\"flip\",\"s\":11.57},{\"id\":\"w23\",\"text\":\"only\",\"s\":11.91},{\"id\":\"w24\",\"text\":\"once,\",\"s\":12.54},{\"id\":\"w25\",\"text\":\"when\",\"s\":13.4},{\"id\":\"w26\",\"text\":\"bubbles\",\"s\":13.88},{\"id\":\"w27\",\"text\":\"pop\",\"s\":14.41},{\"id\":\"w28\",\"text\":\"on\",\"s\":14.84},{\"id\":\"w29\",\"text\":\"top.\",\"s\":15.28}],\"must_break_after\":[\"w5\",\"w13\",\"w20\",\"w24\"],\"total_words\":29,\"sent\":29}", "limits": "{\"max_words\":3,\"max_emphasis\":4,\"kinds\":[\"number\",\"name\",\"key_word\",\"action\",\"contrast\",\"feeling\"]}"}
api POST /estimate "$(cat input.json)" | jq '.data | {hold_credits, min_credits, model, model_alias, markup_bps, warnings}' 

Step 5 — run it and wait for the job

Send an Idempotency-Key derived from the input: a retry with the same key is never billed twice.

KEY="add-captions-to-video:captions:$(shasum -a 256 input.json | cut -c1-16):a1"
JOB=$(curl -s -X POST "$BASE/run" -H "Content-Type: application/json" -H "Authorization: Bearer $TOKEN" \
  -H "Idempotency-Key: $KEY" --data @input.json | jq -r '.data.job_id')
while :; do
  J=$(api GET "/jobs/$JOB"); S=$(echo "$J" | jq -r '.data.status')
  case "$S" in succeeded|failed|cancelled) break;; esac; sleep 2
done
echo "$J" | jq -r '.data.output.output' > reply.json   # one JSON object: breaks_after, emphasis
echo "$J" | jq '.data | {charged_credits, truncated}' 

Step 6 — or stream it

/run-stream sends Server-Sent Events while the job runs and the finished job at the end.

# Server-Sent Events: tick/delta events while the job runs, then a final event with the job
curl -N -s -X POST "$BASE/run-stream" -H "Content-Type: application/json" -H "Authorization: Bearer $TOKEN" \
  -H "Idempotency-Key: add-captions-to-video:captions:$(shasum -a 256 input.json | cut -c1-16):a1" --data @input.json