From 2198a696ced46e1a28320c0d76a433f9f986bb02 Mon Sep 17 00:00:00 2001
From: dumbmoron <log@riseup.net>
Date: Mon, 12 Aug 2024 17:06:45 +0000
Subject: [PATCH] web/libav: make it work & clean up

---
 web/src/lib/libav.ts              | 27 +++++++++++++++++----------
 web/src/routes/remux/+page.svelte |  8 ++++----
 2 files changed, 21 insertions(+), 14 deletions(-)

diff --git a/web/src/lib/libav.ts b/web/src/lib/libav.ts
index 266ac75c..2f2b9f16 100644
--- a/web/src/lib/libav.ts
+++ b/web/src/lib/libav.ts
@@ -10,7 +10,7 @@ type FileInfo = {
 }
 
 type RenderParams = {
-    file: File,
+    blob: Blob,
     output?: FileInfo,
     args: string[],
 }
@@ -26,16 +26,17 @@ export default class LibAVWrapper {
     async init() {
         if (!this.libav) {
             this.libav = await LibAV.LibAV({
-                yesthreads: true
+                yesthreads: true,
+                base: '/_libav/'
             })
         }
     }
 
-    async renderFile({ file, output, args }: RenderParams) {
+    async render({ blob, output, args }: RenderParams) {
         if (!this.libav) throw new Error("LibAV wasn't initialized");
 
-        const inputKind = file.type.split("/")[0];
-        const inputExtension = mime.getExtension(file.type);
+        const inputKind = blob.type.split("/")[0];
+        const inputExtension = mime.getExtension(blob.type);
 
         if (inputKind !== "video" && inputKind !== "audio") return;
         if (!inputExtension) return;
@@ -52,14 +53,15 @@ export default class LibAVWrapper {
 
         const outputName = `output.${output.extension}`;
 
-        const buffer = new Blob([await file.arrayBuffer()]);
-        await this.libav.mkreadaheadfile("input", buffer);
+        await this.libav.mkreadaheadfile("input", blob);
 
         // https://github.com/Yahweasel/libav.js/blob/7d359f69/docs/IO.md#block-writer-devices
         await this.libav.mkwriterdev(outputName);
         let writtenData = new Uint8Array(0);
 
         this.libav.onwrite = (name, pos, data) => {
+            if (name !== outputName) return;
+
             const newLen = Math.max(writtenData.length, pos + data.length);
             if (newLen > writtenData.length) {
                 const newData = new Uint8Array(newLen);
@@ -70,20 +72,25 @@ export default class LibAVWrapper {
         };
 
         await this.libav.ffmpeg([
+            '-nostdin', '-y',
             '-threads', this.concurrency.toString(),
             '-i', 'input',
             ...args,
             outputName
         ]);
 
-        await this.libav.unlinkmkreadaheadfile("input");
+        await this.libav.unlink(outputName);
+
+        // FIXME: this is not correct, and needs to be replaced
+        //        with unlinkmkreadaheadfile().
+        await this.libav.unlink("input");
 
         const renderBlob = new Blob(
-            [writtenData],
+            [ writtenData ],
             { type: output.type }
         );
 
         if (renderBlob.size === 0) return;
         return renderBlob;
     }
-}
+}
\ No newline at end of file
diff --git a/web/src/routes/remux/+page.svelte b/web/src/routes/remux/+page.svelte
index 36666d58..7c570ff1 100644
--- a/web/src/routes/remux/+page.svelte
+++ b/web/src/routes/remux/+page.svelte
@@ -1,5 +1,5 @@
 <script lang="ts">
-    import FFmpegWrapper from "$lib/ffmpeg";
+    import LibAVWrapper from "$lib/libav";
     import { openURL } from "$lib/download";
 
     import DragDropArea from "$components/misc/DragDropArea.svelte";
@@ -9,11 +9,11 @@
     let file: File;
 
     const render = async () => {
-        const ff = new FFmpegWrapper();
+        const ff = new LibAVWrapper();
         await ff.init();
 
-        const render = await ff.renderFile({
-            file,
+        const render = await ff.render({
+            blob: file,
             args: ["-c", "copy"],
         });