Skip to content

armgit / VERT

Public Imported from GitHub
0

The next-generation file converter. Open source, fully local* and free forever.

fix: normalize settings for mxf, prettier format

JovannMC057fa2088177
diff --git a/src/lib/converters/converter.svelte.ts b/src/lib/converters/converter.svelte.ts
index 902e19b..9c2cd90 100644
--- a/src/lib/converters/converter.svelte.ts
+++ b/src/lib/converters/converter.svelte.ts
@@ -3,11 +3,16 @@
import type { VertFile } from "$lib/types";
import type {
ConversionSettings,
+ NormalizedSettings,
SettingDefinition,
} from "$lib/types/conversion-settings";
export type WorkerStatus =
- "not-ready" | "downloading" | "ready" | "partially-ready" | "error";
+ | "not-ready"
+ | "downloading"
+ | "ready"
+ | "partially-ready"
+ | "error";
export class FormatInfo {
public name: string;
@@ -78,6 +83,17 @@ export class Converter {
return defaults;
}
+ public async normalizeSettings(
+ input: VertFile,
+ to: string,
+ settings: ConversionSettings,
+ ): Promise<NormalizedSettings> {
+ return {
+ settings: { ...settings },
+ changes: [],
+ };
+ }
+
private startTimeout() {
this.timeoutId = setTimeout(() => {
if (this.status !== "ready") this.status = "not-ready";
diff --git a/src/lib/converters/ffmpeg/ffmpeg.svelte.ts b/src/lib/converters/ffmpeg/ffmpeg.svelte.ts
index 86ad0e2..6be159b 100644
--- a/src/lib/converters/ffmpeg/ffmpeg.svelte.ts
+++ b/src/lib/converters/ffmpeg/ffmpeg.svelte.ts
@@ -21,6 +21,7 @@ import {
} from "./utils/ffprobe";
import { extractAlbumArt, avWithArt, avWithBg } from "./utils/ffmpeg";
import type {
+ NormalizedSettings,
SettingDefinition,
ConversionSettings,
} from "$lib/types/conversion-settings";
@@ -200,6 +201,59 @@ export class FFmpegConverter extends Converter {
return defaults;
}
+ public async normalizeSettings(
+ input: VertFile,
+ to: string,
+ settings: ConversionSettings,
+ ): Promise<NormalizedSettings> {
+ const normalized = { ...settings };
+ const changes: NormalizedSettings["changes"] = [];
+
+ const change = (setting: string, newValue: string | number) => {
+ const oldValue = normalized[setting];
+ if (oldValue === newValue) return;
+
+ normalized[setting] = newValue;
+ changes.push({
+ setting,
+ oldValue,
+ newValue,
+ file: input.name,
+ });
+ };
+
+ if (to === ".opus") {
+ // work around browser stereo+libopus wasm crash
+ // no idea why its broken like this in the browser only lol
+ if (normalized.channels >= 2) change("channels", 1);
+
+ const bitrate = Number(
+ normalized.bitrate === "custom"
+ ? normalized.customBitrate
+ : normalized.bitrate,
+ );
+ if (Number.isFinite(bitrate) && bitrate > 256)
+ change("bitrate", 256);
+ } else if (to === ".amv") {
+ change("sampleRate", 22050);
+ change("channels", 1);
+ change("bitrate", 32);
+ } else if (to === ".mpg" || to === ".mpeg" || to === ".vob") {
+ const bitrate = Number(
+ normalized.bitrate === "custom"
+ ? normalized.customBitrate
+ : normalized.bitrate,
+ );
+ if (Number.isFinite(bitrate) && bitrate > 0)
+ change("bitrate", Math.pow(2, Math.round(Math.log2(bitrate))));
+ } else if (to === ".gxf") {
+ change("sampleRate", 48000);
+ change("channels", 1);
+ }
+
+ return { settings: normalized, changes };
+ }
+
public async convert(
input: VertFile,
to: string,
@@ -210,7 +264,10 @@ export class FFmpegConverter extends Converter {
const conversionSettings =
Object.keys(settings).length > 4 // TODO: find better way to do this lmfao, rn we are just assuming all settings are present if there's at least 5 keys but ts bad
? settings
- : Object.assign(settings, await this.getDefaultSettings()); // use defaults if not provided
+ : {
+ ...(await this.getDefaultSettings()),
+ ...settings,
+ }; // use defaults if not provided
const isAlac = to === ".alac";
if (isAlac) to = ".m4a";
@@ -428,119 +485,17 @@ export class FFmpegConverter extends Converter {
let tracksArgs: string[] = [];
let metadataArgs: string[] = [];
const extraArgs: string[] = [];
-
- const checkSettings = () => {
- /*
- * sanity check settings for certain formats, to prevent conversion failure
- */
- const settingsChanged: SettingChange[] = [];
-
- console.log(
- `checking settings for compatibility with output format ${to}...`,
+ if (to === ".amv") {
+ // amv requires mono audio, 32kbps, and 22050 sample rate
+ // block size is 22050 (sample rate) / fps. max is 8192, so we have to force a higher fps
+ const amvFps = 3;
+ extraArgs.push(
+ "-r",
+ String(amvFps),
+ "-block_size",
+ String(Math.floor(22050 / amvFps)),
);
-
- if (to === ".opus") {
- // work around browser stereo+libopus wasm crash
- // no idea why its broken like this in the browser only lol
- if (settings.channels >= 2) {
- channelsArgs = ["-ac", "1"];
- settingsChanged.push({
- setting: "channels",
- oldValue: settings.channels,
- newValue: 1,
- file: input.name,
- });
- }
-
- // TODO: surely better way to do this as well :sob:
- if (
- audioBitrateArgs[1] &&
- parseInt(audioBitrateArgs[1], 10) > 256
- ) {
- audioBitrateArgs = ["-b:a", "256k"];
- settingsChanged.push({
- setting: "bitrate",
- oldValue: settings.bitrate,
- newValue: 256,
- file: input.name,
- });
- }
- } else if (to === ".amv") {
- // amv requires mono audio, 32kbps, and 22050 sample rate
- // block size is 22050 (sample rate) / fps. max is 8192, so we have to force a higher fps
- const amvFps = 3;
- const amvBlockSize = Math.floor(22050 / amvFps);
-
- audioBitrateArgs = ["-b:a", "32k"];
- sampleRateArgs = ["-ar", "22050"];
- channelsArgs = ["-ac", "1"];
- extraArgs.push(
- "-r",
- String(amvFps),
- "-block_size",
- String(amvBlockSize),
- );
- settingsChanged.push({
- setting: "sampleRate",
- oldValue: settings.sampleRate,
- newValue: 22050,
- file: input.name,
- });
- settingsChanged.push({
- setting: "channels",
- oldValue: settings.channels,
- newValue: 1,
- file: input.name,
- });
- settingsChanged.push({
- setting: "bitrate",
- oldValue: settings.bitrate,
- newValue: 32,
- file: input.name,
- });
- } else if (to === ".mpg" || to === ".mpeg" || to === ".vob") {
- // these formats need to have its bitrate to the power of 2 to convert correctly
- const bitrate = parseInt(audioBitrateArgs[1], 10);
- const newBitrate = Math.pow(2, Math.round(Math.log2(bitrate)));
- audioBitrateArgs = ["-b:a", `${newBitrate}k`];
- settingsChanged.push({
- setting: "bitrate",
- oldValue: settings.bitrate,
- newValue: newBitrate,
- file: input.name,
- });
- } else if (to === ".gxf") {
- sampleRateArgs = ["-ar", "48000"];
- channelsArgs = ["-ac", "1"];
- settingsChanged.push({
- setting: "sampleRate",
- oldValue: settings.sampleRate,
- newValue: 48000,
- file: input.name,
- });
- settingsChanged.push({
- setting: "channels",
- oldValue: settings.channels,
- newValue: 1,
- file: input.name,
- });
- }
-
- for (const change of settingsChanged) {
- this.log(
- `changed setting "${change.setting}" from "${change.oldValue}" to "${change.newValue}" for file ${change.file} to prevent conversion failure`,
- );
- ToastManager.add({
- type: "warning",
- message: m["workers.warnings.settings_change"]({
- setting: change.setting,
- oldValue: change.oldValue,
- newValue: change.newValue,
- file: change.file,
- }),
- });
- }
- };
+ }
this.log(`keep metadata: ${keepMetadata}`);
if (!keepMetadata) {
@@ -549,8 +504,6 @@ export class FFmpegConverter extends Converter {
"-1",
"-map_chapters", // remove chapters
"-1",
- "-map", // remove cover art
- "a",
];
}
@@ -576,6 +529,20 @@ export class FFmpegConverter extends Converter {
audioBitrateArgs = inputBitrate
? ["-b:a", `${inputBitrate}k`]
: [];
+ if (
+ (to === ".mpg" || to === ".mpeg" || to === ".vob") &&
+ audioBitrateArgs[1]
+ ) {
+ // these formats need to have its bitrate to the power of 2 to convert correctly
+ const bitrate = Number.parseInt(audioBitrateArgs[1], 10);
+ if (Number.isFinite(bitrate) && bitrate > 0) {
+ const normalizedBitrate = Math.pow(
+ 2,
+ Math.round(Math.log2(bitrate)),
+ );
+ audioBitrateArgs = ["-b:a", `${normalizedBitrate}k`];
+ }
+ }
this.log(`using detected audio bitrate: ${inputBitrate}k`);
}
}
@@ -584,7 +551,11 @@ export class FFmpegConverter extends Converter {
if (userSampleRate !== "auto") {
sampleRateArgs = [
"-ar",
- userSampleRate === "custom" ? customSampleRate : userSampleRate,
+ String(
+ userSampleRate === "custom"
+ ? customSampleRate
+ : userSampleRate,
+ ),
];
this.log(`using user setting for sample rate: ${userSampleRate}Hz`);
} else {
@@ -618,7 +589,7 @@ export class FFmpegConverter extends Converter {
// channels setting
if (settings.channels !== 2) {
- channelsArgs = ["-ac", settings.channels];
+ channelsArgs = ["-ac", String(settings.channels)];
this.log(
`using user setting for audio channels: ${settings.channels}`,
);
@@ -642,7 +613,6 @@ export class FFmpegConverter extends Converter {
// video to audio
if (videoFormats.includes(inputFormat)) {
this.log(`Converting video ${input.from} to audio ${to}`);
- checkSettings();
return [
"-i",
"input",
@@ -667,8 +637,6 @@ export class FFmpegConverter extends Converter {
: false;
const codecArgs = toArgs(to, isAlac);
- checkSettings();
-
if (hasAlbumArt) {
this.log("Using album art as video background");
return avWithArt(
@@ -699,8 +667,6 @@ export class FFmpegConverter extends Converter {
const { audio: audioCodec } = getCodecs(to, isAlac);
if (m4a && keepMetadata) extraArgs.push("-c:v", "copy"); // for album art
- checkSettings();
-
return [
"-i",
"input",
@@ -808,10 +774,3 @@ const handleSpecialOutput = async (
return null;
};
-
-interface SettingChange {
- setting: string;
- oldValue: string | number;
- newValue: string | number;
- file: string;
-}
diff --git a/src/lib/converters/vertd/vertd.svelte.ts b/src/lib/converters/vertd/vertd.svelte.ts
index 85f8330..d9fde0c 100644
--- a/src/lib/converters/vertd/vertd.svelte.ts
+++ b/src/lib/converters/vertd/vertd.svelte.ts
@@ -14,6 +14,7 @@ import { converters } from "../";
import type {
SettingDefinition,
ConversionSettings,
+ NormalizedSettings,
} from "$lib/types/conversion-settings";
import { CONVERSION_BITRATES, SAMPLE_RATES } from "../ffmpeg/ffmpeg.codecs";
import { formatBytes } from "$lib/util/file";
@@ -113,7 +114,12 @@ export const vertdFetch: {
// ws types
export type ConversionSpeed =
- "verySlow" | "slower" | "slow" | "medium" | "fast" | "ultraFast";
+ | "verySlow"
+ | "slower"
+ | "slow"
+ | "medium"
+ | "fast"
+ | "ultraFast";
const vertdSpeedValues: ConversionSpeed[] = [
"verySlow",
@@ -730,6 +736,34 @@ export class VertdConverter extends Converter {
return defaults;
}
+ public async normalizeSettings(
+ input: VertFile,
+ to: string,
+ settings: ConversionSettings,
+ ): Promise<NormalizedSettings> {
+ const normalized = { ...settings };
+ const changes: NormalizedSettings["changes"] = [];
+
+ const change = (setting: string, newValue: string | number) => {
+ const oldValue = normalized[setting];
+ if (oldValue === newValue) return;
+
+ normalized[setting] = newValue;
+ changes.push({
+ setting,
+ oldValue,
+ newValue,
+ file: input.name,
+ });
+ };
+
+ if (to === ".mxf") {
+ change("sampleRate", "48000");
+ }
+
+ return { settings: normalized, changes };
+ }
+
public async convert(
input: VertFile,
to: string,
diff --git a/src/lib/sections/settings/vertdSettings.svelte.ts b/src/lib/sections/settings/vertdSettings.svelte.ts
index c8819f5..1f660d7 100644
--- a/src/lib/sections/settings/vertdSettings.svelte.ts
+++ b/src/lib/sections/settings/vertdSettings.svelte.ts
@@ -8,7 +8,10 @@ const LOCATIONS = [
];
export type VertdInner =
- { type: "auto" } | { type: "eu" } | { type: "us" } | { type: "custom" };
+ | { type: "auto" }
+ | { type: "eu" }
+ | { type: "us" }
+ | { type: "custom" };
export const getVertdCustomHeaders = (): Record<string, string> => {
const raw = Settings.instance.settings.vertdCustomHeaders.trim();
diff --git a/src/lib/store/index.svelte.ts b/src/lib/store/index.svelte.ts
index ef7a0ff..f7b9f97 100644
--- a/src/lib/store/index.svelte.ts
+++ b/src/lib/store/index.svelte.ts
@@ -377,7 +377,13 @@ class Files {
public add(file: FileList | null | undefined): void;
public add(
file:
- VertFile | File | VertFile[] | File[] | FileList | null | undefined,
+ | VertFile
+ | File
+ | VertFile[]
+ | File[]
+ | FileList
+ | null
+ | undefined,
) {
if (!file) return;
if (Array.isArray(file) || file instanceof FileList) {
diff --git a/src/lib/types/conversion-settings.ts b/src/lib/types/conversion-settings.ts
index 184ce6a..3e3d38f 100644
--- a/src/lib/types/conversion-settings.ts
+++ b/src/lib/types/conversion-settings.ts
@@ -21,3 +21,15 @@ export interface SettingDefinition {
export interface ConversionSettings {
[key: string]: any;
}
+
+export interface SettingChange {
+ setting: string;
+ oldValue: string | number;
+ newValue: string | number;
+ file: string;
+}
+
+export interface NormalizedSettings {
+ settings: ConversionSettings;
+ changes: SettingChange[];
+}
diff --git a/src/lib/types/file.svelte.ts b/src/lib/types/file.svelte.ts
index 13a3d51..2888635 100644
--- a/src/lib/types/file.svelte.ts
+++ b/src/lib/types/file.svelte.ts
@@ -8,6 +8,7 @@ import FallbackToast from "$lib/components/functional/popups/FallbackToast.svelt
import ServerUploadWarning from "$lib/components/functional/popups/ServerUploadWarning.svelte";
import type {
ConversionSettings,
+ NormalizedSettings,
SettingDefinition,
} from "./conversion-settings";
import { log } from "$lib/util/logger";
@@ -181,11 +182,13 @@ export class VertFile {
const fileExtension = this.from.slice(1);
const detectedExtension = forceKeep.includes(fileExtension)
? fileExtension
- : aliases[this.fileType.ext] ?? this.fileType.ext;
+ : (aliases[this.fileType.ext] ?? this.fileType.ext);
const expectedExtension = aliases[fileExtension] ?? fileExtension;
if (detectedExtension !== expectedExtension) {
- console.warn(`file type mismatched: expected ${expectedExtension}, detected ${detectedExtension}`);
+ console.warn(
+ `file type mismatched: expected ${expectedExtension}, detected ${detectedExtension}`,
+ );
// TODO: show a warning modal or message if detected type doesn't match name
}
@@ -208,7 +211,9 @@ export class VertFile {
if (!this.retryingFallback) this.attemptedConverters.clear();
- console.log(`Starting conversion for ${this.file.name} from ${this.from} to ${this.to}`);
+ console.log(
+ `Starting conversion for ${this.file.name} from ${this.from} to ${this.to}`,
+ );
if (!this.converters.length) throw new Error("No converters found");
let converter: Converter | undefined;
@@ -263,6 +268,32 @@ export class VertFile {
return;
}
+ const normalizedSettings: NormalizedSettings =
+ await converter.normalizeSettings(this, this.to, {
+ ...(await converter.getDefaultSettings(this)),
+ ...Object.fromEntries(
+ Object.entries(this.conversionSettings).filter(
+ ([, value]) => value !== undefined,
+ ),
+ ),
+ });
+
+ for (const change of normalizedSettings.changes) {
+ log(
+ ["file", "settings"],
+ `changed setting "${change.setting}" from "${change.oldValue}" to "${change.newValue}" for file ${change.file}`,
+ );
+ ToastManager.add({
+ type: "warning",
+ message: m["workers.warnings.settings_change"]({
+ setting: change.setting,
+ oldValue: change.oldValue,
+ newValue: change.newValue,
+ file: change.file,
+ }),
+ });
+ }
+
this.attemptedConverters.add(converter.name);
this.activeConverterName = converter.name;
log(["file", "convert"], `using converter: ${converter.name}`);
@@ -277,11 +308,14 @@ export class VertFile {
// else convert normally
res =
this.isZip() && !this.conversionSettings.imageSequence
- ? await this.convertZip(converter)
+ ? await this.convertZip(
+ converter,
+ normalizedSettings.settings,
+ )
: await converter.convert(
this,
this.to,
- this.conversionSettings,
+ normalizedSettings.settings,
...args,
);
this.result = res;
@@ -399,7 +433,10 @@ export class VertFile {
});
}
- private async convertZip(converter: Converter): Promise<VertFile> {
+ private async convertZip(
+ converter: Converter,
+ settings: ConversionSettings,
+ ): Promise<VertFile> {
const { extractZip, createZip } = await import("$lib/util/file");
const { default: PQueue } = await import("p-queue");
@@ -441,7 +478,7 @@ export class VertFile {
const converted = await converter.convert(
tempVFile,
this.to,
- this.conversionSettings,
+ settings,
);
let outputExt = this.to;
@@ -463,7 +500,7 @@ export class VertFile {
const converted = await converter.convert(
tempVFile,
this.to,
- this.conversionSettings,
+ settings,
);
let outputExt = this.to;
diff --git a/src/lib/util/toast.svelte.ts b/src/lib/util/toast.svelte.ts
index a860228..a7e3d75 100644
--- a/src/lib/util/toast.svelte.ts
+++ b/src/lib/util/toast.svelte.ts
@@ -82,7 +82,8 @@ export type ComponentToastOptions<T> = BaseToastOptions & {
};
export type ToastOptions<T = unknown> =
- StringToastOptions | ComponentToastOptions<T>;
+ | StringToastOptions
+ | ComponentToastOptions<T>;
// const toasts = writable<Toast[]>([]);