web/libav: simplify reading probe data

This commit is contained in:
dumbmoron 2024-09-07 11:41:56 +00:00
parent 3189857c77
commit 3232c4a51b
No known key found for this signature in database

View file

@ -28,38 +28,22 @@ export default class LibAVWrapper {
if (!this.libav) throw new Error("LibAV wasn't initialized");
const libav = await this.libav;
const OUT_FILE = 'output.json';
await libav.mkreadaheadfile('input', blob);
await libav.mkwriterdev(OUT_FILE);
let writtenData = new Uint8Array(0);
libav.onwrite = (name, pos, data) => {
if (name !== OUT_FILE) return;
const newLen = Math.max(pos + data.length, writtenData.length);
if (newLen > writtenData.length) {
const newData = new Uint8Array(newLen);
newData.set(writtenData);
writtenData = newData;
}
writtenData.set(data, pos);
};
await libav.ffprobe([
'-v', 'quiet',
'-print_format', 'json',
'-show_format',
'-show_streams',
'input',
'-o', OUT_FILE
'-o', 'output.json'
]);
await libav.unlink(OUT_FILE);
await libav.unlinkreadaheadfile('input');
const copy = new Uint8Array(writtenData);
const copy = await libav.readFile('output.json');
const text = new TextDecoder().decode(copy);
await libav.unlink('output.json');
return JSON.parse(text) as FfprobeData;
}