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

fix: isolate local video audio preparation decisions

Adam Eivy220ca417ffa9
diff --git a/server/services/videoGen/prepareParams.js b/server/services/videoGen/prepareParams.js
index da013579f1..d6f57238d3 100644
--- a/server/services/videoGen/prepareParams.js
+++ b/server/services/videoGen/prepareParams.js
@@ -384,6 +384,96 @@ export async function prepareVideoGenParams({ body, uploads, localOnlyParamKeys
}));
}
+// Local audio is queue-owned whether supplied as an upload or copied from a
+// project song. Keep source selection and durable ownership in one step.
+async function prepareLocalAudio({ body, uploads, stageUploadDurable, stagedDurablePaths, cleanupStaged }) {
+ // Music-video a2v jobs reuse an existing library track rather than uploading
+ // the same song for every cut. Copy it into the queue-owned uploads area so
+ // the worker may safely delete its input on completion without ever touching
+ // the source track under data/music.
+ const stageExistingAudioDurable = async (sourcePath) => {
+ const ext = extname(sourcePath) || '.bin';
+ const durablePath = join(PATHS.uploads, `video-audio-${randomUUID()}${ext}`);
+ await copyFileGuarded(sourcePath, durablePath).catch(async (err) => {
+ await unlinkGuarded(durablePath).catch(() => {});
+ await cleanupStaged();
+ throw new ServerError(
+ `Failed to stage project audio: ${err.message}`,
+ { status: 500, code: 'VIDEO_GEN_AUDIO_STAGE_FAILED' },
+ );
+ });
+ stagedDurablePaths.push(durablePath);
+ return durablePath;
+ };
+
+ let audioFilePath = null;
+ if (uploads.audioFile) {
+ // a2v: audio file rides through the same durable-staging path as the
+ // image uploads. Cleanup tracking via extraUploadedTempPaths so the
+ // worker drops it on terminal events the same way it drops lastImage.
+ audioFilePath = await stageUploadDurable(uploads.audioFile, 'audio');
+ } else if (body.mode === 'a2v' && body.musicVideo) {
+ const project = await getMusicVideoProject(body.musicVideo.projectId);
+ const sceneExists = project?.scenes?.some((scene) => scene.sceneId === body.musicVideo.sceneId);
+ if (!project || !sceneExists) {
+ await cleanupStaged();
+ throw new ServerError('Music-video project or scene not found', { status: 404, code: 'NOT_FOUND' });
+ }
+ const track = project.trackId ? await getTrack(project.trackId) : null;
+ const filename = track?.audioFilename || project.uploadedAudioFilename;
+ const sourceAudioPath = filename ? safeUnder(PATHS.music, filename) : null;
+ if (!sourceAudioPath || !existsSync(sourceAudioPath)) {
+ await cleanupStaged();
+ throw new ServerError('Music-video project audio is unavailable', { status: 400, code: 'VIDEO_GEN_PROJECT_AUDIO_MISSING' });
+ }
+ audioFilePath = await stageExistingAudioDurable(sourceAudioPath);
+ }
+ return audioFilePath;
+}
+
+async function resolveAudioFrameCount({ body, audioFilePath, effectiveModel, effectiveNumFrames, cleanupStaged }) {
+ // A direct-upload LTX-2.5 A2V request follows the whole audio file. The MLX
+ // pipeline still requires an explicit 8n+1 frame canvas, so derive that canvas
+ // from the durable upload rather than trusting browser metadata (or forcing an
+ // API caller to probe the file itself). MiniMax Ref2VA is deliberately excluded:
+ // its wrapper windows arbitrary-length audio and owns its duration internally.
+ // Music-video jobs also keep their explicit scene canvas because they select a
+ // slice of a longer song with audioStartSec.
+ if (body.mode === 'a2v' && audioFilePath
+ && effectiveModel?.audioDurationDriven === true
+ && effectiveModel?.arbitraryLengthAudio !== true
+ && !body.musicVideo) {
+ const durationSeconds = await probeVideoDuration(audioFilePath);
+ if (durationSeconds == null) {
+ await cleanupStaged();
+ throw new ServerError(
+ 'Could not read the uploaded audio duration. Upload a valid WAV, MP3, M4A, AAC, FLAC, or OGG file.',
+ { status: 400, code: 'VIDEO_GEN_AUDIO_DURATION_UNREADABLE' },
+ );
+ }
+ const frameStride = Number(effectiveModel.frameStride);
+ const fps = Number(body.fps ?? 24);
+ const maxNumFrames = Number(effectiveModel.maxNumFrames);
+ if (!Number.isInteger(frameStride) || frameStride <= 0
+ || !Number.isInteger(maxNumFrames) || maxNumFrames <= 0) {
+ await cleanupStaged();
+ throw new ServerError(
+ `${effectiveModel.name} is missing its duration-driven frameStride/maxNumFrames contract.`,
+ { status: 500, code: 'VIDEO_MODEL_MISCONFIGURED' },
+ );
+ }
+ effectiveNumFrames = audioDurationToFrames(durationSeconds, fps, frameStride);
+ if (effectiveNumFrames > maxNumFrames) {
+ await cleanupStaged();
+ throw new ServerError(
+ `${effectiveModel.name} supports up to ${(maxNumFrames / fps).toFixed(1)}s in one audio-to-video render; this file is ${durationSeconds.toFixed(1)}s. Use MiniMax H3 Ref2VA for longer audio, or trim the file.`,
+ { status: 400, code: 'VIDEO_GEN_AUDIO_TOO_LONG' },
+ );
+ }
+ }
+ return effectiveNumFrames;
+}
+
/**
* The staging + resolution region, split out so `prepareVideoGenParams` can
* wrap it in a single rollback guard (#3326) without indenting 400 lines.
@@ -419,25 +509,6 @@ async function resolvePreparedParams({
return durablePath;
};
- // Music-video a2v jobs reuse an existing library track rather than uploading
- // the same song for every cut. Copy it into the queue-owned uploads area so
- // the worker may safely delete its input on completion without ever touching
- // the source track under data/music.
- const stageExistingAudioDurable = async (sourcePath) => {
- const ext = extname(sourcePath) || '.bin';
- const durablePath = join(PATHS.uploads, `video-audio-${randomUUID()}${ext}`);
- await copyFileGuarded(sourcePath, durablePath).catch(async (err) => {
- await unlinkGuarded(durablePath).catch(() => {});
- await cleanupStaged();
- throw new ServerError(
- `Failed to stage project audio: ${err.message}`,
- { status: 500, code: 'VIDEO_GEN_AUDIO_STAGE_FAILED' },
- );
- });
- stagedDurablePaths.push(durablePath);
- return durablePath;
- };
-
// Resolution precedence on each frame side: a fresh upload always wins over
// a gallery filename so users can override a stale gallery pick by dropping
// in a new file without first clearing the picker.
@@ -663,7 +734,6 @@ async function resolvePreparedParams({
let sourceImagePath = null;
let lastImagePath = null;
- let audioFilePath = null;
let icReferenceUploadPath = null;
let uploadedTempPath = null;
const extraUploadedTempPaths = [];
@@ -792,68 +862,13 @@ async function resolvePreparedParams({
// Same path-traversal guard as the start frame.
lastImagePath = resolveGalleryImage(body.lastImageFile);
}
- if (uploads.audioFile) {
- // a2v: audio file rides through the same durable-staging path as the
- // image uploads. Cleanup tracking via extraUploadedTempPaths so the
- // worker drops it on terminal events the same way it drops lastImage.
- audioFilePath = await stageUploadDurable(uploads.audioFile, 'audio');
- extraUploadedTempPaths.push(audioFilePath);
- } else if (body.mode === 'a2v' && body.musicVideo) {
- const project = await getMusicVideoProject(body.musicVideo.projectId);
- const sceneExists = project?.scenes?.some((scene) => scene.sceneId === body.musicVideo.sceneId);
- if (!project || !sceneExists) {
- await cleanupStaged();
- throw new ServerError('Music-video project or scene not found', { status: 404, code: 'NOT_FOUND' });
- }
- const track = project.trackId ? await getTrack(project.trackId) : null;
- const filename = track?.audioFilename || project.uploadedAudioFilename;
- const sourceAudioPath = filename ? safeUnder(PATHS.music, filename) : null;
- if (!sourceAudioPath || !existsSync(sourceAudioPath)) {
- await cleanupStaged();
- throw new ServerError('Music-video project audio is unavailable', { status: 400, code: 'VIDEO_GEN_PROJECT_AUDIO_MISSING' });
- }
- audioFilePath = await stageExistingAudioDurable(sourceAudioPath);
- extraUploadedTempPaths.push(audioFilePath);
- }
- // A direct-upload LTX-2.5 A2V request follows the whole audio file. The MLX
- // pipeline still requires an explicit 8n+1 frame canvas, so derive that canvas
- // from the durable upload rather than trusting browser metadata (or forcing an
- // API caller to probe the file itself). MiniMax Ref2VA is deliberately excluded:
- // its wrapper windows arbitrary-length audio and owns its duration internally.
- // Music-video jobs also keep their explicit scene canvas because they select a
- // slice of a longer song with audioStartSec.
- if (body.mode === 'a2v' && audioFilePath
- && effectiveModel?.audioDurationDriven === true
- && effectiveModel?.arbitraryLengthAudio !== true
- && !body.musicVideo) {
- const durationSeconds = await probeVideoDuration(audioFilePath);
- if (durationSeconds == null) {
- await cleanupStaged();
- throw new ServerError(
- 'Could not read the uploaded audio duration. Upload a valid WAV, MP3, M4A, AAC, FLAC, or OGG file.',
- { status: 400, code: 'VIDEO_GEN_AUDIO_DURATION_UNREADABLE' },
- );
- }
- const frameStride = Number(effectiveModel.frameStride);
- const fps = Number(body.fps ?? 24);
- const maxNumFrames = Number(effectiveModel.maxNumFrames);
- if (!Number.isInteger(frameStride) || frameStride <= 0
- || !Number.isInteger(maxNumFrames) || maxNumFrames <= 0) {
- await cleanupStaged();
- throw new ServerError(
- `${effectiveModel.name} is missing its duration-driven frameStride/maxNumFrames contract.`,
- { status: 500, code: 'VIDEO_MODEL_MISCONFIGURED' },
- );
- }
- effectiveNumFrames = audioDurationToFrames(durationSeconds, fps, frameStride);
- if (effectiveNumFrames > maxNumFrames) {
- await cleanupStaged();
- throw new ServerError(
- `${effectiveModel.name} supports up to ${(maxNumFrames / fps).toFixed(1)}s in one audio-to-video render; this file is ${durationSeconds.toFixed(1)}s. Use MiniMax H3 Ref2VA for longer audio, or trim the file.`,
- { status: 400, code: 'VIDEO_GEN_AUDIO_TOO_LONG' },
- );
- }
- }
+ const audioFilePath = await prepareLocalAudio({
+ body, uploads, stageUploadDurable, stagedDurablePaths, cleanupStaged,
+ });
+ if (audioFilePath) extraUploadedTempPaths.push(audioFilePath);
+ effectiveNumFrames = await resolveAudioFrameCount({
+ body, audioFilePath, effectiveModel, effectiveNumFrames, cleanupStaged,
+ });
if (uploads.icReference) {
// IC-LoRA reference clip — same durable staging + cleanup tracking as the
// audio upload above. A history-picked reference needs neither (it already
diff --git a/server/services/videoGen/prepareParams.test.js b/server/services/videoGen/prepareParams.test.js
index 48369237c6..0e5b24549e 100644
--- a/server/services/videoGen/prepareParams.test.js
+++ b/server/services/videoGen/prepareParams.test.js
@@ -235,6 +235,27 @@ describe('prepareVideoGenParams', () => {
]);
});
+ it('keeps the scene canvas and prefers uploaded audio over the project song', async () => {
+ listVideoModels.mockReturnValue([{
+ id: 'ltx25_mlx_q8', name: 'LTX-2.5', runtime: 'ltx25',
+ supportedModes: ['a2v'], audioDurationDriven: true,
+ frameStride: 8, maxNumFrames: 1017,
+ }]);
+ const prepared = await prepare({
+ modelId: 'ltx25_mlx_q8', mode: 'a2v', numFrames: 121,
+ sourceImageFile: 'reference.png',
+ musicVideo: { projectId: 'project-example', sceneId: 'scene-example' },
+ }, { audioFile: upload('audioFile', 'override.wav') });
+
+ expect(prepared.effectiveNumFrames).toBe(121);
+ expect(prepared.uploadedTempPaths).toEqual([prepared.audioFilePath]);
+ expect(copyFileMock).toHaveBeenCalledWith('/tmp/multipart-audioFile-override.wav', prepared.audioFilePath);
+ expect(getMusicVideoProject).not.toHaveBeenCalled();
+ expect(probeVideoDuration).not.toHaveBeenCalled();
+ await prepared.cleanupStaged();
+ expect(unlinkedDurablePaths()).toEqual([posix(prepared.audioFilePath)]);
+ });
+
it('short-circuits for grok without staging local-only inputs', async () => {
const prepared = await prepare({ backend: 'grok', sourceImageFile: 'still.png' });
expect(prepared.backend).toBe('grok');