import * as path from 'path'; import * as fs from 'fs/promises'; import { exec } from 'child_process'; import { hashBuffer } from './hash'; export async function convertVideo(video: Uint8Array) { let random = Math.floor(Math.random() * 10000000); let hash = await hashBuffer(video.buffer); const tempInPath = `/tmp/${hash}-${random}.in.mp4`; const tempOutPath = `/tmp/${hash}-${random}.out.mp4`; try { await fs.writeFile(tempInPath, video); const status = await new Promise((resolve, reject) => { const child = exec(`ffmpeg -i "${tempInPath}" -preset ultrafast -vf "scale=320:480,eq=saturation=1.4,unsharp=5:5:1.5:5:5:0.0" -b:v 420k -b:a 64k "${tempOutPath}"`, (err) => { if (err) { reject(err); return; } resolve(child.exitCode); }); }); if (status) return null; const result = await fs.readFile(tempOutPath); const resultUint8 = new Uint8Array(result); return resultUint8; } finally { await fs.unlink(tempInPath).catch((_) => {}); await fs.unlink(tempOutPath).catch((_) => {}); } }