# Y6.4 — `video_overview` generator (docs/RFC_CONTENT_STUDIO.md §3).
#
# Source (a URL, fetched with the http node's SSRF filter, pasted text,
# or notebook chunks) → a scene-by-scene script (llm, json_schema: what
# is said, what is shown) → per scene, in parallel, a picture
# (text_to_image) and its narration (tts) → render_video (ffmpeg,
# sandboxed): each picture held for its narration, joined into one MP4
# with the narration as a soft subtitle track, plus an .srt.
#
# With `video_model` set, the script may mark one or two scenes as moving
# shots; those get a short text_to_video clip (looped for the narration)
# instead of a picture. Clips are billed per second, so this is off by
# default.
#
#   yanshi workflow run examples/generators/video_overview.yml \
#     --input url https://example.org/article \
#     --input image_model <text-to-image model> \
#     --input tts_model <tts model> --input voice <voice> --output-dir out
name: video_overview
description: "A narrated video overview of a source: illustrated scenes, voice-over and subtitles, as MP4"

inputs:
  url:
    description: "Page to present (fetched with SSRF protection); leave empty to use `text`"
    default: ""
  text:
    description: "Source text, used when `url` is empty"
    default: ""
  focus:
    description: "What the video should explain"
    default: "the main ideas, in plain language"
  scene_count:
    description: "Number of scenes (4-10); each is about 10-20 seconds"
    default: 6
  language:
    description: "Language of the narration"
    default: "English"
  style:
    description: "Visual style every picture shares"
    default: "clean flat editorial illustration, soft colours, no text or letters"
  image_model:
    description: "Registry name of the text-to-image model"
    default: "gpt-image-2"
  image_size:
    description: "Picture size the image model is asked for (pictures are letterboxed to the video)"
    default: "1536x1024"
  tts_model:
    description: "Registry name of the text-to-speech model"
    default: "tts-1"
  voice:
    description: "Narrator voice"
    default: "alloy"
  video_model:
    description: "Registry name of a text-to-video model; empty (the default) keeps every scene a still picture"
    default: ""
  video_seconds:
    description: "Length of each generated clip, in seconds (it loops for the narration)"
    default: 5
  burn_subtitles:
    description: "Draw the narration into the picture instead of a subtitle track the viewer toggles (needs ffmpeg with libass)"
    default: false
  chunks:
    description: "Notebook passages to work from instead of url / text: [{chunk_id, source_id, content}]. Citations then point at these ids."
    default: []

