Skip to content

Commit f2325bd

Browse files
committed
web/workers/remux: accept several files, custom args and output
1 parent 7caee22 commit f2325bd

File tree

4 files changed

+56
-29
lines changed

4 files changed

+56
-29
lines changed

web/src/lib/queen-bee/queue.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ export const createRemuxPipeline = (file: File) => {
2222
parentId,
2323
workerArgs: {
2424
files: [file],
25+
ffargs: [
26+
"-c", "copy",
27+
"-map", "0"
28+
],
29+
output: {
30+
type: file.type,
31+
extension: file.name.split(".").pop(),
32+
},
33+
filename: file.name,
2534
},
2635
}];
2736

web/src/lib/queen-bee/run-worker.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import FetchWorker from "$lib/workers/fetch?worker";
44
import { updateWorkerProgress } from "$lib/state/queen-bee/current-tasks";
55
import { pipelineTaskDone, itemError, queue } from "$lib/state/queen-bee/queue";
66

7+
import type { FileInfo } from "$lib/types/libav";
78
import type { CobaltQueue } from "$lib/types/queue";
89
import type { CobaltPipelineItem } from "$lib/types/workers";
910

@@ -13,7 +14,7 @@ const killWorker = (worker: Worker, unsubscribe: () => void, interval?: NodeJS.T
1314
if (interval) clearInterval(interval);
1415
}
1516

16-
export const runRemuxWorker = async (workerId: string, parentId: string, file: File) => {
17+
export const runRemuxWorker = async (workerId: string, parentId: string, files: File[], args: string[], output: FileInfo, filename: string) => {
1718
const worker = new RemuxWorker();
1819

1920
// sometimes chrome refuses to start libav wasm,
@@ -42,7 +43,10 @@ export const runRemuxWorker = async (workerId: string, parentId: string, file: F
4243

4344
worker.postMessage({
4445
cobaltRemuxWorker: {
45-
file
46+
files,
47+
args,
48+
output,
49+
filename,
4650
}
4751
});
4852

@@ -139,12 +143,26 @@ export const runFetchWorker = async (workerId: string, parentId: string, url: st
139143
}
140144

141145
export const startWorker = async ({ worker, workerId, parentId, workerArgs }: CobaltPipelineItem) => {
146+
let files: File[] = [];
147+
142148
switch (worker) {
143149
case "remux":
144150
if (workerArgs?.files) {
145-
await runRemuxWorker(workerId, parentId, workerArgs.files[0]);
151+
files = workerArgs.files;
152+
}
153+
154+
if (files.length > 0 && workerArgs.ffargs && workerArgs.output && workerArgs.filename) {
155+
await runRemuxWorker(
156+
workerId,
157+
parentId,
158+
files,
159+
workerArgs.ffargs,
160+
workerArgs.output,
161+
workerArgs.filename
162+
);
146163
}
147164
break;
165+
148166
case "fetch":
149167
if (workerArgs?.url) {
150168
await runFetchWorker(workerId, parentId, workerArgs.url)

web/src/lib/types/workers.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { FileInfo } from "$lib/types/libav";
2+
13
export const resultFileTypes = ["video", "audio", "image"] as const;
24

35
export type CobaltWorkerType = "remux" | "fetch";
@@ -12,7 +14,9 @@ export type CobaltWorkerProgress = {
1214
export type CobaltWorkerArgs = {
1315
files?: File[],
1416
url?: string,
15-
//TODO: args for libav & etc with unique types
17+
ffargs?: string[],
18+
output?: FileInfo,
19+
filename?: string,
1620
}
1721

1822
export type CobaltPipelineItem = {

web/src/lib/workers/remux.ts

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import LibAVWrapper from "$lib/libav";
2+
import type { FileInfo } from "$lib/types/libav";
23

34
const error = (code: string) => {
45
self.postMessage({
@@ -24,13 +25,13 @@ const ff = new LibAVWrapper((progress) => {
2425

2526
ff.init();
2627

27-
const remux = async (file: File) => {
28-
if (!file) return;
28+
const remux = async (files: File[], args: string[], output: FileInfo, filename: string) => {
29+
if (!(files && output && args)) return;
2930

3031
await ff.init();
3132

3233
try {
33-
const file_info = await ff.probe(file).catch((e) => {
34+
const file_info = await ff.probe(files[0]).catch((e) => {
3435
if (e?.message?.toLowerCase().includes("out of memory")) {
3536
console.error("uh oh! out of memory");
3637
console.error(e);
@@ -41,8 +42,7 @@ const remux = async (file: File) => {
4142
});
4243

4344
if (!file_info?.format) {
44-
error("remux.corrupted");
45-
return;
45+
return error("remux.corrupted");
4646
}
4747

4848
self.postMessage({
@@ -53,36 +53,31 @@ const remux = async (file: File) => {
5353
}
5454
});
5555

56-
if (!file.type) {
57-
// TODO: better & more appropriate error code
58-
error("remux.corrupted");
56+
for (const file of files) {
57+
if (!file.type) {
58+
// TODO: better & more appropriate error code
59+
return error("remux.corrupted");
60+
}
5961
}
6062

6163
const render = await ff
6264
.render({
63-
files: [file],
64-
output: {
65-
type: file.type,
66-
extension: file.name.split(".").pop(),
67-
},
68-
args: ["-c", "copy", "-map", "0"],
65+
files,
66+
output,
67+
args,
6968
})
7069
.catch((e) => {
7170
console.error("uh-oh! render error");
7271
console.error(e);
73-
72+
// TODO: better error codes, there are more reasons for a crash
7473
error("remux.out_of_resources");
7574
});
7675

7776
if (!render) {
78-
return console.log("not a valid file");
77+
console.log("not a valid file");
78+
return error("incorrect input or output");
7979
}
8080

81-
const filenameParts = file.name.split(".");
82-
const filenameExt = filenameParts.pop();
83-
84-
const filename = `${filenameParts.join(".")} (remux).${filenameExt}`;
85-
8681
self.postMessage({
8782
cobaltRemuxWorker: {
8883
render,
@@ -95,9 +90,10 @@ const remux = async (file: File) => {
9590
}
9691

9792
self.onmessage = async (event: MessageEvent) => {
98-
console.log(event.data);
99-
100-
if (event.data.cobaltRemuxWorker.file) {
101-
await remux(event.data.cobaltRemuxWorker.file);
93+
const ed = event.data.cobaltRemuxWorker;
94+
if (ed) {
95+
if (ed.files && ed.args && ed.output && ed.filename) {
96+
await remux(ed.files, ed.args, ed.output, ed.filename);
97+
}
10298
}
10399
}

0 commit comments

Comments
 (0)