2026-04-07 21:58:19 -04:00
|
|
|
import { join } from 'path';
|
2026-05-15 23:40:13 -04:00
|
|
|
import { unlink, writeFile, mkdir } from 'fs/promises';
|
2026-04-07 21:58:19 -04:00
|
|
|
import { tmpdir } from 'os';
|
|
|
|
|
import { query } from '../db/client.js';
|
|
|
|
|
import { downloadFromS3, uploadToS3 } from '../s3/client.js';
|
|
|
|
|
import { trimSegment, concatSegments } from '../ffmpeg/executor.js';
|
|
|
|
|
import { parseEDL } from '../edl/parser.js';
|
|
|
|
|
|
|
|
|
|
const S3_BUCKET = process.env.S3_BUCKET || 'wild-dragon';
|
|
|
|
|
|
|
|
|
|
export const conformWorker = async (job) => {
|
|
|
|
|
const { jobId, edl, projectId, outputFormat } = job.data;
|
|
|
|
|
|
2026-05-15 23:40:13 -04:00
|
|
|
const tmpDir = tmpdir();
|
|
|
|
|
const edlPath = join(tmpDir, `edl-${job.id}.edl`);
|
|
|
|
|
const segmentsDir = join(tmpDir, `segments-${job.id}`);
|
2026-04-07 21:58:19 -04:00
|
|
|
const segmentListPath = join(tmpDir, `segments-${job.id}.txt`);
|
2026-05-15 23:40:13 -04:00
|
|
|
const outputPath = join(tmpDir, `output-${job.id}.${outputFormat || 'mov'}`);
|
2026-04-07 21:58:19 -04:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// Write EDL to temp file
|
|
|
|
|
await writeFile(edlPath, edl, 'utf-8');
|
|
|
|
|
|
|
|
|
|
// Parse EDL
|
2026-05-15 23:40:13 -04:00
|
|
|
await job.updateProgress(5);
|
2026-04-07 21:58:19 -04:00
|
|
|
console.log(`[conform] Parsing EDL for job ${jobId}`);
|
|
|
|
|
const edits = parseEDL(edl);
|
|
|
|
|
|
2026-05-15 23:40:13 -04:00
|
|
|
// Create temp directory for segment files
|
|
|
|
|
await mkdir(segmentsDir, { recursive: true });
|
2026-04-07 21:58:19 -04:00
|
|
|
|
|
|
|
|
let processedEdits = 0;
|
2026-05-15 23:40:13 -04:00
|
|
|
const concatList = [];
|
2026-04-07 21:58:19 -04:00
|
|
|
|
|
|
|
|
for (const edit of edits) {
|
2026-05-15 23:40:13 -04:00
|
|
|
await job.updateProgress(Math.min(5 + (processedEdits / edits.length) * 50, 55));
|
2026-04-07 21:58:19 -04:00
|
|
|
|
|
|
|
|
console.log(`[conform] Processing edit ${edit.editNumber}: ${edit.reelName}`);
|
|
|
|
|
|
2026-05-15 23:40:13 -04:00
|
|
|
// Look up asset by filename (the reel name in the EDL matches the clip filename)
|
2026-04-07 21:58:19 -04:00
|
|
|
const assetRes = await query(
|
2026-05-15 23:40:13 -04:00
|
|
|
'SELECT id, original_s3_key FROM assets WHERE filename = $1 LIMIT 1',
|
2026-04-07 21:58:19 -04:00
|
|
|
[edit.reelName]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (assetRes.rows.length === 0) {
|
|
|
|
|
throw new Error(`Asset not found for reel: ${edit.reelName}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { id: assetId, original_s3_key: sourceKey } = assetRes.rows[0];
|
2026-05-15 23:40:13 -04:00
|
|
|
const segmentInputPath = join(segmentsDir, `segment-${edit.editNumber}-src`);
|
|
|
|
|
const segmentOutputPath = join(segmentsDir, `segment-${edit.editNumber}.mov`);
|
2026-04-07 21:58:19 -04:00
|
|
|
|
2026-05-15 23:40:13 -04:00
|
|
|
// Download source clip from S3
|
|
|
|
|
console.log(`[conform] Downloading segment ${edit.editNumber} from S3 (${sourceKey})`);
|
2026-04-07 21:58:19 -04:00
|
|
|
await downloadFromS3(S3_BUCKET, sourceKey, segmentInputPath);
|
|
|
|
|
|
2026-05-15 23:40:13 -04:00
|
|
|
// Trim to EDL in/out points
|
|
|
|
|
console.log(`[conform] Trimming ${edit.editNumber}: ${edit.sourceIn} → ${edit.sourceOut}`);
|
|
|
|
|
await trimSegment(segmentInputPath, segmentOutputPath, edit.sourceIn, edit.sourceOut);
|
2026-04-07 21:58:19 -04:00
|
|
|
|
|
|
|
|
concatList.push(segmentOutputPath);
|
|
|
|
|
|
2026-05-15 23:40:13 -04:00
|
|
|
// Remove the (large) source download immediately to conserve disk
|
2026-04-07 21:58:19 -04:00
|
|
|
await unlink(segmentInputPath).catch(() => {});
|
|
|
|
|
|
|
|
|
|
processedEdits++;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-15 23:40:13 -04:00
|
|
|
// Write ffmpeg concat file
|
|
|
|
|
await job.updateProgress(60);
|
|
|
|
|
console.log(`[conform] Writing concat list for ${concatList.length} segments`);
|
|
|
|
|
const concatContent = concatList.map(p => `file '${p}'`).join('\n');
|
2026-04-07 21:58:19 -04:00
|
|
|
await writeFile(segmentListPath, concatContent, 'utf-8');
|
|
|
|
|
|
2026-05-15 23:40:13 -04:00
|
|
|
// Concatenate
|
|
|
|
|
await job.updateProgress(70);
|
2026-04-07 21:58:19 -04:00
|
|
|
console.log(`[conform] Concatenating segments for job ${jobId}`);
|
|
|
|
|
await concatSegments(segmentListPath, outputPath);
|
|
|
|
|
|
2026-05-15 23:40:13 -04:00
|
|
|
// Upload to S3
|
|
|
|
|
await job.updateProgress(85);
|
2026-04-07 21:58:19 -04:00
|
|
|
const outputKey = `jobs/${jobId}/output.${outputFormat || 'mov'}`;
|
2026-05-15 23:40:13 -04:00
|
|
|
console.log(`[conform] Uploading output to ${outputKey}`);
|
2026-04-07 21:58:19 -04:00
|
|
|
await uploadToS3(S3_BUCKET, outputKey, outputPath);
|
|
|
|
|
|
2026-05-15 23:40:13 -04:00
|
|
|
// Mark job complete
|
|
|
|
|
await job.updateProgress(95);
|
2026-04-07 21:58:19 -04:00
|
|
|
await query(
|
2026-05-15 23:40:13 -04:00
|
|
|
`UPDATE jobs SET status = 'complete', result = $1, updated_at = NOW() WHERE id = $2`,
|
|
|
|
|
[JSON.stringify({ output_key: outputKey }), jobId]
|
2026-04-07 21:58:19 -04:00
|
|
|
);
|
|
|
|
|
|
2026-05-15 23:40:13 -04:00
|
|
|
await job.updateProgress(100);
|
|
|
|
|
console.log(`[conform] Job ${jobId} complete`);
|
2026-04-07 21:58:19 -04:00
|
|
|
return { jobId, outputKey };
|
2026-05-15 23:40:13 -04:00
|
|
|
|
2026-04-07 21:58:19 -04:00
|
|
|
} catch (error) {
|
2026-05-15 23:40:13 -04:00
|
|
|
console.error(`[conform] Error in job ${jobId}:`, error);
|
2026-04-07 21:58:19 -04:00
|
|
|
await query(
|
2026-05-15 23:40:13 -04:00
|
|
|
`UPDATE jobs SET status = 'failed', error = $1, updated_at = NOW() WHERE id = $2`,
|
|
|
|
|
[error.message, jobId]
|
2026-04-07 21:58:19 -04:00
|
|
|
);
|
|
|
|
|
throw error;
|
2026-05-15 23:40:13 -04:00
|
|
|
|
2026-04-07 21:58:19 -04:00
|
|
|
} finally {
|
2026-05-15 23:40:13 -04:00
|
|
|
// Best-effort cleanup of temp files
|
|
|
|
|
await Promise.all([
|
|
|
|
|
unlink(edlPath).catch(() => {}),
|
|
|
|
|
unlink(segmentListPath).catch(() => {}),
|
|
|
|
|
unlink(outputPath).catch(() => {}),
|
|
|
|
|
]);
|
2026-04-07 21:58:19 -04:00
|
|
|
}
|
|
|
|
|
};
|