File size: 6,703 Bytes
11fcc5a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
import { get } from "svelte/store";
import { t } from "$lib/i18n/translations";
import { ffmpegMetadataArgs } from "$lib/util";
import { createDialog } from "$lib/state/dialogs";
import { addItem } from "$lib/state/task-manager/queue";
import { openQueuePopover } from "$lib/state/queue-visibility";
import { uuid } from "$lib/util";
import type { CobaltQueueItem } from "$lib/types/queue";
import type { CobaltCurrentTasks } from "$lib/types/task-manager";
import { resultFileTypes, type CobaltPipelineItem, type CobaltPipelineResultFileType } from "$lib/types/workers";
import type { CobaltLocalProcessingResponse, CobaltSaveRequestBody } from "$lib/types/api";
export const getMediaType = (type: string) => {
const kind = type.split('/')[0] as CobaltPipelineResultFileType;
if (resultFileTypes.includes(kind)) {
return kind;
}
}
export const createRemuxPipeline = (file: File) => {
const parentId = uuid();
const mediaType = getMediaType(file.type);
const pipeline: CobaltPipelineItem[] = [{
worker: "remux",
workerId: uuid(),
parentId,
workerArgs: {
files: [file],
ffargs: [
"-c", "copy",
"-map", "0"
],
output: {
type: file.type,
format: file.name.split(".").pop(),
},
},
}];
if (mediaType) {
addItem({
id: parentId,
state: "waiting",
pipeline,
filename: file.name,
mimeType: file.type,
mediaType,
});
openQueuePopover();
}
}
const makeRemuxArgs = (info: CobaltLocalProcessingResponse) => {
const ffargs = ["-c:v", "copy"];
if (["merge", "remux"].includes(info.type)) {
ffargs.push("-c:a", "copy");
} else if (info.type === "mute") {
ffargs.push("-an");
}
if (info.output.subtitles) {
ffargs.push(
"-c:s",
info.output.filename.endsWith(".mp4") ? "mov_text" : "webvtt"
);
}
ffargs.push(
...(info.output.metadata ? ffmpegMetadataArgs(info.output.metadata) : [])
);
return ffargs;
}
const makeAudioArgs = (info: CobaltLocalProcessingResponse) => {
if (!info.audio) {
return;
}
const ffargs = [];
if (info.audio.cover && info.audio.format === "mp3") {
ffargs.push(
"-map", "0",
"-map", "1",
...(info.audio.cropCover ? [
"-c:v", "mjpeg",
"-vf", "scale=-1:720,crop=720:720",
] : [
"-c:v", "copy",
]),
);
} else {
ffargs.push("-vn");
}
ffargs.push(
...(info.audio.copy ? ["-c:a", "copy"] : ["-b:a", `${info.audio.bitrate}k`]),
...(info.output.metadata ? ffmpegMetadataArgs(info.output.metadata) : [])
);
if (info.audio.format === "mp3" && info.audio.bitrate === "8") {
ffargs.push("-ar", "12000");
}
if (info.audio.format === "opus") {
ffargs.push("-vbr", "off")
}
const outFormat = info.audio.format === "m4a" ? "ipod" : info.audio.format;
ffargs.push('-f', outFormat);
return ffargs;
}
const makeGifArgs = () => {
return [
"-vf",
"scale=-1:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse",
"-loop", "0",
"-f", "gif"
];
}
const showError = (errorCode: string) => {
return createDialog({
id: "pipeline-error",
type: "small",
meowbalt: "error",
buttons: [
{
text: get(t)("button.gotit"),
main: true,
action: () => {},
},
],
bodyText: get(t)(`error.${errorCode}`),
});
}
export const createSavePipeline = (
info: CobaltLocalProcessingResponse,
request: CobaltSaveRequestBody,
oldTaskId?: string
) => {
// this is a pre-queue part of processing,
// so errors have to be returned via a regular dialog
if (!info.output?.filename || !info.output?.type) {
return showError("pipeline.missing_response_data");
}
const parentId = oldTaskId || uuid();
const pipeline: CobaltPipelineItem[] = [];
// reverse is needed for audio (second item) to be downloaded first
const tunnels = info.tunnel.reverse();
for (const tunnel of tunnels) {
pipeline.push({
worker: "fetch",
workerId: uuid(),
parentId,
workerArgs: {
url: tunnel,
},
});
}
if (info.type !== "proxy") {
let ffargs: string[];
let workerType: 'encode' | 'remux';
if (["merge", "mute", "remux"].includes(info.type)) {
workerType = "remux";
ffargs = makeRemuxArgs(info);
} else if (info.type === "audio") {
const args = makeAudioArgs(info);
if (!args) {
return showError("pipeline.missing_response_data");
}
workerType = "encode";
ffargs = args;
} else if (info.type === "gif") {
workerType = "encode";
ffargs = makeGifArgs();
} else {
console.error("unknown work type: " + info.type);
return showError("pipeline.missing_response_data");
}
pipeline.push({
worker: workerType,
workerId: uuid(),
parentId,
dependsOn: pipeline.map(w => w.workerId),
workerArgs: {
files: [],
ffargs,
output: {
type: info.output.type,
format: info.output.filename.split(".").pop(),
},
},
});
}
addItem({
id: parentId,
state: "waiting",
pipeline,
canRetry: true,
originalRequest: request,
filename: info.output.filename,
mimeType: info.output.type,
mediaType: getMediaType(info.output.type) || "file",
});
openQueuePopover();
}
export const getProgress = (item: CobaltQueueItem, currentTasks: CobaltCurrentTasks): number => {
if (item.state === 'done' || item.state === 'error') {
return 1;
} else if (item.state === 'waiting') {
return 0;
}
let sum = 0;
for (const worker of item.pipeline) {
if (item.pipelineResults[worker.workerId]) {
sum += 1;
} else {
const task = currentTasks[worker.workerId];
sum += (task?.progress?.percentage || 0) / 100;
}
}
return sum / item.pipeline.length;
}
|