2026-04-07 21:58:19 -04:00
|
|
|
import { join } from 'path';
|
2026-05-15 21:26:16 -04:00
|
|
|
import { unlink } from 'fs/promises'
|
2026-04-07 21:58:19 -04:00
|
|
|
import { tmpdir } from 'os';
|
2026-05-15 21:26:16 -04:00
|
|
|
import { Queue } from 'bullmq';
|
2026-04-07 21:58:19 -04:00
|
|
|
import { query } from '../db/client.js';
|
|
|
|
|
import { downloadFromS3, uploadToS3 } from '../s3/client.js';
|
|
|
|
|
import { transcodeVideo } from '../ffmpeg/executor.js';
|
|
|
|
|
|
|
|
|
|
const S3_BUCKET = process.env.S3_BUCKET || 'wild-dragon';
|
|
|
|
|
|
2026-05-15 21:26:16 -04:00
|
|
|
// Dispatch thumbnail job once proxy is ready — same Redis connection as the worker
|
|
|
|
|
const parseRedisUrl = (url) => {
|
|
|
|
|
const parsed = new URL(url);
|
|
|
|
|
return { host: parsed.hostname, port: parseInt(parsed.port, 10) };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const thumbnailQueue = new Queue('thumbnail', {
|
|
|
|
|
connection: parseRedisUrl(process.env.REDIS_URL || 'redis://queue:6379'),
|
|
|
|
|
});
|
|
|
|
|
|
2026-04-07 21:58:19 -04:00
|
|
|
export const proxyWorker = async (job) => {
|
|
|
|
|
const { assetId, inputKey, outputKey } = job.data;
|
|
|
|
|
|
|
|
|
|
const tmpDir = tmpdir();
|
2026-05-15 21:26:16 -04:00
|
|
|
const inputPath = join(tmpDir, `proxy-input-${job.id}.mov`);
|
2026-04-07 21:58:19 -04:00
|
|
|
const outputPath = join(tmpDir, `proxy-output-${job.id}.mp4`);
|
|
|
|
|
|
|
|
|
|
try {
|
2026-05-15 21:26:16 -04:00
|
|
|
// Download original from S3
|
2026-05-16 00:29:50 -04:00
|
|
|
await job.updateProgress(10);
|
2026-04-07 21:58:19 -04:00
|
|
|
console.log(`[proxy] Downloading ${inputKey} for asset ${assetId}`);
|
|
|
|
|
await downloadFromS3(S3_BUCKET, inputKey, inputPath);
|
|
|
|
|
|
2026-05-15 21:26:16 -04:00
|
|
|
// Transcode to H.264 proxy
|
2026-05-16 00:29:50 -04:00
|
|
|
await job.updateProgress(30);
|
2026-04-07 21:58:19 -04:00
|
|
|
console.log(`[proxy] Transcoding asset ${assetId}`);
|
|
|
|
|
await transcodeVideo(inputPath, outputPath, {
|
2026-05-15 21:26:16 -04:00
|
|
|
videoCodec: 'libx264',
|
|
|
|
|
videoPreset: 'fast',
|
2026-04-07 21:58:19 -04:00
|
|
|
videoBitrate: '10M',
|
2026-05-15 21:26:16 -04:00
|
|
|
audioCodec: 'aac',
|
2026-04-07 21:58:19 -04:00
|
|
|
audioBitrate: '192k',
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-15 21:26:16 -04:00
|
|
|
// Upload proxy to S3
|
2026-05-16 00:29:50 -04:00
|
|
|
await job.updateProgress(70);
|
2026-04-07 21:58:19 -04:00
|
|
|
console.log(`[proxy] Uploading to ${outputKey}`);
|
|
|
|
|
await uploadToS3(S3_BUCKET, outputKey, outputPath);
|
|
|
|
|
|
2026-05-16 00:29:50 -04:00
|
|
|
// Update asset record — proxy ready, status stays processing until thumbnail done
|
|
|
|
|
await job.updateProgress(90);
|
2026-04-07 21:58:19 -04:00
|
|
|
await query(
|
2026-05-15 21:26:16 -04:00
|
|
|
`UPDATE assets SET proxy_s3_key = $1, updated_at = NOW() WHERE id = $2`,
|
|
|
|
|
[outputKey, assetId]
|
2026-04-07 21:58:19 -04:00
|
|
|
);
|
|
|
|
|
|
2026-05-15 21:26:16 -04:00
|
|
|
// Now proxy exists in S3 — safe to queue thumbnail generation
|
|
|
|
|
const thumbnailKey = `thumbnails/${assetId}.jpg`;
|
|
|
|
|
await thumbnailQueue.add('generate', {
|
|
|
|
|
assetId,
|
|
|
|
|
proxyKey: outputKey,
|
|
|
|
|
outputKey: thumbnailKey,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
console.log(`[proxy] Asset ${assetId} proxy complete, thumbnail job queued`);
|
2026-05-16 00:29:50 -04:00
|
|
|
await job.updateProgress(100);
|
2026-04-07 21:58:19 -04:00
|
|
|
|
|
|
|
|
return { assetId, outputKey };
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(`[proxy] Error processing asset ${assetId}:`, error);
|
|
|
|
|
await query(
|
2026-05-15 21:26:16 -04:00
|
|
|
`UPDATE assets SET status = 'error', updated_at = NOW() WHERE id = $1`,
|
|
|
|
|
[assetId]
|
2026-04-07 21:58:19 -04:00
|
|
|
);
|
|
|
|
|
throw error;
|
|
|
|
|
} finally {
|
2026-05-15 21:26:16 -04:00
|
|
|
await Promise.all([
|
|
|
|
|
unlink(inputPath).catch(() => {}),
|
|
|
|
|
unlink(outputPath).catch(() => {}),
|
|
|
|
|
]);
|
2026-04-07 21:58:19 -04:00
|
|
|
}
|
|
|
|
|
};
|