Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

Play the shots rendered so far, keep each film look to swap back to, and render shots four at a time with retries (#11589)

[._.]/60dab216863d
diff --git a/.env.example b/.env.example
index 61e242cda4..784ec40971 100644
--- a/.env.example
+++ b/.env.example
@@ -384,3 +384,7 @@ PGPASSWORD=portos
# HY3DGEN_MODELS=/path/to/hy3dgen
# HF_XET_CACHE=/path/to/xet
# PIXIE_FORGE_HOME=/path/to/.pixie-forge
+
+# Music-video shots rendered at once in a final render (shotSegments.js;
+# default: up to 4, fewer on a small machine). Each holds a film-look browser.
+# PORTOS_MUSIC_VIDEO_SEGMENT_WORKERS=4
diff --git a/client/src/components/musicVideo/FilmLookVersions.jsx b/client/src/components/musicVideo/FilmLookVersions.jsx
new file mode 100644
index 0000000000..763e9add76
--- /dev/null
+++ b/client/src/components/musicVideo/FilmLookVersions.jsx
@@ -0,0 +1,74 @@
+import { useEffect, useRef, useState } from 'react';
+import { Check, Trash2 } from 'lucide-react';
+import { deleteMusicVideoFilmLook, getMusicVideoFilmLooks } from '../../services/apiMusicVideo.js';
+import { FILM_LOOK_PRESETS, describeFilmLook } from '../../lib/filmLook.js';
+import { formatBytes } from '../../utils/formatters.js';
+
+const BUTTON = 'inline-flex min-h-[44px] items-center gap-1 rounded border border-port-border bg-port-bg px-2 py-1.5 text-xs hover:bg-port-border/40 disabled:opacity-50 sm:min-h-0';
+const presetLabel = (look) => FILM_LOOK_PRESETS.find((p) => p.id === look?.preset)?.label || 'Custom';
+
+/**
+ * The film looks rendered over the current takes, kept side by side: Use puts
+ * one back on the video (the final render then only stitches its cached
+ * shots), Delete frees its files. Shots with their own look are not counted.
+ */
+export default function FilmLookVersions({ project, onUse }) {
+ const projectId = project?.id;
+ const composed = project?.composition?.mode === 'composed';
+ const lookKey = JSON.stringify(project?.filmLook ?? null);
+ const [state, setState] = useState(null);
+ const [busyId, setBusyId] = useState(null);
+ const [error, setError] = useState(null);
+ const ticket = useRef(0);
+
+ useEffect(() => {
+ if (!projectId || !composed) { setState(null); return undefined; }
+ const mine = ++ticket.current;
+ const timer = setTimeout(() => {
+ getMusicVideoFilmLooks(projectId, { silent: true })
+ .then((next) => { if (mine === ticket.current) setState(next); })
+ .catch(() => {});
+ }, 400);
+ return () => clearTimeout(timer);
+ }, [projectId, composed, lookKey, project?.renderHistoryId]);
+
+ const remove = (id) => {
+ setBusyId(id);
+ setError(null);
+ ticket.current += 1;
+ deleteMusicVideoFilmLook(projectId, id, { silent: true })
+ .then(setState)
+ .catch((err) => setError(err?.message || 'Could not delete that look'))
+ .finally(() => setBusyId(null));
+ };
+
+ const looks = state?.looks || [];
+ if (!state?.available || !looks.length) return null;
+ return (
+ <section aria-label="Film looks rendered" className="min-w-0 space-y-2 rounded border border-port-border p-2">
+ <h4 className="text-xs font-medium">Film looks rendered</h4>
+ {error && <p role="alert" className="break-words text-[11px] text-port-error">{error}</p>}
+ <ul className="space-y-1.5">
+ {looks.map((entry) => (
+ <li key={entry.id} data-testid="film-look-version" className="flex flex-wrap items-center gap-2 text-xs">
+ <span className="min-w-0 flex-1">
+ <span className="font-medium">{presetLabel(entry.look)}</span>
+ {entry.current && <span className="ml-1.5 rounded bg-port-success/10 px-1.5 text-[11px] text-port-success">In use</span>}
+ <span className="block truncate text-[11px] text-port-text-muted" title={describeFilmLook(entry.look)}>
+ {entry.rendered} of {state.total} shots · {formatBytes(entry.bytes)}
+ </span>
+ </span>
+ {!entry.current && (
+ <button type="button" className={BUTTON} onClick={() => onUse(entry.look)} disabled={busyId != null}>
+ <Check size={12} aria-hidden="true" /> Use
+ </button>
+ )}
+ <button type="button" className={BUTTON} onClick={() => remove(entry.id)} disabled={busyId != null} aria-label={`Delete ${presetLabel(entry.look)} renders`}>
+ <Trash2 size={12} aria-hidden="true" /> Delete
+ </button>
+ </li>
+ ))}
+ </ul>
+ </section>
+ );
+}
diff --git a/client/src/components/musicVideo/FilmLookVersions.test.jsx b/client/src/components/musicVideo/FilmLookVersions.test.jsx
new file mode 100644
index 0000000000..161341d4ff
--- /dev/null
+++ b/client/src/components/musicVideo/FilmLookVersions.test.jsx
@@ -0,0 +1,39 @@
+import { describe, expect, it, vi, beforeEach } from 'vitest';
+import { act, fireEvent, render, screen } from '@testing-library/react';
+
+vi.mock('../../services/apiMusicVideo.js', () => ({
+ getMusicVideoFilmLooks: vi.fn(),
+ deleteMusicVideoFilmLook: vi.fn(),
+}));
+
+import FilmLookVersions from './FilmLookVersions.jsx';
+import { deleteMusicVideoFilmLook, getMusicVideoFilmLooks } from '../../services/apiMusicVideo.js';
+
+const project = { id: 'mv-1', composition: { mode: 'composed' }, filmLook: { preset: 'super8' } };
+const super8 = { id: 'b'.repeat(32), look: { preset: 'super8', grain: 0.75 }, current: true, rendered: 116, bytes: 650 * 1024 ** 2 };
+const vhs = { id: 'c'.repeat(32), look: { preset: 'vhs', grain: 0.25 }, current: false, rendered: 40, bytes: 120 * 1024 ** 2 };
+
+beforeEach(() => vi.clearAllMocks());
+
+describe('FilmLookVersions', () => {
+ it('lists the cached looks, puts one back on the video, and deletes one', async () => {
+ vi.useFakeTimers();
+ getMusicVideoFilmLooks.mockResolvedValue({ available: true, total: 116, looks: [super8, vhs] });
+ const onUse = vi.fn();
+ render(<FilmLookVersions project={project} onUse={onUse} />);
+ await act(async () => { await vi.advanceTimersByTimeAsync(500); });
+ vi.useRealTimers();
+ const rows = screen.getAllByTestId('film-look-version');
+ expect(rows.map((row) => row.textContent)).toEqual([
+ expect.stringMatching(/Super 8In use116 of 116 shots/),
+ expect.stringMatching(/VHS40 of 116 shots/),
+ ]);
+ fireEvent.click(screen.getByRole('button', { name: /Use/ }));
+ expect(onUse).toHaveBeenCalledWith(vhs.look);
+
+ deleteMusicVideoFilmLook.mockResolvedValueOnce({ available: true, total: 116, looks: [super8] });
+ await act(async () => fireEvent.click(screen.getByRole('button', { name: 'Delete VHS renders' })));
+ expect(deleteMusicVideoFilmLook).toHaveBeenCalledWith('mv-1', vhs.id, { silent: true });
+ expect(screen.getAllByTestId('film-look-version')).toHaveLength(1);
+ });
+});
diff --git a/client/src/components/musicVideo/RenderedSoFarPlayer.jsx b/client/src/components/musicVideo/RenderedSoFarPlayer.jsx
new file mode 100644
index 0000000000..71647a0bdb
--- /dev/null
+++ b/client/src/components/musicVideo/RenderedSoFarPlayer.jsx
@@ -0,0 +1,77 @@
+import { useEffect, useRef, useState } from 'react';
+import { Activity, Play, RefreshCw, X } from 'lucide-react';
+import { buildMusicVideoRenderPreview, musicVideoShotRenderUrl } from '../../services/apiMusicVideo.js';
+import { useLocalStorageBool } from '../../hooks/useLocalStorageBool.js';
+import { formatDurationSec } from '../../utils/formatters.js';
+
+const BUTTON = 'inline-flex min-h-[44px] items-center gap-1 rounded border border-port-border bg-port-bg px-2 py-1.5 text-xs hover:bg-port-border/40 disabled:opacity-50 sm:min-h-0';
+
+/**
+ * The composed final render's shots finished so far, stitched in order up to
+ * the first one still missing, with the song under them: a seam check while
+ * the rest renders. Phone copy plays a lighter encode (film grain makes the
+ * master heavy for a phone); off, it plays the exact shots.
+ */
+export default function RenderedSoFarPlayer({ project }) {
+ const projectId = project?.id;
+ const [light, setLight] = useLocalStorageBool('portos.musicVideo.renderedSoFar.light', false);
+ const [preview, setPreview] = useState(null);
+ const [building, setBuilding] = useState(false);
+ const [error, setError] = useState(null);
+ const generation = useRef(0);
+
+ useEffect(() => { generation.current += 1; setPreview(null); setError(null); setBuilding(false); }, [projectId]);
+
+ const build = (asLight = light) => {
+ const ticket = ++generation.current;
+ setBuilding(true);
+ setError(null);
+ buildMusicVideoRenderPreview(projectId, { light: asLight }, { silent: true })
+ .then((next) => { if (ticket === generation.current) setPreview(next); })
+ .catch((err) => { if (ticket === generation.current) setError(err?.message || 'Could not stitch the rendered shots'); })
+ .finally(() => { if (ticket === generation.current) setBuilding(false); });
+ };
+ const pickLight = (next) => {
+ setLight(next);
+ if (preview) build(next);
+ };
+
+ if (project?.composition?.mode !== 'composed') return null;
+ return (
+ <section aria-label="Rendered so far" className="min-w-0 space-y-2 rounded border border-port-border p-2">
+ <div className="flex flex-wrap items-center gap-2">
+ <h4 className="text-xs font-medium">Rendered so far</h4>
+ {preview && (
+ <span data-testid="rendered-so-far-state" className="text-[11px] text-port-text-muted">
+ {preview.complete ? 'Whole video' : `${preview.shots} of ${preview.totalShots} shots`} · {formatDurationSec(preview.durationSec)}
+ </span>
+ )}
+ <span className="ml-auto flex flex-wrap items-center gap-2">
+ <label htmlFor={`rendered-so-far-light-${projectId}`} className="inline-flex min-h-[44px] items-center gap-1 text-[11px] text-port-text-muted sm:min-h-0">
+ <input id={`rendered-so-far-light-${projectId}`} type="checkbox" checked={light} onChange={(e) => pickLight(e.target.checked)} disabled={building} /> Phone copy
+ </label>
+ {preview ? (
+ <>
+ {!preview.complete && (
+ <button type="button" className={BUTTON} onClick={() => build()} disabled={building}>
+ {building ? <Activity size={12} className="animate-spin" aria-hidden="true" /> : <RefreshCw size={12} aria-hidden="true" />} Update
+ </button>
+ )}
+ <button type="button" className={BUTTON} onClick={() => setPreview(null)}>
+ <X size={12} aria-hidden="true" /> Close
+ </button>
+ </>
+ ) : (
+ <button type="button" className={BUTTON} onClick={() => build()} disabled={building}>
+ {building ? <Activity size={12} className="animate-spin" aria-hidden="true" /> : <Play size={12} aria-hidden="true" />} Play
+ </button>
+ )}
+ </span>
+ </div>
+ {error && <p role="alert" className="break-words text-[11px] text-port-error">{error}</p>}
+ {preview && (
+ <video key={preview.name} src={musicVideoShotRenderUrl(projectId, preview.name)} controls autoPlay playsInline className="w-full rounded bg-black" />
+ )}
+ </section>
+ );
+}
diff --git a/client/src/components/musicVideo/RenderedSoFarPlayer.test.jsx b/client/src/components/musicVideo/RenderedSoFarPlayer.test.jsx
new file mode 100644
index 0000000000..65e4f84230
--- /dev/null
+++ b/client/src/components/musicVideo/RenderedSoFarPlayer.test.jsx
@@ -0,0 +1,42 @@
+import { describe, expect, it, vi, beforeEach } from 'vitest';
+import { act, fireEvent, render, screen } from '@testing-library/react';
+
+vi.mock('../../services/apiMusicVideo.js', () => ({
+ buildMusicVideoRenderPreview: vi.fn(),
+ musicVideoShotRenderUrl: (id, name) => `/api/music-video/${id}/shot-renders/${name}`,
+}));
+
+import RenderedSoFarPlayer from './RenderedSoFarPlayer.jsx';
+import { buildMusicVideoRenderPreview } from '../../services/apiMusicVideo.js';
+
+const project = { id: 'mv-1', composition: { mode: 'composed' } };
+const key = 'a'.repeat(32);
+
+beforeEach(() => {
+ vi.clearAllMocks();
+ localStorage.clear();
+});
+
+describe('RenderedSoFarPlayer', () => {
+ it('plays the shots stitched so far, updates as more finish, and switches to the phone copy', async () => {
+ buildMusicVideoRenderPreview.mockResolvedValueOnce({ name: `preview-${key}.mp4`, light: false, durationSec: 45.5, shots: 12, totalShots: 116, complete: false });
+ render(<RenderedSoFarPlayer project={project} />);
+ await act(async () => fireEvent.click(screen.getByRole('button', { name: /Play/ })));
+ expect(buildMusicVideoRenderPreview).toHaveBeenCalledWith('mv-1', { light: false }, { silent: true });
+ expect(screen.getByTestId('rendered-so-far-state').textContent).toBe('12 of 116 shots · 0:45');
+ expect(document.querySelector('video').getAttribute('src')).toBe(`/api/music-video/mv-1/shot-renders/preview-${key}.mp4`);
+
+ buildMusicVideoRenderPreview.mockResolvedValueOnce({ name: `preview-${key}-light.mp4`, light: true, durationSec: 248.4, shots: 116, totalShots: 116, complete: true });
+ await act(async () => fireEvent.click(screen.getByLabelText('Phone copy')));
+ expect(buildMusicVideoRenderPreview).toHaveBeenLastCalledWith('mv-1', { light: true }, { silent: true });
+ expect(screen.getByTestId('rendered-so-far-state').textContent).toBe('Whole video · 4:08');
+ expect(screen.queryByRole('button', { name: /Update/ })).toBeNull();
+ });
+
+ it('says why nothing plays yet', async () => {
+ buildMusicVideoRenderPreview.mockRejectedValueOnce(new Error('No shot is rendered yet: the first shot plays here once it is done'));
+ render(<RenderedSoFarPlayer project={project} />);
+ await act(async () => fireEvent.click(screen.getByRole('button', { name: /Play/ })));
+ expect(screen.getByRole('alert').textContent).toMatch(/No shot is rendered yet/);
+ });
+});
diff --git a/client/src/components/musicVideo/stages/ReviewStage.jsx b/client/src/components/musicVideo/stages/ReviewStage.jsx
index a149e0a7b1..e4ea6e4adb 100644
--- a/client/src/components/musicVideo/stages/ReviewStage.jsx
+++ b/client/src/components/musicVideo/stages/ReviewStage.jsx
@@ -6,6 +6,8 @@ import StageSection from '../StageSection.jsx';
import { isFinalRenderStale } from '../../../lib/musicVideoStages.js';
import { RenderFinalButton } from '../ProjectActionGroups.jsx';
import ReviewDraftPanel from '../ReviewDraftPanel.jsx';
+import RenderedSoFarPlayer from '../RenderedSoFarPlayer.jsx';
+import FilmLookVersions from '../FilmLookVersions.jsx';
/**
* Final render: the full render and its player, and the draft excerpts and
@@ -19,6 +21,11 @@ export default function ReviewStage({ board }) {
const {
project, locked, renderJob, renderBound, finalVideo, excerpts, revisions, autoReview, sceneMedia,
} = board;
+ // A cached look goes back on the video; the final render then only stitches it.
+ const applyCachedLook = (filmLook) => {
+ board.editProjectLocal({ filmLook });
+ board.saveProjectFields({ filmLook });
+ };
const finalRender = (
<section id="mv-final-video" aria-label="Final render" className="min-w-0 space-y-2 rounded-lg border border-port-border bg-port-card p-3">
<div className="flex justify-end">
@@ -32,6 +39,8 @@ export default function ReviewStage({ board }) {
finalVideo={finalVideo}
onOpenPreview={board.openPreview}
/>
+ <RenderedSoFarPlayer project={project} />
+ <FilmLookVersions project={project} onUse={applyCachedLook} />
{project.renderHistoryId && <SharingCopyPanel key={`${project.id}:${project.renderHistoryId}`} projectId={project.id} />}
</section>
);
diff --git a/client/src/services/apiMusicVideo.js b/client/src/services/apiMusicVideo.js
index 7d54639cce..f1263c83a9 100644
--- a/client/src/services/apiMusicVideo.js
+++ b/client/src/services/apiMusicVideo.js
@@ -291,6 +291,16 @@ export const cancelMusicVideoShotRender = (id, sceneId, options = {}) =>
// A rendered shot's silent segment, to play.
export const musicVideoShotRenderUrl = (id, name) =>
`/api/music-video/${encodeURIComponent(id)}/shot-renders/${encodeURIComponent(name)}`;
+// The shots rendered so far stitched in order with the song (`light`: a phone copy):
+// { name, light, durationSec, shots, totalShots, complete }. Play it at musicVideoShotRenderUrl(id, name).
+export const buildMusicVideoRenderPreview = (id, { light = false } = {}, options = {}) =>
+ request(`/music-video/${encodeURIComponent(id)}/shot-renders/preview`, { method: 'POST', body: JSON.stringify({ light }), ...options });
+// The video-wide film looks cached over the current takes:
+// { available, error?, total, looks: [{ id, look, current, rendered, bytes }] }.
+export const getMusicVideoFilmLooks = (id, options = {}) =>
+ request(`/music-video/${encodeURIComponent(id)}/film-looks`, options);
+export const deleteMusicVideoFilmLook = (id, lookId, options = {}) =>
+ request(`/music-video/${encodeURIComponent(id)}/film-looks/${encodeURIComponent(lookId)}`, { method: 'DELETE', ...options });
// EventSource URL for a render job's progress stream (consumed by useSseProgress).
export const musicVideoRenderEventsUrl = (jobId) =>
diff --git a/server/lib/musicVideoValidation.js b/server/lib/musicVideoValidation.js
index cfe57509fa..ff4c956f75 100644
--- a/server/lib/musicVideoValidation.js
+++ b/server/lib/musicVideoValidation.js
@@ -93,6 +93,8 @@ export const MUSIC_VIDEO_MODES = ['director', 'autonomous'];
export const MUSIC_VIDEO_STATUSES = ['draft', 'analyzed', 'ready', 'rendering', 'complete', 'failed'];
// Re-read a project's song style from its Suno song link.
+// The rendered-so-far player: `light` asks for the lighter phone copy.
+export const musicVideoRenderPreviewSchema = z.object({ light: z.boolean().optional() }).strict();
export const musicVideoSongStyleSunoSchema = z.object({ url: z.string().trim().min(1).max(2000) });
// Optional global visual direction for the whole video.
diff --git a/server/routes/musicVideo.js b/server/routes/musicVideo.js
index 9a9ba48300..6e83e85b0b 100644
--- a/server/routes/musicVideo.js
+++ b/server/routes/musicVideo.js
@@ -12,7 +12,7 @@ import { existsSync } from 'fs';
import { unlink } from 'fs/promises';
import { pipeline } from 'stream/promises';
import { Router } from 'express';
-import { musicVideoSongStyleSunoSchema, musicVideoProjectListQuerySchema, musicVideoFinishedOutsideSchema, musicVideoProductionDraftSchema, musicVideoProductionApprovalSchema, musicVideoShotLockSchema, musicVideoProductionProofSchema, musicVideoProductionImportSchema, musicVideoContinueShotSchema, musicVideoPoolUseSchema, musicVideoPoolMarkSchema, musicVideoPoolSearchQuerySchema, musicVideoPoolDeleteSchema, musicVideoPoolImportSchema, musicVideoLibrarySearchQuerySchema, musicVideoPoolAddSchema, musicVideoProductionFeedbackSchema, musicVideoProductionFeedbackResolutionSchema, musicVideoLyricAnchorSyncSchema, musicVideoProductionReviseSchema, musicVideoProductionRevertSchema } from '../lib/musicVideoValidation.js';
+import { musicVideoRenderPreviewSchema, musicVideoSongStyleSunoSchema, musicVideoProjectListQuerySchema, musicVideoFinishedOutsideSchema, musicVideoProductionDraftSchema, musicVideoProductionApprovalSchema, musicVideoShotLockSchema, musicVideoProductionProofSchema, musicVideoProductionImportSchema, musicVideoContinueShotSchema, musicVideoPoolUseSchema, musicVideoPoolMarkSchema, musicVideoPoolSearchQuerySchema, musicVideoPoolDeleteSchema, musicVideoPoolImportSchema, musicVideoLibrarySearchQuerySchema, musicVideoPoolAddSchema, musicVideoProductionFeedbackSchema, musicVideoProductionFeedbackResolutionSchema, musicVideoLyricAnchorSyncSchema, musicVideoProductionReviseSchema, musicVideoProductionRevertSchema } from '../lib/musicVideoValidation.js';
import { productionReadiness } from '../services/musicVideo/productionReview.js';
import { finishedOutsideBlocker } from '../lib/musicVideoFinishedOutside.js';
import { getProductionReview, saveProductionDraft, prepareProductionReview, approveProductionReview, lockProductionShots, renderProductionProof, requireProductionReviewer, importProductionPlanning, bindProductionShot, addProductionFeedback, closeProductionFeedback, syncLyricAnchors, removeStaleStoryboardRows, reviseProductionFromFeedback, revertProductionInput } from '../services/musicVideo/productionReviewService.js';
@@ -733,6 +733,21 @@ router.post('/:id/shots/:sceneId/render/cancel', asyncHandler(async (req, res) =
const { cancelShotRender } = await import('../services/musicVideo/shotSegments.js');
res.json({ ok: cancelShotRender(req.params.id, req.params.sceneId) });
}));
+// The shots rendered so far, stitched in order with the song (a seam check
+// while the rest renders), and the film looks cached over the current takes.
+router.post('/:id/shot-renders/preview', asyncHandler(async (req, res) => {
+ const { light } = validateRequest(musicVideoRenderPreviewSchema, req.body ?? {});
+ const { buildRenderPreview } = await import('../services/musicVideo/shotSegments.js');
+ res.json(await buildRenderPreview(req.params.id, { light: light === true }));
+}));
+router.get('/:id/film-looks', asyncHandler(async (req, res) => {
+ const { listFilmLooks } = await import('../services/musicVideo/shotSegments.js');
+ res.json(await listFilmLooks(req.params.id));
+}));
+router.delete('/:id/film-looks/:lookId', asyncHandler(async (req, res) => {
+ const { deleteFilmLook } = await import('../services/musicVideo/shotSegments.js');
+ res.json(await deleteFilmLook(req.params.id, req.params.lookId));
+}));
router.get('/:id/shot-renders/:name', asyncHandler(async (req, res) => {
const { shotSegmentFile } = await import('../services/musicVideo/shotSegments.js');
const path = shotSegmentFile(req.params.id, req.params.name);
diff --git a/server/services/htmlComposition/encode.js b/server/services/htmlComposition/encode.js
index a5da1f23f2..8132fb5a6d 100644
--- a/server/services/htmlComposition/encode.js
+++ b/server/services/htmlComposition/encode.js
@@ -45,9 +45,10 @@ const SCREENSHOT = Object.freeze({ format: 'png', optimizeForSpeed: true, fromSu
// the window edge as an edge of the song.
// A stalled or lost browser (a CDP command past its deadline on an overloaded
// host, a crashed target) is not the document's fault, and seeks are
-// deterministic, so a fresh browser can redraw the same frame (#10839).
+// deterministic, so a fresh browser can redraw the same frame (#10839). A
+// film-look page's video seek that never settles is the same stall.
// Anything else (a script error, a refused asset) fails as before.
-const RECOVERABLE_PAGE_FAILURE = /Browser command timed out|Composition browser target crashed|Composition browser target closed|Managed browser disconnected/;
+const RECOVERABLE_PAGE_FAILURE = /Browser command timed out|Composition browser target crashed|Composition browser target closed|Managed browser disconnected|render seek timed out/;
const MAX_FRAME_RETRIES = 2;
// `reopenPage(failedPage)` resolves a fresh page when a capture hits a
diff --git a/server/services/musicVideo/filmLookFinish.js b/server/services/musicVideo/filmLookFinish.js
index 84c11de822..c49720f8b8 100644
--- a/server/services/musicVideo/filmLookFinish.js
+++ b/server/services/musicVideo/filmLookFinish.js
@@ -39,6 +39,9 @@ function filmLookFinishPage({ width, height, fps, durationSec, offsetSec = 0, sr
const js = `(() => {
const FPS = ${fps}, DURATION = ${durationSec}, OFFSET = ${offsetSec}, WIDTH = ${width}, HEIGHT = ${height};
const FRAME_GRACE_MS = 1000;
+ // A loaded host can take many seconds to decode the first frame; kept under
+ // the browser's 30 s command deadline so the stall is named, then retried.
+ const SEEK_TIMEOUT_MS = 25000;
const video = document.getElementById('source');
const loaded = new Promise((resolve, reject) => {
if (video.readyState >= 2) { resolve(); return; }
@@ -52,7 +55,7 @@ function filmLookFinishPage({ width, height, fps, durationSec, offsetSec = 0, sr
let presented = typeof video.requestVideoFrameCallback !== 'function';
let grace = null;
let settled = false;
- const timer = setTimeout(() => { cleanup(); reject(new Error('render seek timed out @ ' + target.toFixed(3) + 's')); }, 15000);
+ const timer = setTimeout(() => { cleanup(); reject(new Error('render seek timed out @ ' + target.toFixed(3) + 's')); }, SEEK_TIMEOUT_MS);
const finish = () => { if (seeked && presented) { cleanup(); resolve(); } };
const onSeeked = () => {
seeked = true;
diff --git a/server/services/musicVideo/shotSegments.js b/server/services/musicVideo/shotSegments.js
index 40bbce1f2c..97a764e2e5 100644
--- a/server/services/musicVideo/shotSegments.js
+++ b/server/services/musicVideo/shotSegments.js
@@ -21,10 +21,10 @@
import { createHash, randomUUID } from 'crypto';
import { existsSync } from 'fs';
-import { copyFile, mkdir, readdir, rename, rm, stat, writeFile } from 'fs/promises';
+import { copyFile, mkdir, readdir, readFile, rename, rm, stat, writeFile } from 'fs/promises';
import { availableParallelism, tmpdir, totalmem } from 'os';
import { join } from 'path';
-import { PATHS } from '../../lib/fileUtils.js';
+import { PATHS, safeJSONParse } from '../../lib/fileUtils.js';
import { ServerError } from '../../lib/errorHandler.js';
import { AAC_ENCODE_ARGS, runFfmpegProcess, supportsSetparamsFilter } from '../../lib/ffmpeg.js';
import { filterGraphViaFile } from '../../lib/ffmpegFilterGraph.js';
@@ -52,15 +52,21 @@ const KEY_MARGIN_SEC = 5;
const SEGMENT_TAG = 'setparams=range=tv:color_primaries=bt709:color_trc=bt709:colorspace=bt709';
const hash = (value) => createHash('sha256').update(JSON.stringify(value)).digest('hex').slice(0, 32);
+// A segment's film-look file is keyed by its base file, its place in the song
+// and the (normalized) look, so any look's files can be found again from the plan.
+const segmentLookKey = ({ baseKey, startSec }, look, fps) => hash({ v: SHOT_SEGMENT_VERSION, baseKey, look, startSec, fps });
+const segmentLookPath = (plan, segment, look) => join(plan.dir, `${segment.outro ? 'outro-' : ''}${segment.baseKey}-${segmentLookKey(segment, look, plan.fps)}.mp4`);
const safeProjectId = (projectId) => {
if (typeof projectId !== 'string' || !/^[A-Za-z0-9_-]{1,128}$/.test(projectId)) throw new ServerError('Invalid project id', { status: 400, code: 'VALIDATION_ERROR' });
return projectId;
};
export const shotSegmentDir = (projectId) => join(PATHS.data, SHOT_SEGMENT_DIR, safeProjectId(projectId));
+const hasOwnLook = (scene) => !!(scene?.filmLook && typeof scene.filmLook === 'object');
+
/** A shot's own pinned look, else the video's; null when the shot plays clean. Pure. */
function shotFilmLook(project, scene) {
- const own = scene?.filmLook && typeof scene.filmLook === 'object' ? scene.filmLook : null;
+ const own = hasOwnLook(scene) ? scene.filmLook : null;
const look = normalizeFilmLook(own || project?.filmLook);
return look && !isFilmLookNeutral(look) ? look : null;
}
@@ -131,11 +137,12 @@ export async function planShotSegments(project) {
...(overlayPages.some((p) => p.name === 'lyrics') ? { lyrics: windowedKeyData({ ...lyricData, render: nearEnd ? lyricData.render : null }, lo, hi) } : {}),
};
const baseKey = hash({ v: SHOT_SEGMENT_VERSION, args: await fingerprintArgs(built.args), overlays: overlayKey, width, height, fps });
- const look = shotFilmLook(project, scenes.get(built.sceneId));
- const lookKey = look ? hash({ v: SHOT_SEGMENT_VERSION, baseKey, look, startSec: built.startSec, fps }) : null;
+ const scene = scenes.get(built.sceneId);
+ const look = shotFilmLook(project, scene);
+ const lookKey = look ? segmentLookKey({ baseKey, startSec: built.startSec }, look, fps) : null;
segments.push({
index, sceneId: built.sceneId, layer: built.layer, startSec: built.startSec, endSec: built.endSec,
- startFrame, frames: built.frames, window, segment, look, baseKey, lookKey,
+ startFrame, frames: built.frames, window, segment, look, baseKey, lookKey, ownLook: hasOwnLook(scene),
basePath: join(dir, `${baseKey}.mp4`),
path: join(dir, lookKey ? `${baseKey}-${lookKey}.mp4` : `${baseKey}.mp4`),
});
@@ -156,7 +163,7 @@ async function planOutroSegment(project, { dir, width, height, fps, offsetSec })
const stats = await getMusicVideoOutroStats(project);
const baseKey = hash({ v: SHOT_SEGMENT_VERSION, outro, stats, width, height, fps });
const look = shotFilmLook(project, null);
- const lookKey = look ? hash({ v: SHOT_SEGMENT_VERSION, baseKey, look, startSec: offsetSec, fps }) : null;
+ const lookKey = look ? segmentLookKey({ baseKey, startSec: offsetSec }, look, fps) : null;
return {
outro: true, stats, startSec: offsetSec, durationSec: outro.durationSec, frames: Math.round(outro.durationSec * fps), look, baseKey, lookKey,
basePath: join(dir, `outro-${baseKey}.mp4`),
@@ -247,6 +254,8 @@ async function renderSegmentFiles(plan, segment, { signal, onProgress }) {
label: `Music-video film look ${segment.outro ? 'ending card' : `shot ${segment.sceneId.slice(0, 8)}`}`,
onProgress: (f) => onProgress?.(1 - lookShare + f * lookShare) });
await rename(partial, segment.path);
+ // A shot rendered from its card lists its look too, so it can be deleted later.
+ if (!segment.ownLook) await rememberFilmLook(plan.dir, segment.look);
} catch (err) {
await rm(partial, { force: true }).catch(() => {});
if (signal.aborted) throw Object.assign(new Error('Render cancelled'), { code: 'CANCELED' });
@@ -279,23 +288,34 @@ async function renderShotBase(plan, segment, outputPath, { jobId, signal, onProg
}
}
-// Each segment in flight may hold a film-look browser (about one core, since
-// the filter rasterizes serially) and an encoder. Two thirds of the cores keep
-// the server and the user's own apps responsive; the memory term keeps a small
-// machine from running out of RAM with a browser per shot.
-const SEGMENT_MAX_CONCURRENCY = 12;
+// Each segment in flight may hold a film-look browser and an encoder. Browsers
+// seeking video in parallel contend for the same decoder and compositor, so
+// past a few they slow each other down instead of adding throughput: twelve
+// on an 18-core Mac finished fewer shots than three and timed out seeks. The
+// memory term keeps a small machine from running out of RAM with a browser per
+// shot. PORTOS_MUSIC_VIDEO_SEGMENT_WORKERS overrides the count.
+const SEGMENT_MAX_CONCURRENCY = 4;
const SEGMENT_BYTES_EACH = 3 * 1024 ** 3;
/** How many segments render at once: each may hold a browser and an encoder. */
-export const segmentConcurrency = ({ cores = availableParallelism(), memoryBytes = totalmem() } = {}) => Math.max(1, Math.min(
- SEGMENT_MAX_CONCURRENCY, Math.floor((cores * 2) / 3), Math.floor(memoryBytes / SEGMENT_BYTES_EACH),
-));
+export const segmentConcurrency = ({ cores = availableParallelism(), memoryBytes = totalmem(), override = process.env.PORTOS_MUSIC_VIDEO_SEGMENT_WORKERS } = {}) => {
+ const forced = Number.parseInt(override, 10);
+ if (Number.isInteger(forced) && forced > 0) return Math.min(forced, 32);
+ return Math.max(1, Math.min(SEGMENT_MAX_CONCURRENCY, Math.floor((cores * 2) / 3), Math.floor(memoryBytes / SEGMENT_BYTES_EACH)));
+};
+
+// A shot whose render fails (a stalled browser, an overloaded host) is tried
+// again after the rest of the queue before the whole render fails.
+const SEGMENT_ATTEMPTS = 2;
/**
* Render every missing segment of `plan` (`concurrency` at a time). `onProgress`
* reports the share of frames ready, counting cached ones as done.
*/
export async function renderMissingSegments(plan, { signal, onProgress, concurrency = segmentConcurrency() } = {}) {
+ // The video's look is listed even when every one of its shots was cached already.
+ const videoLook = videoLookSegments(plan).find((s) => s.look)?.look;
+ if (videoLook) await rememberFilmLook(plan.dir, videoLook);
const all = [...plan.segments, ...(plan.outro ? [plan.outro] : [])];
const total = all.reduce((sum, s) => sum + s.frames, 0) || 1;
const done = new Map(all.map((s) => [s, existsSync(s.path) ? 1 : 0]));
@@ -303,9 +323,11 @@ export async function renderMissingSegments(plan, { signal, onProgress, concurre
report();
const queue = all.filter((s) => done.get(s) < 1);
const failures = [];
+ const attempts = new Map();
const worker = async () => {
while (queue.length && !signal?.aborted) {
const segment = queue.shift();
+ attempts.set(segment, (attempts.get(segment) || 0) + 1);
try {
await renderSegment(plan, segment, { signal, onProgress: (f) => { done.set(segment, f); report(); } });
done.set(segment, 1);
@@ -314,6 +336,13 @@ export async function renderMissingSegments(plan, { signal, onProgress, concurre
if (segment.sceneId) emitShotRender({ projectId: plan.project?.id, sceneId: segment.sceneId, state: 'rendered', progress: 1 });
} catch (err) {
if (signal?.aborted || err.code === 'CANCELED') throw err;
+ done.set(segment, 0);
+ report();
+ if (attempts.get(segment) < SEGMENT_ATTEMPTS) {
+ console.warn(`⚠️ ${err.message}; trying it again after the other shots`);
+ queue.push(segment);
+ continue;
+ }
failures.push(err);
queue.length = 0;
}
@@ -328,16 +357,17 @@ export async function renderMissingSegments(plan, { signal, onProgress, concurre
/**
* Join the cached segments by stream copy and lay the song (with any sound
* bed) under them once, silence under the outro, into `outputPath`. Resolves
- * `{ durationSec, songSec }`.
+ * `{ durationSec, songSec }`. `segments` (default: all, in order) joins a
+ * leading run of them instead, with the song under just that part.
*/
-export async function stitchSegments(plan, outputPath, { signal } = {}) {
- const parts = [...plan.segments, ...(plan.outro ? [plan.outro] : [])].map((s) => s.path);
+export async function stitchSegments(plan, outputPath, { signal, segments = [...plan.segments, ...(plan.outro ? [plan.outro] : [])] } = {}) {
+ const parts = segments.map((s) => s.path);
const missing = parts.filter((p) => !existsSync(p));
if (missing.length) throw new ServerError('A shot segment went missing before stitching — render again', { status: 409, code: 'SHOT_SEGMENT_MISSING' });
const listPath = `${outputPath}.segments.txt`;
const silent = `${outputPath}.silent.mp4`;
- const songSec = plan.songSec;
- const durationSec = songSec + (plan.outro ? plan.outro.frames / plan.fps : 0);
+ const songSec = segments.filter((s) => !s.outro).reduce((sum, s) => sum + s.frames, 0) / plan.fps;
+ const durationSec = songSec + segments.filter((s) => s.outro).reduce((sum, s) => sum + s.frames, 0) / plan.fps;
try {
await writeFile(listPath, parts.map((p) => `file '${p.replaceAll("'", "'\\''")}'`).join('\n'));
const joined = await runFfmpegProcess({ bin: plan.ffmpeg, signal, args: ['-hide_banner', '-loglevel', 'error', '-f', 'concat', '-safe', '0', '-i', listPath,
@@ -370,15 +400,29 @@ export async function stitchSegments(plan, outputPath, { signal } = {}) {
}
}
-/** Remove cached files the current plan no longer uses (older takes of each shot and look). */
+/**
+ * Remove cached files the current plan no longer uses: older takes of each
+ * shot, with every look rendered over them. Each listed video look over a
+ * current take stays, so a look can be restored without rendering
+ * (deleteFilmLook drops one); a look that isn't listed could never be found
+ * again, so its files go. The rendered-so-far previews are replaced by their
+ * own builder.
+ */
export async function pruneSegments(plan) {
const keep = new Set([...plan.segments, ...(plan.outro ? [plan.outro] : [])].flatMap((s) => [s.path, s.basePath]));
+ // Only takes that play the video's look keep its other looks; a shot's own
+ // look keeps just its current file, as before.
+ await lookWrites.get(plan.dir);
+ const listed = Object.values(await readFilmLooks(plan.dir)).map((entry) => entry?.look).filter(Boolean);
+ for (const segment of videoLookSegments(plan)) {
+ for (const look of listed) keep.add(segmentLookPath(plan, segment, look));
+ }
const names = await readdir(plan.dir).catch(() => []);
let removed = 0;
for (const name of names) {
const path = join(plan.dir, name);
if (keep.has(path) || [...inFlight.keys()].some((p) => path.startsWith(p))) continue;
- if (name.endsWith('.partial.mp4')) continue;
+ if (name.endsWith('.partial.mp4') || name.startsWith(LOOKS_FILE) || name.startsWith('preview-')) continue;
await rm(path, { force: true }).then(() => { removed += 1; }, () => {});
}
return removed;
@@ -525,7 +569,177 @@ export function cancelShotRender(projectId, sceneId) {
/** A cached segment file of `projectId` by name, or null when the name is not one or it is gone. */
export function shotSegmentFile(projectId, name) {
- if (typeof name !== 'string' || !/^(outro-)?[0-9a-f]{32}(-[0-9a-f]{32})?\.mp4$/.test(name)) return null;
+ if (typeof name !== 'string' || !/^(?:(outro-)?[0-9a-f]{32}(-[0-9a-f]{32})?|preview-[0-9a-f]{32}(-light)?)\.mp4$/.test(name)) return null;
const path = join(shotSegmentDir(projectId), name);
return existsSync(path) ? path : null;
}
+
+// ── Film looks kept side by side ────────────────────────────────────────────
+// Every video-wide look rendered over the current takes stays cached until it
+// is deleted, so switching back to one only stitches. `looks.json` remembers
+// each look's settings (its files are found again from the plan).
+const LOOKS_FILE = 'looks.json';
+const filmLookId = (look) => hash({ look });
+const lookWrites = new Map();
+
+async function readFilmLooks(dir) {
+ const raw = await readFile(join(dir, LOOKS_FILE), 'utf8').catch(() => null);
+ const parsed = raw ? safeJSONParse(raw, null, { allowArray: false }) : null;
+ return parsed?.looks && typeof parsed.looks === 'object' ? parsed.looks : {};
+}
+
+// One read-modify-write at a time per project: shots finish in parallel.
+function updateFilmLooks(dir, change) {
+ const run = (lookWrites.get(dir) || Promise.resolve()).then(async () => {
+ const looks = await readFilmLooks(dir);
+ if (change(looks) === false) return;
+ await mkdir(dir, { recursive: true });
+ const tmp = join(dir, `${LOOKS_FILE}.${randomUUID()}.tmp`);
+ await writeFile(tmp, JSON.stringify({ looks }, null, 2));
+ await rename(tmp, join(dir, LOOKS_FILE));
+ });
+ const settled = run.catch((err) => console.warn(`⚠️ Music-video film look list not saved: ${err.message}`));
+ lookWrites.set(dir, settled);
+ return settled;
+}
+
+const rememberFilmLook = (dir, look) => updateFilmLooks(dir, (looks) => {
+ const id = filmLookId(look);
+ if (looks[id]) return false;
+ looks[id] = { look, savedAt: new Date().toISOString() };
+ return true;
+});
+
+const videoLookSegments = (plan) => [...plan.segments.filter((s) => !s.ownLook), ...(plan.outro ? [plan.outro] : [])];
+
+async function planForLooks(projectId) {
+ const project = await getProject(projectId);
+ if (!project) throw new ServerError('Project not found', { status: 404, code: 'NOT_FOUND' });
+ if (project.composition?.mode !== 'composed') return { project, plan: null };
+ const plan = await planShotSegments(project).catch((err) => {
+ if (!err?.status || err.status >= 500) throw err;
+ return { error: { message: err.message, code: err.code || null } };
+ });
+ return { project, plan };
+}
+
+/**
+ * The video-wide film looks cached over the current takes: each one's
+ * settings, how many of the shots that use the video's look have it rendered,
+ * and the disk it holds. The look in use comes first.
+ */
+export async function listFilmLooks(projectId) {
+ const { project, plan } = await planForLooks(projectId);
+ if (!plan) return { available: false, looks: [], total: 0 };
+ if (plan.error) return { available: true, error: plan.error, looks: [], total: 0 };
+ const stored = await readFilmLooks(plan.dir);
+ const current = normalizeFilmLook(project.filmLook);
+ const currentId = current && !isFilmLookNeutral(current) ? filmLookId(current) : null;
+ if (currentId && !stored[currentId]) {
+ await rememberFilmLook(plan.dir, current);
+ stored[currentId] = { look: current, savedAt: new Date().toISOString() };
+ }
+ const segments = videoLookSegments(plan);
+ const looks = [];
+ for (const [id, entry] of Object.entries(stored)) {
+ const look = normalizeFilmLook(entry?.look);
+ if (!look || isFilmLookNeutral(look)) continue;
+ let rendered = 0;
+ let bytes = 0;
+ for (const segment of segments) {
+ const info = await stat(segmentLookPath(plan, segment, look)).catch(() => null);
+ if (info) { rendered += 1; bytes += info.size; }
+ }
+ if (!rendered && id !== currentId) continue;
+ looks.push({ id, look, current: id === currentId, rendered, bytes, savedAt: entry.savedAt || null });
+ }
+ looks.sort((a, b) => Number(b.current) - Number(a.current) || String(b.savedAt).localeCompare(String(a.savedAt)));
+ return { available: true, looks, total: segments.length };
+}
+
+/** Delete one cached look's files over the current takes, and forget it. */
+export async function deleteFilmLook(projectId, lookId) {
+ if (typeof lookId !== 'string' || !/^[0-9a-f]{32}$/.test(lookId)) throw new ServerError('Invalid film look id', { status: 400, code: 'VALIDATION_ERROR' });
+ const { project, plan } = await planForLooks(projectId);
+ if (!plan || plan.error) throw new ServerError('This video has no shot renders to delete', { status: 409, code: 'NO_SHOT_RENDERS' });
+ if ([...inFlight.keys()].some((path) => path.startsWith(plan.dir))) {
+ throw new ServerError('Shots of this video are rendering. Delete the look after they finish, or stop the render', { status: 409, code: 'SHOT_RENDER_ACTIVE' });
+ }
+ const entry = (await readFilmLooks(plan.dir))[lookId];
+ const current = normalizeFilmLook(project.filmLook);
+ const look = normalizeFilmLook(entry?.look) || (current && filmLookId(current) === lookId ? current : null);
+ let removed = 0;
+ if (look) {
+ for (const segment of videoLookSegments(plan)) {
+ await rm(segmentLookPath(plan, segment, look)).then(() => { removed += 1; }, () => {});
+ }
+ }
+ await updateFilmLooks(plan.dir, (looks) => {
+ if (!looks[lookId]) return false;
+ delete looks[lookId];
+ return true;
+ });
+ if (removed) console.log(`🗑️ Music-video film look [${projectId.slice(0, 8)}] deleted: ${removed} shot files`);
+ return listFilmLooks(projectId);
+}
+
+// ── Rendered so far ────────────────────────────────────────────────────────
+// The shots finished so far, stitched in order up to the first one still
+// missing, with the song under them: a seam check while the rest renders.
+// `light` adds a lighter encode for a phone (grain makes the master heavy).
+const previewBuilds = new Map();
+const LIGHT_ENCODE_ARGS = ['-c:v', 'libx264', '-preset', 'veryfast', '-crf', '26', '-maxrate', '5M', '-bufsize', '10M', '-pix_fmt', 'yuv420p'];
+
+export async function buildRenderPreview(projectId, { light = false } = {}) {
+ const { plan } = await planForLooks(projectId);
+ if (!plan) throw new ServerError('Rendered-so-far playback needs the composed render style', { status: 409, code: 'SHOT_RENDER_NEEDS_COMPOSED' });
+ if (plan.error) throw new ServerError(plan.error.message, { status: 409, code: plan.error.code || 'SHOT_RENDER_REFUSED' });
+ const lead = [];
+ for (const segment of plan.segments) {
+ if (!segmentRendered(segment)) break;
+ lead.push(segment);
+ }
+ if (lead.length === plan.segments.length && plan.outro && segmentRendered(plan.outro)) lead.push(plan.outro);
+ if (!lead.length) throw new ServerError('No shot is rendered yet: the first shot plays here once it is done', { status: 409, code: 'NO_SHOTS_RENDERED' });
+ const key = hash({ parts: lead.map((s) => s.path), audio: plan.audioPath, bed: plan.soundBed || null });
+ const name = `preview-${key}${light ? '-light' : ''}.mp4`;
+ const path = join(plan.dir, name);
+ const shots = new Set(lead.filter((s) => !s.outro).map((s) => s.sceneId)).size;
+ const result = {
+ name, light,
+ durationSec: lead.reduce((sum, s) => sum + s.frames, 0) / plan.fps,
+ shots, totalShots: new Set(plan.segments.map((s) => s.sceneId)).size,
+ complete: lead.length === plan.segments.length + (plan.outro ? 1 : 0),
+ };
+ if (existsSync(path)) return result;
+ let build = previewBuilds.get(path);
+ if (!build) {
+ build = writePreview(plan, lead, path, { light }).finally(() => previewBuilds.delete(path));
+ previewBuilds.set(path, build);
+ }
+ await build;
+ return result;
+}
+
+async function writePreview(plan, lead, path, { light }) {
+ const jobId = randomUUID();
+ const full = `${path}.${jobId}.full.mp4`;
+ const partial = `${path}.${jobId}.partial.mp4`;
+ try {
+ await stitchSegments(plan, light ? full : partial, { segments: lead });
+ if (light) {
+ const encoded = await runFfmpegProcess({ bin: plan.ffmpeg, args: ['-hide_banner', '-loglevel', 'error', '-i', full,
+ '-vf', "scale=-2:'min(720,ih)'", ...LIGHT_ENCODE_ARGS, '-c:a', 'copy', '-movflags', '+faststart', '-y', partial] });
+ if (!encoded.ok) throw new ServerError(`The phone copy failed: ${encoded.reason}`, { status: 500, code: 'SHOT_PREVIEW_ENCODE' });
+ }
+ // Only the newest preview of each kind is kept.
+ const variant = light ? /^preview-[0-9a-f]{32}-light\.mp4$/ : /^preview-[0-9a-f]{32}\.mp4$/;
+ for (const old of await readdir(plan.dir).catch(() => [])) {
+ if (variant.test(old)) await rm(join(plan.dir, old), { force: true }).catch(() => {});
+ }
+ await rename(partial, path);
+ } finally {
+ await rm(full, { force: true }).catch(() => {});
+ await rm(partial, { force: true }).catch(() => {});
+ }
+}
diff --git a/server/services/musicVideo/shotSegments.test.js b/server/services/musicVideo/shotSegments.test.js
index 995087971a..85b4e71932 100644
--- a/server/services/musicVideo/shotSegments.test.js
+++ b/server/services/musicVideo/shotSegments.test.js
@@ -6,7 +6,7 @@
*/
import { describe, it, expect, vi, afterAll, beforeAll } from 'vitest';
import { execFileSync } from 'child_process';
-import { existsSync, mkdirSync } from 'fs';
+import { existsSync, mkdirSync, rmSync } from 'fs';
import { join } from 'path';
import { cleanupTempDataRoots, lazyTempDataRoot, makePathsProxy } from '../../lib/mockPathsDataRoot.js';
@@ -25,9 +25,15 @@ vi.mock('./render.js', async (importOriginal) => {
};
});
-const { planShotSegments, renderMissingSegments, stitchSegments } = await import('./shotSegments.js');
+// The look pass draws in a browser; here it keeps the base picture, which is
+// enough to follow which look file each segment lands in.
+vi.mock('./filmLookFinish.js', () => ({ applyFilmLookFinish: vi.fn(async () => true) }));
+vi.mock('./projects.js', () => ({ getProject: vi.fn(async (id) => (id === fixture.project?.id ? fixture.project : null)) }));
+
+const { planShotSegments, renderMissingSegments, renderSegment, stitchSegments, pruneSegments, listFilmLooks, deleteFilmLook, buildRenderPreview } = await import('./shotSegments.js');
const { buildMusicVideoFfmpegArgs } = await import('./render.js');
const { musicVideoEvents } = await import('./events.js');
+const { applyFilmLookFinish } = await import('./filmLookFinish.js');
afterAll(cleanupTempDataRoots);
@@ -117,4 +123,106 @@ describe.skipIf(!hasFfmpeg)('shot segments', () => {
const clean = await planShotSegments(project({ scenes: scenes.map((s) => (s.sceneId === 'c' ? { ...s, filmLook: { preset: 'none' } } : s)), filmLook: look }));
expect(clean.segments[2].path).toBe(clean.segments[2].basePath);
}, 60000);
+
+ it('keeps each film look to swap back to, plays what is rendered so far, and deletes a look', async () => {
+ const lookA = { preset: 'vhs', grain: 0.3 };
+ const lookB = { preset: 'super8', grain: 0.6 };
+ fixture.project = project({ filmLook: lookA });
+ const planA = await planShotSegments(fixture.project);
+ await renderMissingSegments(planA, { concurrency: 2 });
+
+ const whole = await buildRenderPreview(fixture.project.id);
+ expect(whole).toMatchObject({ shots: 3, totalShots: 3, complete: true });
+ expect(whole.durationSec).toBeCloseTo(3, 5);
+ const wholePath = join(planA.dir, whole.name);
+ expect(probe(wholePath, 'stream=codec_type').sort()).toEqual(['audio', 'video']);
+
+ fixture.project = project({ filmLook: lookB });
+ const planB = await planShotSegments(fixture.project);
+ await renderMissingSegments(planB, { concurrency: 2 });
+ await pruneSegments(planB);
+ // The other look's files survive the prune: switching back only stitches.
+ expect(planA.segments.every((s) => existsSync(s.path))).toBe(true);
+ const listed = await listFilmLooks(fixture.project.id);
+ expect(listed.total).toBe(3);
+ expect(listed.looks.map((l) => [l.look.preset, l.current, l.rendered])).toEqual([['super8', true, 3], ['vhs', false, 3]]);
+
+ const left = await deleteFilmLook(fixture.project.id, listed.looks[1].id);
+ expect(left.looks.map((l) => l.look.preset)).toEqual(['super8']);
+ expect(planA.segments.some((s) => existsSync(s.path))).toBe(false);
+ expect(planB.segments.every((s) => existsSync(s.path))).toBe(true);
+
+ // A missing shot in the middle: the player stops before it.
+ rmSync(planB.segments[1].path);
+ const partial = await buildRenderPreview(fixture.project.id);
+ expect(partial).toMatchObject({ shots: 1, totalShots: 3, complete: false });
+ expect(partial.durationSec).toBeCloseTo(1, 5);
+ // Only the newest preview is kept.
+ expect(existsSync(wholePath)).toBe(false);
+ expect(probe(join(planB.dir, partial.name), 'stream=nb_read_frames')).toContain('24');
+ }, 60000);
+
+ it('deletes the look in use from a cache made before looks were listed, and drops a pinned shot\'s old look', async () => {
+ fixture.project = project({ filmLook: { preset: 'polaroid', grain: 0.4 } });
+ const plan = await planShotSegments(fixture.project);
+ await renderMissingSegments(plan, { concurrency: 2 });
+ rmSync(join(plan.dir, 'looks.json'));
+ const listed = await listFilmLooks(fixture.project.id);
+ const inUse = listed.looks.find((l) => l.current);
+ expect(inUse.rendered).toBe(3);
+ rmSync(join(plan.dir, 'looks.json'));
+ const left = await deleteFilmLook(fixture.project.id, inUse.id);
+ expect(plan.segments.some((s) => existsSync(s.path))).toBe(false);
+ expect(left.looks.map((l) => l.rendered)).toEqual([0]);
+
+ // A shot's own look keeps only its current file once it changes.
+ const pin = (grain) => project({ scenes: project().scenes.map((sc) => (sc.sceneId === 'c' ? { ...sc, filmLook: { preset: 'polaroid', grain } } : sc)) });
+ const before = await planShotSegments(pin(0.2));
+ await renderMissingSegments(before, { concurrency: 2 });
+ const after = await planShotSegments(pin(0.5));
+ await renderMissingSegments(after, { concurrency: 2 });
+ await pruneSegments(after);
+ expect(existsSync(before.segments[2].path)).toBe(false);
+ expect(existsSync(after.segments[2].path)).toBe(true);
+ }, 60000);
+
+ it('tries a shot whose film look failed again before failing the render', async () => {
+ fixture.project = project({ id: 'mv-segments-retry-test', filmLook: { preset: 'vhs', grain: 0.7 } });
+ const plan = await planShotSegments(fixture.project);
+ applyFilmLookFinish.mockRejectedValueOnce(new Error('render seek timed out @ 0.021s'));
+ await renderMissingSegments(plan, { concurrency: 1 });
+ expect(plan.segments.every((s) => existsSync(s.path))).toBe(true);
+
+ const again = await planShotSegments(project({ id: 'mv-segments-retry-test', filmLook: { preset: 'vhs', grain: 0.8 } }));
+ // A shot that fails both times fails the render.
+ applyFilmLookFinish.mockClear().mockRejectedValue(new Error('stalled'));
+ await expect(renderMissingSegments(again, { concurrency: 1 })).rejects.toThrow(/stalled/);
+ expect(applyFilmLookFinish).toHaveBeenCalledTimes(4);
+ applyFilmLookFinish.mockResolvedValue(true);
+ }, 60000);
+
+ it('lists a look rendered only shot by shot, and drops an un-pinned shot\'s old look', async () => {
+ const id = 'mv-segments-shot-test';
+ const lookA = { preset: 'vhs', grain: 0.1 };
+ fixture.project = project({ id, filmLook: lookA });
+ const planA = await planShotSegments(fixture.project);
+ await renderSegment(planA, planA.segments[0]);
+ fixture.project = project({ id, filmLook: { preset: 'super8', grain: 0.1 } });
+ const planB = await planShotSegments(fixture.project);
+ await renderSegment(planB, planB.segments[0]);
+ const listed = await listFilmLooks(id);
+ expect(listed.looks.map((l) => [l.look.preset, l.rendered])).toEqual([['super8', 1], ['vhs', 1]]);
+ await pruneSegments(planB);
+ expect(existsSync(planA.segments[0].path)).toBe(true);
+
+ // A shot's own look was never listed, so once the shot is un-pinned its file goes.
+ const pinnedScenes = project().scenes.map((sc) => (sc.sceneId === 'c' ? { ...sc, filmLook: { preset: 'polaroid', grain: 0.2 } } : sc));
+ const pinned = await planShotSegments(project({ id, scenes: pinnedScenes, filmLook: fixture.project.filmLook }));
+ await renderSegment(pinned, pinned.segments[2]);
+ expect(pinned.segments[2].path).not.toBe(pinned.segments[2].basePath);
+ const unpinned = await planShotSegments(fixture.project);
+ await pruneSegments(unpinned);
+ expect(existsSync(pinned.segments[2].path)).toBe(false);
+ expect(existsSync(pinned.segments[2].basePath)).toBe(true);
+ }, 60000);
});