CtrlK
BlogDocsLog inGet started
Tessl Logo
PluginPublic

gamussa/reels-producer-skill

Write talking-head scripts and produce Instagram reels and YouTube shorts

1.07x
Quality

96%

Does it follow best practices?

Impact

98%

1.07x

Average score across 8 eval scenarios

SecuritybySnyk

Low

Low-risk findings worth noting

Overview
Quality
Evals
Security
Files

export_timeline.pyskills/reel-builder/scripts/

#!/usr/bin/env python3
"""Export cut_plan.json as an FCPXML timeline for manual editing in an NLE.

Opens in both Final Cut Pro (File > Import > XML) and DaVinci Resolve
(File > Import Timeline). Every segment lands as a clip pre-positioned at the
right source timecode; segment notes become clip names so the pacing phases
stay visible on the timeline. Music is attached as a connected audio clip.

Version notes (why the default is 1.10):
  - Resolve rejects fcpxml versions newer than it understands (1.12+ from
    modern FCP is the classic failure). Resolve 18+ imports 1.10; use
    --fcpxml-version 1.9 for Resolve 17.
  - Final Cut imports older versions and upgrades them silently.
  - Flat .fcpxml only — Resolve cannot read .fcpxmld bundles.

All times are frame-quantized (cut boundaries rounded cumulatively, so error
never exceeds half a frame) — NLEs require times on frame boundaries, and the
renderer quantizes to the same 30fps grid anyway.

Usage:
  export_timeline.py plan/cut_plan.json --out plan/reel.fcpxml [--name my-reel]
Round trip: edit in the NLE, export fcpxml (from Resolve: version 1.9/1.10),
then import_timeline.py it back into a cut plan.
"""

import argparse
import subprocess
import sys
import xml.etree.ElementTree as ET
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parent))
import plan_schema  # noqa: E402  — owner of cut_plan.json reads and writes

FPS = 30  # mezzanine grid — everything upstream is CFR 30
VOICE_LANE = "-2"  # the plan's continuous voice; music is lane -1
W, H = 1080, 1920


def rt(frames):  # rational time on the frame grid, e.g. 28 -> "2800/3000s"
    return f"{frames * 100}/{FPS * 100}s"


def quantize_bounds(durations, fps=FPS):
    """Cut boundaries on the frame grid, quantized cumulatively.

    Rounding each duration on its own lets error accumulate: sixteen segments
    each off by a third of a frame walk the last cut half a second off the
    beat. Rounding the running total instead pins every boundary to within
    half a frame of where the beat grid wanted it, no matter how many cuts
    precede it.

    Returns boundary frame numbers, length len(durations) + 1, starting at 0
    and strictly increasing (a segment never quantizes away to nothing).
    """
    bounds, acc, prev = [0], 0.0, 0
    for d in durations:
        acc += d
        prev = max(prev + 1, round(acc * fps))
        bounds.append(prev)
    return bounds


