import { spawn } from 'child_process'; const ENCTOOL = process.env['ENCTOOL'] || 'ffmpeg'; function ffmpeg(args: string[], files: number): Promise { return new Promise((resolve, reject) => { console.log(`${ENCTOOL} ${args.join(' ')}`); const process = spawn(ENCTOOL, args); const to = setTimeout(async () => { process.kill(); reject(new Error('timed out')); }, 5000 * files); process.on('exit', async (code) => { clearTimeout(to); if (code !== 0) { reject(new Error(`exited with ${code}`)); } else { resolve(); } }); }); } async function Stitcher(files: string[]) { const args: string[] = []; files.forEach(f => args.push('-i', f)); args.push('-filter_complex', `[0:a][1:a][2:a]concat=n=${files.length}:v=0:a=1[out]`); args.push('-map', '[out]', '-ar', '44100', '-ac', '2', '-c:a', 'pcm_s16le', 'output.wav', '-y'); await ffmpeg(args, files.length); } export default Stitcher; export { Stitcher };