auto-lms / src /server /render /video.ts
akashdahad's picture
Deploy auto-lms (Next.js + worker + embedded Postgres)
6d6eaef verified
Raw History Blame Contribute Delete
1.27 kB
import { spawn } from 'child_process';
import ffmpegPath from 'ffmpeg-static';
// Run the bundled ffmpeg binary with the given args.
export function runFfmpeg(args: string[]): Promise<void> {
return new Promise((resolve, reject) => {
if (!ffmpegPath) {
reject(new Error('ffmpeg binary not found (ffmpeg-static)'));
return;
}
const proc = spawn(ffmpegPath, args, { stdio: ['ignore', 'ignore', 'pipe'] });
let stderr = '';
proc.stderr.on('data', (chunk) => {
stderr += chunk.toString();
});
proc.on('error', reject);
proc.on('close', (code) => {
if (code === 0) resolve();
else reject(new Error(`ffmpeg exited ${code}: ${stderr.slice(-600)}`));
});
});
}
// Loop a still image for the duration of the audio and mux them into an mp4.
export async function renderStillVideo(args: {
imagePath: string;
audioPath: string;
outPath: string;
}): Promise<void> {
await runFfmpeg([
'-y',
'-loglevel',
'error',
'-loop',
'1',
'-i',
args.imagePath,
'-i',
args.audioPath,
'-c:v',
'libx264',
'-tune',
'stillimage',
'-pix_fmt',
'yuv420p',
'-r',
'30',
'-c:a',
'aac',
'-b:a',
'192k',
'-shortest',
args.outPath,
]);
}