def media_duration(path):
    r = subprocess.run(
        ["ffprobe", "-v", "quiet", "-show_entries", "format=duration", "-of", "csv=p=0", str(path)],
        capture_output=True,
        text=True,
    )
    if r.returncode != 0 or not r.stdout.strip():
        sys.exit(f"ffprobe failed on {path} — do the mezzanine files exist?")
    return float(r.stdout.strip())


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("plan")
    ap.add_argument("--out", default="plan/reel.fcpxml")
    ap.add_argument("--name", help="Project name in the NLE (default: plan folder name)")
    ap.add_argument(
        "--fcpxml-version",
        default="1.10",
        choices=["1.9", "1.10"],
        help="1.10 for Resolve 18+/FCP (default); 1.9 for Resolve 17",
    )
    args = ap.parse_args()

    plan = plan_schema.load_or_exit(args.plan)
    segs = plan["segments"]
    name = args.name or Path(args.plan).resolve().parent.parent.name

    bounds = quantize_bounds([s["duration"] for s in segs])
    frame_durs = [b - a for a, b in zip(bounds, bounds[1:])]
    total_frames = bounds[-1]

    root = ET.Element("fcpxml", version=args.fcpxml_version)
    res = ET.SubElement(root, "resources")
    ET.SubElement(
        res,
        "format",
        id="r1",
        frameDuration=rt(1),
        width=str(W),
        height=str(H),
        colorSpace="1-1-1 (Rec. 709)",
    )

    # one asset per unique clip; in-points clamped to real media length
    assets, next_id = {}, 2

    def asset_for(path, has_video):
        nonlocal next_id
        p = Path(path).resolve()
        if p in assets:
            return assets[p]
        dur = media_duration(p)
        rid = f"r{next_id}"
        next_id += 1
        a = ET.SubElement(
            res,
            "asset",
            id=rid,
            name=p.stem,
            start="0s",
            duration=rt(int(dur * FPS)),
            hasVideo="1" if has_video else "0",
            hasAudio="1",
            audioSources="1",
            audioChannels="2",
            audioRate="48000",
        )
        if has_video:
            a.set("format", "r1")
        ET.SubElement(a, "media-rep", kind="original-media", src=p.as_uri())
        assets[p] = (rid, dur)
        return assets[p]

    lib = ET.SubElement(root, "library")
    event = ET.SubElement(lib, "event", name="Reel Builder")
    project = ET.SubElement(event, "project", name=name)
    seq = ET.SubElement(
        project,
        "sequence",
        format="r1",
        duration=rt(total_frames),
        tcStart="0s",
        tcFormat="NDF",
        audioLayout="stereo",
        audioRate="48k",
    )
    spine = ET.SubElement(seq, "spine")

    first_clip_el, first_in_frames = None, 0
    clamped = []
    for k, s in enumerate(segs):
        rid, media_dur = asset_for(s["clip"], has_video=True)
        max_in = max(0, int(media_dur * FPS) - frame_durs[k])
        want_in = max(0, round(s["in"] * FPS))
        in_frames = min(want_in, max_in)
        if in_frames < want_in:
            clamped.append((k, (want_in - in_frames) / FPS, Path(s["clip"]).name))
        el = ET.SubElement(
            spine,
            "asset-clip",
            ref=rid,
            name=s.get("note") or Path(s["clip"]).stem,
            offset=rt(bounds[k]),
            start=rt(in_frames),
            duration=rt(frame_durs[k]),
            format="r1",
            tcFormat="NDF",
        )
        if k == 0:
            first_clip_el, first_in_frames = el, in_frames

    # music: connected clip on lane -1, anchored to the head of the first clip
    # (offset of a connected clip is in the parent's source time, so anchoring
    # at the parent's start places it at timeline zero)
    if plan.get("music"):
        if first_clip_el is None:
            sys.exit("plan has music but no clips to anchor it to — add segments to the plan")
        rid, music_dur = asset_for(plan["music"], has_video=False)
        m_start = round(plan.get("music_start", 0.0) * FPS)
        m_len = min(total_frames, int(music_dur * FPS) - m_start)
        ET.SubElement(
            first_clip_el,
            "asset-clip",
            ref=rid,
            lane="-1",
            name=Path(plan["music"]).stem,
            offset=rt(first_in_frames),
            start=rt(m_start),
            duration=rt(max(1, m_len)),
        )

    # the continuous voice (a talk layout's silent picture needs it): its own
    # connected audio lane under the music, from the plan's vo.start
    vo = plan.get("vo") if isinstance(plan.get("vo"), dict) else None
    if vo and vo.get("file"):
        if first_clip_el is None:
            sys.exit("plan has a voice but no clips to anchor it to — add segments to the plan")
        rid, vo_dur = asset_for(vo["file"], has_video=False)
        v_start = round(float(vo.get("start", 0.0)) * FPS)
        v_len = min(total_frames, int(vo_dur * FPS) - v_start)
        ET.SubElement(
            first_clip_el,
            "asset-clip",
            ref=rid,
            lane=VOICE_LANE,
            name=Path(vo["file"]).stem,
            offset=rt(first_in_frames),
            start=rt(v_start),
            duration=rt(max(1, v_len)),
        )

    ET.indent(root)
    out = Path(args.out)
    out.parent.mkdir(parents=True, exist_ok=True)
    out.write_text(
        '<?xml version="1.0" encoding="UTF-8"?>\n'
        "<!DOCTYPE fcpxml>\n" + ET.tostring(root, encoding="unicode") + "\n"
    )

    if clamped:
        print(
            f"{len(clamped)} segment(s) asked for frames past the end of "
            f"their source; in-points were pulled back to fit. The cut moves "
            f"by the amount shown — run audit_plan.py to see them in the "
            f"plan itself:",
            file=sys.stderr,
        )
        for k, delta, name in clamped[:8]:
            print(f"  segment {k + 1}: {name} pulled back {delta * 1000:.0f} ms", file=sys.stderr)
        print("", file=sys.stderr)

    print(
        f"Timeline: {len(segs)} clips, {total_frames / FPS:.2f}s -> {out} "
        f"(fcpxml {args.fcpxml_version})"
    )
    print("  Final Cut Pro:   File > Import > XML")
    print("  DaVinci Resolve: File > Import Timeline (check 'import source clips')")
    print(
        "After manual edits, export fcpxml from the NLE (Resolve: v1.9/1.10) "
        "and run import_timeline.py to bring the cuts back into the plan."
    )


if __name__ == "__main__":
    main()

skills

tile.json