diff --git a/src/clipwright/pipeline.py b/src/clipwright/pipeline.py index ce3a7c8..18f90d6 100644 --- a/src/clipwright/pipeline.py +++ b/src/clipwright/pipeline.py @@ -531,10 +531,11 @@ def rerender_clip(manifest: Path, n: int, start: float, end: float, log: Log = p # otherwise ffprobe fails and all you get is its command line raise ValueError(f"the original video is gone ({source}), was it moved or renamed?") info = probe(source) + hook = clip_hook(entry) if opts.hook else None log(f"re-rendering {entry['file']}: {start:.1f}s - {end:.1f}s, {opts.layout} layout") try: r = _render_one(info, words, start, end, out_dir, base, opts, suffix=".tmp", log=log, - hook=entry.get("hook") if opts.hook else None, cuts=_saved_cuts(source), + hook=hook, cuts=_saved_cuts(source), on_progress=lambda f: progress("render", f)) except BaseException: for leftover in (f"{base}.tmp.mp4", f"{base}.tmp.ass", f"{base}.tmp.srt", f"{base}.tmp.jpg"): @@ -558,10 +559,19 @@ def rerender_clip(manifest: Path, n: int, start: float, end: float, log: Log = p edited=bool(entry.get("edited")) or moved) if layout is not None: entry["layout"] = layout + if hook: + entry["hook"] = hook _write_manifest(manifest, data) return dict(entry) +def clip_hook(entry: dict[str, Any]) -> str | None: + """The hook a --hook clip should show: its title when it has one (a run with --copy, or one + written later on the review page), like run() does, otherwise the hook it was rendered with.""" + title = (entry.get("copy") or {}).get("title") + return str(title) if title else entry.get("hook") + + def _saved_cuts(source: Path) -> list[float]: """The source's scene cuts from its analysis cache, for re-renders (they have no Signals). None saved (a --no-cache run, or the cache was deleted): no cuts, the camera pans as before.""" diff --git a/src/clipwright/web/index.html b/src/clipwright/web/index.html index 1ea597f..f4ebd4e 100644 --- a/src/clipwright/web/index.html +++ b/src/clipwright/web/index.html @@ -55,6 +55,8 @@ .clip .post { font-size: 13px; display: grid; gap: 2px; } .clip .post strong { font-size: 14px; } .clip .post .tags { color: var(--muted); } + .clip .hook-note { font-size: 13px; color: var(--muted); display: flex; gap: 8px; align-items: center; } + .clip .hook-note:empty { display: none; } .clip .files a { color: var(--muted); } .clip .actions { display: flex; gap: 8px; margin-top: auto; } .clip .actions button { flex: 1; } @@ -346,14 +348,13 @@

clipwright

start = clip.start; end = clip.end; layout = layoutSelect.value = saved; err.textContent = ""; showTimes(); }); - const rerender = button("Re-render", async () => { - if (end - start < 1) { err.textContent = "a clip has to be at least 1s long"; return; } - const body = editBody(clip, start, end, layout, saved); - if (!body) return; + // re-renders with `body` from the button `pressed`; on success every card is rebuilt + const renderWith = async (body, pressed) => { + const label = pressed.textContent; err.textContent = ""; card.classList.add("busy"); - rerender.disabled = reset.disabled = layoutSelect.disabled = true; - rerender.textContent = "Rendering..."; + rerender.disabled = reset.disabled = layoutSelect.disabled = pressed.disabled = true; + pressed.textContent = "Rendering..."; // let go of the file first: on Windows the server can't replace a file that's still being streamed video.pause(); video.removeAttribute("src"); @@ -366,11 +367,17 @@

clipwright

} catch (e) { err.textContent = `re-render failed: ${e.message}`; video.src = src; - rerender.textContent = "Re-render"; - layoutSelect.disabled = false; + pressed.textContent = label; + layoutSelect.disabled = pressed.disabled = false; card.classList.remove("busy"); showTimes(); } + }; + const rerender = button("Re-render", async () => { + if (end - start < 1) { err.textContent = "a clip has to be at least 1s long"; return; } + const body = editBody(clip, start, end, layout, saved); + if (!body) return; + await renderWith(body, rerender); }, "primary"); const keepRow = el("div", "actions"); @@ -418,6 +425,20 @@

clipwright

} }; showPost(clip.copy); + // with --hook the title is burned into the first seconds of the clip. After a rewrite the video + // still shows the old one until it's rendered again (over the range it has now) + const hookNote = el("div", "hook-note"); + const showHookNote = () => { + hookNote.replaceChildren(); + if (!manifest.render?.hook || !clip.copy || clip.hook === clip.copy.title) return; + // takes along a trim or layout change that's waiting on this card, same as the main Re-render + const again = button("Re-render", async () => { + if (end - start < 1) { err.textContent = "a clip has to be at least 1s long"; return; } + await renderWith(editBody(clip, start, end, layout, saved) ?? { start: clip.start, end: clip.end }, again); + }); + hookNote.append(el("span", null, "the hook in the video is out of date"), again); + }; + showHookNote(); // writes the text for this clip's current range: after a trim, or for a run that didn't ask for it. // Uses the form's "Write titles with" choice if one is set, otherwise whatever the run used const titleLabel = () => (clip.copy ? "Rewrite title" : "Write title"); @@ -434,6 +455,7 @@

