web/workers/remux: accept several files, custom args and output

This commit is contained in:
wukko 2025-01-31 11:16:04 +06:00
parent 7caee22aee
commit f2325bdc24
No known key found for this signature in database
GPG Key ID: 3E30B3F26C7B4AA2
4 changed files with 56 additions and 29 deletions

View File

@ -22,6 +22,15 @@ export const createRemuxPipeline = (file: File) => {
parentId,
workerArgs: {
files: [file],
ffargs: [
"-c", "copy",
"-map", "0"
],
output: {
type: file.type,
extension: file.name.split(".").pop(),
},
filename: file.name,
},
}];

View File

@ -4,6 +4,7 @@ import FetchWorker from "$lib/workers/fetch?worker";
import { updateWorkerProgress } from "$lib/state/queen-bee/current-tasks";
import { pipelineTaskDone, itemError, queue } from "$lib/state/queen-bee/queue";
import type { FileInfo } from "$lib/types/libav";
import type { CobaltQueue } from "$lib/types/queue";
import type { CobaltPipelineItem } from "$lib/types/workers";
@ -13,7 +14,7 @@ const killWorker = (worker: Worker, unsubscribe: () => void, interval?: NodeJS.T
if (interval) clearInterval(interval);
}
export const runRemuxWorker = async (workerId: string, parentId: string, file: File) => {
export const runRemuxWorker = async (workerId: string, parentId: string, files: File[], args: string[], output: FileInfo, filename: string) => {
const worker = new RemuxWorker();
// sometimes chrome refuses to start libav wasm,
@ -42,7 +43,10 @@ export const runRemuxWorker = async (workerId: string, parentId: string, file: F
worker.postMessage({
cobaltRemuxWorker: {
file
files,
args,
output,
filename,
}
});
@ -139,12 +143,26 @@ export const runFetchWorker = async (workerId: string, parentId: string, url: st
}
export const startWorker = async ({ worker, workerId, parentId, workerArgs }: CobaltPipelineItem) => {
let files: File[] = [];
switch (worker) {
case "remux":
if (workerArgs?.files) {
await runRemuxWorker(workerId, parentId, workerArgs.files[0]);
files = workerArgs.files;
}
if (files.length > 0 && workerArgs.ffargs && workerArgs.output && workerArgs.filename) {
await runRemuxWorker(
workerId,
parentId,
files,
workerArgs.ffargs,
workerArgs.output,
workerArgs.filename
);
}
break;
case "fetch":
if (workerArgs?.url) {
await runFetchWorker(workerId, parentId, workerArgs.url)

View File

@ -1,3 +1,5 @@
import type { FileInfo } from "$lib/types/libav";
export const resultFileTypes = ["video", "audio", "image"] as const;
export type CobaltWorkerType = "remux" | "fetch";
@ -12,7 +14,9 @@ export type CobaltWorkerProgress = {
export type CobaltWorkerArgs = {
files?: File[],
url?: string,
//TODO: args for libav & etc with unique types
ffargs?: string[],
output?: FileInfo,
filename?: string,
}
export type CobaltPipelineItem = {

View File

@ -1,4 +1,5 @@
import LibAVWrapper from "$lib/libav";
import type { FileInfo } from "$lib/types/libav";
const error = (code: string) => {
self.postMessage({
@ -24,13 +25,13 @@ const ff = new LibAVWrapper((progress) => {
ff.init();
const remux = async (file: File) => {
if (!file) return;
const remux = async (files: File[], args: string[], output: FileInfo, filename: string) => {
if (!(files && output && args)) return;
await ff.init();
try {
const file_info = await ff.probe(file).catch((e) => {
const file_info = await ff.probe(files[0]).catch((e) => {
if (e?.message?.toLowerCase().includes("out of memory")) {
console.error("uh oh! out of memory");
console.error(e);
@ -41,8 +42,7 @@ const remux = async (file: File) => {
});
if (!file_info?.format) {
error("remux.corrupted");
return;
return error("remux.corrupted");
}
self.postMessage({
@ -53,36 +53,31 @@ const remux = async (file: File) => {
}
});
if (!file.type) {
// TODO: better & more appropriate error code
error("remux.corrupted");
for (const file of files) {
if (!file.type) {
// TODO: better & more appropriate error code
return error("remux.corrupted");
}
}
const render = await ff
.render({
files: [file],
output: {
type: file.type,
extension: file.name.split(".").pop(),
},
args: ["-c", "copy", "-map", "0"],
files,
output,
args,
})
.catch((e) => {
console.error("uh-oh! render error");
console.error(e);
// TODO: better error codes, there are more reasons for a crash
error("remux.out_of_resources");
});
if (!render) {
return console.log("not a valid file");
console.log("not a valid file");
return error("incorrect input or output");
}
const filenameParts = file.name.split(".");
const filenameExt = filenameParts.pop();
const filename = `${filenameParts.join(".")} (remux).${filenameExt}`;
self.postMessage({
cobaltRemuxWorker: {
render,
@ -95,9 +90,10 @@ const remux = async (file: File) => {
}
self.onmessage = async (event: MessageEvent) => {
console.log(event.data);
if (event.data.cobaltRemuxWorker.file) {
await remux(event.data.cobaltRemuxWorker.file);
const ed = event.data.cobaltRemuxWorker;
if (ed) {
if (ed.files && ed.args && ed.output && ed.filename) {
await remux(ed.files, ed.args, ed.output, ed.filename);
}
}
}