dragonflight/services/worker/src/workers/thumbnail.js

95 lines
3.2 KiB
JavaScript
Raw Normal View History

import { join } from 'path';
import { unlink } from 'fs/promises';
import { tmpdir } from 'os';
feat: server-side filmstrip worker + fix scheduler crash + fix clip freeze Root causes found: 1. Scheduler crashing every 15s: assets table has no error_message column. Fix: remove error_message from UPDATE in scheduler.js (#66 regression). 2. Clip freezing: client-side filmstrip seek loop runs on main thread, seeks same proxy the player is streaming → both stall → freeze. Fix: replace browser seek loop entirely with server-side FFmpeg worker. 3. No dedicated filmstrip worker: filmstrip was never pre-built server-side. Changes: - services/mam-api/src/db/migrations/018-add-filmstrip-s3-key.sql Add filmstrip_s3_key TEXT column to assets table - services/worker/src/workers/filmstrip.js (new) BullMQ worker: downloads proxy, runs FFmpeg fps filter to extract 28 evenly-spaced JPEG frames, base64-encodes them, uploads JSON array to S3 at filmstrips/<assetId>.json, stores key in DB - services/worker/src/workers/thumbnail.js Queue filmstrip job automatically after thumbnail completes - services/worker/src/index.js Register filmstrip worker (concurrency=2), export filmstripQueue singleton, close it on SIGTERM - services/mam-api/src/routes/assets.js - filmstripQueue added - POST /reprocess?type=filmstrip now supported - GET /:id/filmstrip returns signed S3 URL for JSON frames - services/mam-api/src/routes/jobs.js filmstrip queue visible in Jobs UI - services/web-ui/public/screens-asset.jsx Replace browser seek loop with fetch of /assets/:id/filmstrip → fetch S3 JSON → render frames. Zero browser-side video seeking. Right-click and Files tab re-generate via API endpoint.
2026-05-26 12:39:44 -04:00
import { Queue } from 'bullmq';
import { query } from '../db/client.js';
import { downloadFromS3, uploadToS3 } from '../s3/client.js';
import { extractFrameAtTime, getMediaDuration } from '../ffmpeg/executor.js';
feat: server-side filmstrip worker + fix scheduler crash + fix clip freeze Root causes found: 1. Scheduler crashing every 15s: assets table has no error_message column. Fix: remove error_message from UPDATE in scheduler.js (#66 regression). 2. Clip freezing: client-side filmstrip seek loop runs on main thread, seeks same proxy the player is streaming → both stall → freeze. Fix: replace browser seek loop entirely with server-side FFmpeg worker. 3. No dedicated filmstrip worker: filmstrip was never pre-built server-side. Changes: - services/mam-api/src/db/migrations/018-add-filmstrip-s3-key.sql Add filmstrip_s3_key TEXT column to assets table - services/worker/src/workers/filmstrip.js (new) BullMQ worker: downloads proxy, runs FFmpeg fps filter to extract 28 evenly-spaced JPEG frames, base64-encodes them, uploads JSON array to S3 at filmstrips/<assetId>.json, stores key in DB - services/worker/src/workers/thumbnail.js Queue filmstrip job automatically after thumbnail completes - services/worker/src/index.js Register filmstrip worker (concurrency=2), export filmstripQueue singleton, close it on SIGTERM - services/mam-api/src/routes/assets.js - filmstripQueue added - POST /reprocess?type=filmstrip now supported - GET /:id/filmstrip returns signed S3 URL for JSON frames - services/mam-api/src/routes/jobs.js filmstrip queue visible in Jobs UI - services/web-ui/public/screens-asset.jsx Replace browser seek loop with fetch of /assets/:id/filmstrip → fetch S3 JSON → render frames. Zero browser-side video seeking. Right-click and Files tab re-generate via API endpoint.
2026-05-26 12:39:44 -04:00
const parseRedisUrl = (url) => {
try { const p = new URL(url); return { host: p.hostname, port: parseInt(p.port, 10) || 6379 }; }
catch { return { host: 'localhost', port: 6379 }; }
};
const filmstripQueue = new Queue('filmstrip', {
connection: parseRedisUrl(process.env.REDIS_URL || 'redis://queue:6379'),
});
const S3_BUCKET = process.env.S3_BUCKET || 'wild-dragon';
/**
* Pick a seek time that won't exceed the clip duration.
* Tries 5s first, then 1s, then 0s as a last resort.
*/
async function pickSeekTime(inputPath) {
try {
const duration = await getMediaDuration(inputPath);
if (duration >= 5) return '00:00:05';
if (duration >= 1) return '00:00:01';
return '00:00:00';
} catch {
// If ffprobe fails, fall back to 5s and let ffmpeg handle it
return '00:00:05';
}
}
export const thumbnailWorker = async (job) => {
const { assetId, proxyKey, outputKey } = job.data;
const tmpDir = tmpdir();
const inputPath = join(tmpDir, `thumb-input-${job.id}.mp4`);
const outputPath = join(tmpDir, `thumb-output-${job.id}.jpg`);
try {
// Download proxy from S3
await job.updateProgress(10);
console.log(`[thumbnail] Downloading ${proxyKey} for asset ${assetId}`);
await downloadFromS3(S3_BUCKET, proxyKey, inputPath);
// Pick a safe seek time based on actual clip duration
const seekTime = await pickSeekTime(inputPath);
// Extract frame
await job.updateProgress(40);
console.log(`[thumbnail] Extracting frame at ${seekTime} for asset ${assetId}`);
await extractFrameAtTime(inputPath, outputPath, seekTime);
// Upload thumbnail to S3
await job.updateProgress(70);
console.log(`[thumbnail] Uploading to ${outputKey}`);
await uploadToS3(S3_BUCKET, outputKey, outputPath);
// Update asset: thumbnail key + mark ready
await job.updateProgress(90);
await query(
`UPDATE assets
SET thumbnail_s3_key = $1, status = 'ready', updated_at = NOW()
WHERE id = $2`,
[outputKey, assetId]
);
await job.updateProgress(100);
console.log(`[thumbnail] Asset ${assetId} thumbnail complete → status=ready`);
feat: server-side filmstrip worker + fix scheduler crash + fix clip freeze Root causes found: 1. Scheduler crashing every 15s: assets table has no error_message column. Fix: remove error_message from UPDATE in scheduler.js (#66 regression). 2. Clip freezing: client-side filmstrip seek loop runs on main thread, seeks same proxy the player is streaming → both stall → freeze. Fix: replace browser seek loop entirely with server-side FFmpeg worker. 3. No dedicated filmstrip worker: filmstrip was never pre-built server-side. Changes: - services/mam-api/src/db/migrations/018-add-filmstrip-s3-key.sql Add filmstrip_s3_key TEXT column to assets table - services/worker/src/workers/filmstrip.js (new) BullMQ worker: downloads proxy, runs FFmpeg fps filter to extract 28 evenly-spaced JPEG frames, base64-encodes them, uploads JSON array to S3 at filmstrips/<assetId>.json, stores key in DB - services/worker/src/workers/thumbnail.js Queue filmstrip job automatically after thumbnail completes - services/worker/src/index.js Register filmstrip worker (concurrency=2), export filmstripQueue singleton, close it on SIGTERM - services/mam-api/src/routes/assets.js - filmstripQueue added - POST /reprocess?type=filmstrip now supported - GET /:id/filmstrip returns signed S3 URL for JSON frames - services/mam-api/src/routes/jobs.js filmstrip queue visible in Jobs UI - services/web-ui/public/screens-asset.jsx Replace browser seek loop with fetch of /assets/:id/filmstrip → fetch S3 JSON → render frames. Zero browser-side video seeking. Right-click and Files tab re-generate via API endpoint.
2026-05-26 12:39:44 -04:00
// Queue filmstrip generation now that the proxy is confirmed good
await filmstripQueue.add('generate', { assetId, proxyKey }).catch(err => {
console.warn(`[thumbnail] Failed to queue filmstrip for ${assetId}:`, err.message);
});
return { assetId, outputKey };
} catch (error) {
console.error(`[thumbnail] Error processing asset ${assetId}:`, error);
// Thumbnail failed but the asset is still usable via proxy — mark it ready
// so it doesn't stay in 'processing' state forever.
await query(
`UPDATE assets SET status = 'ready', updated_at = NOW() WHERE id = $1`,
[assetId]
).catch(e => console.error('[thumbnail] Failed to update asset status:', e));
throw error;
} finally {
await Promise.all([
unlink(inputPath).catch(() => {}),
unlink(outputPath).catch(() => {}),
]);
}
};