web/libav: always clean up files on function exit

This commit is contained in:
dumbmoron 2024-09-07 14:23:33 +00:00
parent 0ce743d13f
commit 0a37c84e93
No known key found for this signature in database

View file

@ -29,6 +29,8 @@ export default class LibAVWrapper {
const libav = await this.libav;
await libav.mkreadaheadfile('input', blob);
try {
await libav.ffprobe([
'-v', 'quiet',
'-print_format', 'json',
@ -38,13 +40,14 @@ export default class LibAVWrapper {
'-o', 'output.json'
]);
await libav.unlinkreadaheadfile('input');
const copy = await libav.readFile('output.json');
const text = new TextDecoder().decode(copy);
await libav.unlink('output.json');
return JSON.parse(text) as FfprobeData;
} finally {
await libav.unlinkreadaheadfile('input');
}
}
static getExtensionFromType(blob: Blob) {
@ -82,6 +85,7 @@ export default class LibAVWrapper {
const outputName = `output.${output.extension}`;
try {
await libav.mkreadaheadfile("input", blob);
// https://github.com/Yahweasel/libav.js/blob/7d359f69/docs/IO.md#block-writer-devices
@ -121,10 +125,6 @@ export default class LibAVWrapper {
outputName
]);
await libav.unlink(outputName);
await libav.unlink('progress.txt');
await libav.unlinkreadaheadfile("input");
// if we didn't need as much space as we allocated for some reason,
// shrink the buffer so that we don't inflate the file with zeros
if (writtenData.length > actualSize) {
@ -138,6 +138,14 @@ export default class LibAVWrapper {
if (renderBlob.size === 0) return;
return renderBlob;
} finally {
try {
await libav.unlink(outputName);
await libav.unlink('progress.txt');
await libav.unlinkreadaheadfile("input");
} catch {}
}
}
#emitProgress(data: Uint8Array | Int8Array) {