2026-04-07 21:58:19 -04:00
|
|
|
import { join } from 'path';
|
|
|
|
|
import { unlink } from 'fs/promises';
|
|
|
|
|
import { tmpdir } from 'os';
|
|
|
|
|
import { query } from '../db/client.js';
|
|
|
|
|
import { downloadFromS3, uploadToS3 } from '../s3/client.js';
|
2026-05-16 00:29:51 -04:00
|
|
|
import { extractFrameAtTime, getMediaDuration } from '../ffmpeg/executor.js';
|
2026-04-07 21:58:19 -04:00
|
|
|
|
|
|
|
|
const S3_BUCKET = process.env.S3_BUCKET || 'wild-dragon';
|
|
|
|
|
|
2026-05-16 00:29:51 -04:00
|
|
|
/**
|
|
|
|
|
* 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';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-07 21:58:19 -04:00
|
|
|
export const thumbnailWorker = async (job) => {
|
|
|
|
|
const { assetId, proxyKey, outputKey } = job.data;
|
|
|
|
|
|
|
|
|
|
const tmpDir = tmpdir();
|
2026-05-15 21:26:22 -04:00
|
|
|
const inputPath = join(tmpDir, `thumb-input-${job.id}.mp4`);
|
2026-04-07 21:58:19 -04:00
|
|
|
const outputPath = join(tmpDir, `thumb-output-${job.id}.jpg`);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// Download proxy from S3
|
2026-05-16 00:29:51 -04:00
|
|
|
await job.updateProgress(10);
|
2026-04-07 21:58:19 -04:00
|
|
|
console.log(`[thumbnail] Downloading ${proxyKey} for asset ${assetId}`);
|
|
|
|
|
await downloadFromS3(S3_BUCKET, proxyKey, inputPath);
|
|
|
|
|
|
2026-05-16 00:29:51 -04:00
|
|
|
// 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);
|
2026-04-07 21:58:19 -04:00
|
|
|
|
2026-05-15 21:26:22 -04:00
|
|
|
// Upload thumbnail to S3
|
2026-05-16 00:29:51 -04:00
|
|
|
await job.updateProgress(70);
|
2026-04-07 21:58:19 -04:00
|
|
|
console.log(`[thumbnail] Uploading to ${outputKey}`);
|
|
|
|
|
await uploadToS3(S3_BUCKET, outputKey, outputPath);
|
|
|
|
|
|
2026-05-15 21:26:22 -04:00
|
|
|
// Update asset: thumbnail key + mark ready
|
2026-05-16 00:29:51 -04:00
|
|
|
await job.updateProgress(90);
|
2026-04-07 21:58:19 -04:00
|
|
|
await query(
|
2026-05-15 21:26:22 -04:00
|
|
|
`UPDATE assets
|
|
|
|
|
SET thumbnail_s3_key = $1, status = 'ready', updated_at = NOW()
|
|
|
|
|
WHERE id = $2`,
|
2026-04-07 21:58:19 -04:00
|
|
|
[outputKey, assetId]
|
|
|
|
|
);
|
|
|
|
|
|
2026-05-16 00:29:51 -04:00
|
|
|
await job.updateProgress(100);
|
2026-05-15 21:26:22 -04:00
|
|
|
console.log(`[thumbnail] Asset ${assetId} thumbnail complete → status=ready`);
|
2026-04-07 21:58:19 -04:00
|
|
|
|
|
|
|
|
return { assetId, outputKey };
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(`[thumbnail] Error processing asset ${assetId}:`, error);
|
2026-05-18 23:51:04 -04:00
|
|
|
// 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));
|
2026-04-07 21:58:19 -04:00
|
|
|
throw error;
|
|
|
|
|
} finally {
|
2026-05-15 21:26:22 -04:00
|
|
|
await Promise.all([
|
|
|
|
|
unlink(inputPath).catch(() => {}),
|
|
|
|
|
unlink(outputPath).catch(() => {}),
|
|
|
|
|
]);
|
2026-04-07 21:58:19 -04:00
|
|
|
}
|
|
|
|
|
};
|