clipwright

clip.post = updated.manifest.clips[n - 1].post; showPost(clip.copy); showFiles(); // the first title adds the .txt + showHookNote(); // only the text changed, keep the cards (and their videos) as they are shownManifest = JSON.stringify(updated.manifest); } catch (e) { @@ -444,7 +466,7 @@

clipwright

}); const titleRow = el("div", "actions"); titleRow.append(writeTitle); - meta.append(times, score, why, post, files, startRow, endRow, layoutRow, editRow, keepRow); + meta.append(times, score, why, post, hookNote, files, startRow, endRow, layoutRow, editRow, keepRow); // a title is written from the transcript, without one there's nothing to send if (manifest.words) meta.append(titleRow); meta.append(err); diff --git a/tests/test_rerender.py b/tests/test_rerender.py index e547565..5108201 100644 --- a/tests/test_rerender.py +++ b/tests/test_rerender.py @@ -333,3 +333,53 @@ def test_temp_clips_still_open_elsewhere_are_skipped(tmp_path: Path): with open(tmp_path / "v_clip02.tmp.mp4", "rb"): # like ffmpeg still writing a re-render pipeline._remove_temp_clips(tmp_path, "v") assert sorted(p.name for p in tmp_path.iterdir()) == ["v_clip02.tmp.mp4"] + + +def _capture_hooks(monkeypatch: pytest.MonkeyPatch) -> list[str | None]: + hooks: list[str | None] = [] + + def build_ass(*_a: object, hook: str | None = None, **_kw: object) -> str: + hooks.append(hook) + return "" + + def make_cover(*_a: object, title: str | None = None, **_kw: object) -> None: + hooks.append(f"cover: {title}") + raise RuntimeError("no cover in this test") + + _capture_layouts(monkeypatch) + monkeypatch.setattr(pipeline, "build_ass", build_ass) + monkeypatch.setattr(pipeline.thumbnail, "make_cover", make_cover) + return hooks + + +def test_rerender_burns_in_the_current_title_as_the_hook(tmp_path: Path, monkeypatch: pytest.MonkeyPatch): + manifest = fake_run(tmp_path) + data = json.loads(manifest.read_text()) + data["render"]["hook"] = True + # clip 1's title was rewritten on the review page after the run burned in the old one + data["clips"][0].update(hook="Old title", copy={"title": "New title", "description": "", "hashtags": []}) + data["clips"][1]["hook"] = "no way he did that" # a run without --copy: the hook came from the transcript + manifest.write_text(json.dumps(data)) + hooks = _capture_hooks(monkeypatch) + assert rerender_clip(manifest, 1, 10, 15, log=lambda _m: None)["hook"] == "New title" + assert rerender_clip(manifest, 2, 21, 25, log=lambda _m: None)["hook"] == "no way he did that" + # the cover draws the hook too (#173) + assert hooks == ["New title", "cover: New title", "no way he did that", "cover: no way he did that"] + assert json.loads(manifest.read_text())["clips"][0]["hook"] == "New title" + + +def test_no_hook_without_the_hook_option(tmp_path: Path, monkeypatch: pytest.MonkeyPatch): + manifest = fake_run(tmp_path) # render has no hook + data = json.loads(manifest.read_text()) + data["clips"][0]["copy"] = {"title": "A title", "description": "", "hashtags": []} + manifest.write_text(json.dumps(data)) + hooks = _capture_hooks(monkeypatch) + entry = rerender_clip(manifest, 1, 10, 15, log=lambda _m: None) + assert hooks == ["cover: None"] and "hook" not in entry # no words and no hook: no .ass at all + + +def test_clip_hook(): + assert pipeline.clip_hook({"hook": "old", "copy": {"title": "new"}}) == "new" + assert pipeline.clip_hook({"hook": "old"}) == "old" + assert pipeline.clip_hook({"hook": "old", "copy": None}) == "old" + assert pipeline.clip_hook({}) is None diff --git a/tests/test_write_title.py b/tests/test_write_title.py index f484b7a..18979f1 100644 --- a/tests/test_write_title.py +++ b/tests/test_write_title.py @@ -265,3 +265,15 @@ def test_page_links_the_post_text_and_refreshes_it(): write = html[html.index("const writeTitle = button("):] write = write[:write.index("titleRow.append(writeTitle)")] assert "clip.post = updated.manifest.clips[n - 1].post;" in write and "showFiles();" in write + + +def test_page_flags_an_out_of_date_hook(): + html = (Path(__file__).parents[1] / "src" / "clipwright" / "web" / "index.html").read_text(encoding="utf-8") + note = html[html.index("const showHookNote = () => {"):] + note = note[:note.index("showHookNote();")] + # only for --hook runs, and only when the burned-in hook isn't the current title + assert "manifest.render?.hook" in note and "clip.hook === clip.copy.title" in note + # a pending trim or layout goes along, an untouched card renders its current range + assert "renderWith(editBody(clip, start, end, layout, saved) ?? { start: clip.start, end: clip.end }" in note + write = html[html.index("const writeTitle = button("):] + assert "showHookNote();" in write[:write.index("titleRow.append(writeTitle)")]