#!/usr/bin/env bash # Article reel with narration: up to five article links become a narrated, # captioned news reel. Per article: 2 credits for the read and 8 for its two # slides; once: 6 for the script, 9 for the narration, 6 for the music and 2 # per started minute of narration for the word timings; the final is 15 # credits for each started 10 seconds. Five articles and a 50-second reel are # about 148 credits. The Preview is free. # The calls of the page https://api.oneupai.com/docs/recipes/article-reel, in # its order, as one program. # # export ONEUP_API_KEY="oneup_live_…" # your Builder key # bash run.sh "https://…/story-1" "https://…/story-2" # # Needs curl, jq and uuidgen. ONEUP_BASE_URL changes the host (default # https://api.oneupai.com). Every answer is printed as it arrives. set -euo pipefail : "${ONEUP_API_KEY:?Set ONEUP_API_KEY to your API key first}" API=${ONEUP_BASE_URL:-https://api.oneupai.com} if [ $# -lt 1 ] || [ $# -gt 5 ]; then echo "Give one to five article links as arguments" >&2 exit 1 fi HEADERS=$(mktemp) trap 'rm -f "$HEADERS"' EXIT # One API call; the answer is printed and kept in OUT. A refusal stops here. # A 429 carries Retry-After: wait that long, then send the SAME request again # with the SAME Idempotency-Key, which can never charge twice. api() { local method=$1 path=$2 wait shift 2 while :; do OUT=$(curl -sS -D "$HEADERS" -X "$method" "$API$path" \ -H "Authorization: Bearer $ONEUP_API_KEY" \ -H "Content-Type: application/json" "$@") echo "$OUT" if echo "$OUT" | jq -e '.error == "rate_limited"' > /dev/null 2>&1; then wait=$(grep -i '^retry-after:' "$HEADERS" | tr -dc '0-9') sleep "${wait:-5}" continue fi break done if ! echo "$OUT" | jq -e 'type == "object" and (has("error") | not)' > /dev/null; then echo "refused: $method $path" >&2 exit 1 fi } # Starts a queued job; its ticket is left in TICKET. submit() { api POST "/v1/queue/submit/$1" -H "Idempotency-Key: $(uuidgen)" -d "$2" TICKET=$(echo "$OUT" | jq -r .ticket) } # Waits for a queued job (every 2 s for 30 s, then every 5 s) and leaves its # result in OUT. wait_for() { local start=$SECONDS api GET "/v1/queue/status/$1" until echo "$OUT" | jq -e '.status == "COMPLETED" or .status == "FAILED"' > /dev/null; do if [ $((SECONDS - start)) -lt 30 ]; then sleep 2; else sleep 5; fi api GET "/v1/queue/status/$1" done api GET "/v1/queue/result/$1" } # Starts a render, waits for it, and leaves the finished render in OUT. render() { api POST /v1/renders -H "Idempotency-Key: $(uuidgen)" \ -d "$(jq -n --arg p "$PROJECT" --arg v "$VERSION" --arg k "$1" \ '{alias: "oneup/render-timeline", project_id: $p, version_id: $v, kind: $k}')" local id id=$(echo "$OUT" | jq -r .render_id) until echo "$OUT" | jq -e '.status == "COMPLETED" or .status == "FAILED"' > /dev/null; do sleep 5 api GET "/v1/renders/$id" done } # 1. Read each page (2 credits each; a page it cannot use is refunded). ARTICLES='[]' for url in "$@"; do api POST /v1/run/oneup/web-read-article -H "Idempotency-Key: $(uuidgen)" \ -d "$(jq -n --arg u "$url" '{url: $u}')" ARTICLES=$(echo "$ARTICLES" | jq -c --argjson a "$(echo "$OUT" | jq -c .output)" '. + [$a]') done # 2. The script, as JSON. The prompt and the context share one limit of # 16,000 characters, counted as JSON (a line break or a quote is two), so # each article gets an even share of what the prompt leaves. SHAPE='{ hook, items: [{ headline, narration, slide_a, slide_b }] }' PROMPT="Write a 50-second news reel as JSON: $SHAPE. One item per article, in the order given. hook is one short line shown before the first item. narration is what the voice says for that item, in one or two short sentences. slide_a and slide_b each describe a picture for it. Answer with one fenced json block." FIT='def fit($max): if (tojson | length) > $max then .[0:((length * 0.9) | floor)] | fit($max) else . end;' # The JSON block of the answer as {script}, or {problem} saying what is wrong. READ_SCRIPT=' .output | ((capture("```(?:json)?\\s*\\n(?[\\s\\S]*?)```") | .j) // .) | (try fromjson catch null) as $s | if ($s | type) != "object" then {problem: "the answer has no JSON block that parses"} elif ($s.hook | type) != "string" or $s.hook == "" then {problem: "hook must be a string"} elif ($s.items | type) != "array" or ($s.items | length) == 0 then {problem: "items must be a list with at least one item"} else ([$s.items | to_entries[] | .key as $i | .value as $item | ("headline", "narration", "slide_a", "slide_b") as $k | select(($item | type) != "object" or ($item[$k] | type) != "string" or $item[$k] == "") | "items[\($i)].\($k) must be a string"] | .[0]) as $p | if $p then {problem: $p} else {script: $s} end end' api POST /v1/run/oneup/text-script -H "Idempotency-Key: $(uuidgen)" \ -d "$(jq -n --arg prompt "$PROMPT" --argjson articles "$ARTICLES" "$FIT"' ((15500 - ($prompt | tojson | length)) / ($articles | length) | floor) as $share | {prompt: $prompt, context: ([$articles[] | "\(.title)\n\(.site)\n\(.text)" | fit($share)] | join("\n\n"))}')" CHECK=$(echo "$OUT" | jq -c "$READ_SCRIPT") if echo "$CHECK" | jq -e 'has("problem")' > /dev/null; then # One repair call quoting the problem, then stop. PROBLEM=$(echo "$CHECK" | jq -r .problem) FIRST=$(echo "$OUT" | jq -r .output) api POST /v1/run/oneup/text-script -H "Idempotency-Key: $(uuidgen)" \ -d "$(jq -n --arg p "Your JSON had this problem: $PROBLEM. Answer again with only the corrected JSON, shaped $SHAPE, in one fenced json block." \ --arg c "$FIRST" "$FIT"' {prompt: $p, context: ($c | fit(12000))}')" CHECK=$(echo "$OUT" | jq -c "$READ_SCRIPT") if echo "$CHECK" | jq -e 'has("problem")' > /dev/null; then echo "the script: $(echo "$CHECK" | jq -r .problem)" >&2 exit 1 fi fi SCRIPT=$(echo "$CHECK" | jq -c .script) # 3. Two slides per item, the narration, the music; then wait for each. SLIDE_TICKETS='[]' COUNT=$(echo "$SCRIPT" | jq '.items | length') for i in $(seq 0 $((COUNT - 1))); do for side in slide_a slide_b; do submit oneup/image-generate-standard "$(echo "$SCRIPT" | jq -c --argjson i "$i" --arg s "$side" \ '{prompt: .items[$i][$s], quality: "medium", image_size: {width: 736, height: 1280}, output_format: "jpeg"}')" SLIDE_TICKETS=$(echo "$SLIDE_TICKETS" | jq -c --arg t "$TICKET" '. + [$t]') done done # The narration, cut at a sentence end under 1,000 characters. NARRATION=$(echo "$SCRIPT" | jq -r ' [.items[].narration] | join(" ") | if length > 1000 then .[0:1000] as $cut | ([($cut | rindex(". ")), ($cut | rindex("! ")), ($cut | rindex("? "))] | map(. // -1) | max) as $end | if $end > 0 then $cut[0:($end + 1)] else $cut end else . end') submit oneup/speech-standard "$(jq -n --arg p "$NARRATION" '{prompt: $p}')" VOICE_TICKET=$TICKET submit oneup/audio-song '{"prompt": "calm newsroom bed, no vocals", "lyrics_prompt": "[Instrumental]"}' MUSIC_TICKET=$TICKET SLIDES='[]' for ticket in $(echo "$SLIDE_TICKETS" | jq -r '.[]'); do wait_for "$ticket" SLIDES=$(echo "$SLIDES" | jq -c --arg id "$(echo "$OUT" | jq -r '.assets[0].asset_id')" '. + [$id]') done wait_for "$VOICE_TICKET" VOICE=$(echo "$OUT" | jq -r '.assets[0].asset_id') VOICE_URL=$(echo "$OUT" | jq -r '.assets[0].url') wait_for "$MUSIC_TICKET" MUSIC=$(echo "$OUT" | jq -r '.assets[0].asset_id') # 4. The word timings of the narration, and its length. submit oneup/speech-transcribe "$(jq -n --arg u "$VOICE_URL" '{audio_url: $u}')" wait_for "$TICKET" ANNOTATION=$(echo "$OUT" | jq -r .annotation.annotation_id) api GET "/v1/assets/$VOICE/url" NARRATION_MS=$(echo "$OUT" | jq .duration_ms) if [ "$NARRATION_MS" = null ]; then echo "the narration's length is not known" >&2 exit 1 fi # 5. The Timeline: a 3-second hook slide, then two segments per item, each # half of that item's share of the narration. The transcript's word times # start at 0 in the narration file; the narration starts at 3 s, so the # caption track's offset moves every word there. TIMELINE=$(jq -n --argjson script "$SCRIPT" --argjson articles "$ARTICLES" \ --argjson slides "$SLIDES" --arg voice "$VOICE" --arg music "$MUSIC" \ --arg annotation "$ANNOTATION" --argjson ms "$NARRATION_MS" ' {op: "kenburns", from: {x: 0.5, y: 0.5, zoom: 1}, to: {x: 0.5, y: 0.5, zoom: 1.1}, ease: "ease_in_out"} as $pushIn | {op: "kenburns", from: {x: 0.55, y: 0.5, zoom: 1.1}, to: {x: 0.45, y: 0.5, zoom: 1.1}, ease: "ease_in_out"} as $pan | (3000 + $ms) as $total | $script.items as $items | ([$items[].narration | length] | add) as $weight | [$items[:-1][] | ($ms * (.narration | length) / $weight | round)] as $heads | ($heads + [$ms - ($heads | add // 0)]) as $lengths | [foreach $lengths[] as $l (3000; . + $l; [. - $l, $l])] as $spans | { v: "1.6", output: {aspect: "9:16", resolution: 1080, fps: 30, container: "mp4"}, segments: ( [{id: "hook", source: {"$asset": $slides[0]}, fit: "cover", duration: 3000, effects: [$pushIn], transition_out: {op: "cut", duration: 0}}] + [range(0; $items | length) as $i | ($spans[$i][1]) as $l | (($l / 2) | floor) as $half | {id: "a\($i + 1)s1", source: {"$asset": $slides[2 * $i]}, fit: "cover", duration: $half, effects: [$pushIn]}, {id: "a\($i + 1)s2", source: {"$asset": $slides[2 * $i + 1]}, fit: "cover", duration: ($l - $half), effects: [$pan]}] | (length - 1) as $last | .[$last].effects += [{op: "fade", from: 1, to: 0, duration: 600}]), overlays: ( [{id: "hook_text", kind: "text", content: $script.hook, style: "headline", position: "center", range: {from: 0, to: 3000}}] + [range(0; $items | length) as $i | {from: $spans[$i][0], to: ($spans[$i][0] + $spans[$i][1])} as $r | {id: "a\($i + 1)_headline", kind: "text", content: $items[$i].headline, style: "headline", position: "top", range: $r}, {id: "a\($i + 1)_source", kind: "text", content: $articles[[$i, ($articles | length) - 1] | min].site, style: "lower_third", position: "bottom", range: $r}]), audio: [ {id: "voiceover", role: "voiceover", source: {"$asset": $voice}, range: {from: 3000, to: $total}}, {id: "bed", role: "music", source: {"$asset": $music}, range: {from: 0, to: $total}, loop: true, gain: -16, fade_in: 600, fade_out: 1000, ducking: {against: "voiceover", amount_db: -12, attack: 120, release: 400}} ], captions: [ {id: "captions", source: {"$annotation": $annotation}, language: "en", highlight: "word_by_word", style: {preset: "caption_bold_outline"}, offset: 3000, range: {from: 3000, to: $total}} ] }') # 6. Save it, a free Preview, the final, then the MP4's link (an hour). api POST /v1/projects -d '{"agent_slug": "oneup/render-timeline"}' PROJECT=$(echo "$OUT" | jq -r .project_id) api PUT "/v1/projects/$PROJECT/timeline" -d "$(jq -n --argjson t "$TIMELINE" '{timeline: $t}')" VERSION=$(echo "$OUT" | jq -r .version_id) render preview render final api GET "/v1/queue/result/$(echo "$OUT" | jq -r .ticket)"