nodes:
  - id: options
    type: template
    parameters:
      output_format: json
      template: '{"has_url": {{ url != "" }}}'

  # `url` (the workflow input) reaches the node directly.
  - id: fetch
    type: http
    dependencies: ["options"]
    run_if: "{{ nodes.options.outputs.has_url }}"

  - id: source
    type: template
    input_mapping:
      page: "{{ nodes.fetch.outputs.body }}"
    parameters:
      template: "{% if chunks | length > 0 %}{% for c in chunks %}[{{ c.chunk_id }}] {{ c.content }}\n\n{% endfor %}{% elif page %}{{ page | striptags | truncate(length=24000) }}{% else %}{{ text | truncate(length=24000) }}{% endif %}"

  - id: script_prompt
    type: template
    dependencies: ["source"]
    input_mapping:
      source_text: "{{ nodes.source.outputs.output }}"
    parameters:
      template: |
        Write a short explainer video in {{ language }} about the source
        below, covering {{ focus }}, as {{ scene_count }} scenes. For each
        scene: `narration`, what the narrator says (two or three spoken
        sentences, no stage directions); and `visual`, one sentence
        describing a single still picture that illustrates it (a scene,
        object or diagram-like composition; never text, letters or
        logos). The first scene introduces the subject, the last sums up.
        {% if video_model %}Set `"motion": true` on the one or two scenes
        whose picture most needs movement to be understood (a process, a
        change over time); leave it out everywhere else.
        {% endif %}        Every fact must come from the source; when its passages carry
        [ids], give each scene the chunk_id of its passage.

        SOURCE:
        {{ source_text }}

  - id: script
    type: llm
    dependencies: ["script_prompt"]
    input_mapping:
      prompt: "{{ nodes.script_prompt.outputs.output }}"
    parameters:
      system: "You write scripts for short explainer videos. Answer with JSON only."
      json_schema:
        type: object
        required: [title, scenes]
        properties:
          title: { type: string }
          scenes:
            type: array
            minItems: 1
            maxItems: 12
            items:
              type: object
              required: [narration, visual]
              properties:
                narration: { type: string }
                visual: { type: string }
                motion: { type: boolean }
                chunk_id: { type: string }

  # One Map item per scene, carrying what its picture and voice need (a
  # Map body sees only `item`, not the workflow inputs).
  - id: shots
    type: template
    dependencies: ["script"]
    input_mapping:
      plan: "{{ nodes.script.outputs.output }}"
    parameters:
      output_format: json
      template: |
        {"items": [{% for s in plan.scenes %}{% set visual = s.visual ~ " Style: " ~ style ~ "." %}{"visual": {{ visual | json_encode() }}, "narration": {{ s.narration | json_encode() }}, "image_model": {{ image_model | json_encode() }}, "image_size": {{ image_size | json_encode() }}, "tts_model": {{ tts_model | json_encode() }}, "voice": {{ voice | json_encode() }}, "moving": {% if s.motion and video_model %}true{% else %}false{% endif %}, "video_model": {{ video_model | json_encode() }}, "video_seconds": {{ video_seconds | json_encode() }}}{% if not loop.last %},{% endif %}{% endfor %}]}

  - id: produce
    type: map
    dependencies: ["shots"]
    input_mapping:
      input_list: "{{ nodes.shots.outputs.items }}"
    parameters:
      parallel: true
      max_concurrent: 4
      template:
        # `run_if` reads node outputs, not `item`, so the choice is
        # surfaced as an output first.
        - id: mode
          type: template
          input_mapping:
            moving: "{{ item.moving }}"
          parameters:
            output_format: json
            template: '{"moving": {{ moving }}}'
        - id: picture
          type: text_to_image
          dependencies: ["mode"]
          run_if: "{{ !nodes.mode.outputs.moving }}"
          input_mapping:
            visual: "{{ item.visual }}"
            model: "{{ item.image_model }}"
            size: "{{ item.image_size }}"
          parameters:
            prompt: "{{visual}}"
        - id: clip
          type: text_to_video
          dependencies: ["mode"]
          run_if: "{{ nodes.mode.outputs.moving }}"
          input_mapping:
            visual: "{{ item.visual }}"
            model: "{{ item.video_model }}"
            duration_seconds: "{{ item.video_seconds }}"
          parameters:
            prompt: "{{visual}}"
        - id: say
          type: tts
          input_mapping:
            input_template: "{{ item.narration }}"
            voice: "{{ item.voice }}"
            model: "{{ item.tts_model }}"

  # The scenes, in script order, as render_video takes them.
  - id: cut
    type: template
    dependencies: ["produce"]
    input_mapping:
      plan: "{{ nodes.script.outputs.output }}"
      made: "{{ nodes.produce.outputs.results }}"
    parameters:
      output_format: json
      template: |
        {"scenes": [{% for r in made %}{% set s = plan.scenes | nth(n=loop.index0) %}{"audio": {{ r.say.Ok.say_output.path | json_encode() }}, "caption": {{ s.narration | json_encode() }}, {% if r.clip.Ok %}"video": {{ r.clip.Ok.clip_video.path | json_encode() }}{% else %}"image": {{ r.picture.Ok.picture_image.path | json_encode() }}{% endif %}}{% if not loop.last %},{% endif %}{% endfor %}]}

  # `burn_subtitles` (the workflow input) reaches the node directly.
  - id: film
    type: render_video
    dependencies: ["cut"]
    input_mapping:
      scenes: "{{ nodes.cut.outputs.scenes }}"

  - id: deliverable
    type: template
    dependencies: ["film"]
    input_mapping:
      plan: "{{ nodes.script.outputs.output }}"
      mp4: "{{ nodes.film.outputs.film_output }}"
      srt: "{{ nodes.film.outputs.film_subtitles }}"
      seconds: "{{ nodes.film.outputs.film_seconds }}"
    parameters:
      output_format: json
      template: |
        {"deliverable": {
          "kind": "video_overview",
          "title": {{ plan.title | json_encode() }},
          "content": {"scenes": {{ plan.scenes | json_encode() }}, "seconds": {{ seconds | json_encode() }}},
          "citations": [{% set_global first = true %}{% for l in plan.scenes %}{% if l.chunk_id %}{% set_global sid = "" %}{% set_global passage = "" %}{% for c in chunks %}{% if c.chunk_id == l.chunk_id %}{% set_global sid = c.source_id %}{% set_global passage = c.content %}{% endif %}{% endfor %}{% if passage %}{% if not first %},{% endif %}{% set_global first = false %}{"source_id": {{ sid | json_encode() }}, "chunk_id": {{ l.chunk_id | json_encode() }}, "quote": {{ passage | truncate(length=300) | json_encode() }}}{% endif %}{% endif %}{% endfor %}],
          "files": [
            {"node_id": "film", "name": {{ mp4 | split(pat="/") | last | json_encode() }}, "mime_type": "video/mp4", "role": "primary"},
            {"node_id": "film", "name": {{ srt | split(pat="/") | last | json_encode() }}, "mime_type": "application/x-subrip", "role": "asset"}
          ]
        }}
