Skip to content

The swiss army knife of lossless video/audio editing

Use the merged files template as fallback when merging files

Claudebaadf2cf9b11
diff --git a/src/renderer/src/util/outputNameTemplate.ts b/src/renderer/src/util/outputNameTemplate.ts
index e44192c5..ab98ec48 100644
--- a/src/renderer/src/util/outputNameTemplate.ts
+++ b/src/renderer/src/util/outputNameTemplate.ts
@@ -13,7 +13,7 @@ import type { FileStats, FormatTimecode, SegmentToExport } from '../types';
import safeishEval from '../worker/eval';
import { UserFacingError } from '../../errors';
import type { FileFfprobeMeta } from '../ffmpeg';
-import { getUtf8Length, truncateFileName, truncateUtf8 } from '../../../common/util';
+import { truncateFileName } from '../../../common/util';
export const segNumVariable = 'SEG_NUM';
@@ -221,26 +221,18 @@ async function interpolateOutFileName(template: string, {
return ret;
}
-function truncatePath(fileName: string) {
+function maybeTruncatePath(fileName: string, truncate: boolean) {
// Split the path by its separator, so we can check the actual file name (last path seg)
const pathSegs = fileName.split(pathSep);
if (pathSegs.length === 0) return '';
const [lastSeg] = pathSegs.slice(-1);
const rest = pathSegs.slice(0, -1);
- return [...rest, truncateFileName(lastSeg!, maxFileNameLength)].join(pathSep);
-}
-
-// If sanitation is enabled, make sure the file name (last seg of the path) is not too long.
-// First try to shorten the source file name (FILENAME), so that the parts that make each output name unique (cut times, segment number etc.) are kept
-async function interpolateTruncatedOutFileName(template: string, params: Parameters<typeof interpolateOutFileName>[1], truncate: boolean) {
- const fileName = await interpolateOutFileName(template, params);
- if (!truncate) return fileName;
- const overflow = getUtf8Length(basename(fileName)) - maxFileNameLength;
- if (overflow <= 0) return fileName;
- const { inputFileNameWithoutExt } = params;
- const shortenedFileName = await interpolateOutFileName(template, { ...params, inputFileNameWithoutExt: truncateUtf8(inputFileNameWithoutExt, getUtf8Length(inputFileNameWithoutExt) - overflow) });
- return truncatePath(shortenedFileName);
+ return [
+ ...rest,
+ // If sanitation is enabled, make sure filename (last seg of the path) is not too long
+ truncate ? truncateFileName(lastSeg!, maxFileNameLength, isWindows) : lastSeg,
+ ].join(pathSep);
}
async function generateWithFallback({ generate, desiredTemplate, defaultTemplate, safeOutputFileName, filePath, outputDir, maxLabelLength }: {
@@ -324,7 +316,7 @@ export async function generateCutFileNames({ fileDuration, segmentsToExport: seg
const cutDuration = end - start;
- return interpolateTruncatedOutFileName(template, {
+ const segFileName = await interpolateOutFileName(template, {
epochMs,
segNum,
segNumPadded,
@@ -343,7 +335,9 @@ export async function generateCutFileNames({ fileDuration, segmentsToExport: seg
tags: Object.fromEntries(Object.entries(getSegmentTags(segment)).map(([tag, value]) => [tag, sanitizeName(value)])),
exportCount,
currentFileExportCount,
- }, safeOutputFileName2);
+ });
+
+ return maybeTruncatePath(segFileName, safeOutputFileName2);
}, { concurrency: 5 });
},
desiredTemplate,
@@ -374,7 +368,7 @@ export async function generateCutMergedFileNames({ template: desiredTemplate, is
generate: async ({ template, safeOutputFileName: safeOutputFileName2, sanitizeName }) => {
const { name: inputFileNameWithoutExt } = parsePath(sourceFile.path);
- return [await interpolateTruncatedOutFileName(template, {
+ const fileName = await interpolateOutFileName(template, {
epochMs,
sourceFiles: [sourceFile],
inputFileNameWithoutExt,
@@ -382,7 +376,9 @@ export async function generateCutMergedFileNames({ template: desiredTemplate, is
exportCount,
currentFileExportCount,
segLabels: segLabels.map((label) => sanitizeName(label)),
- }, safeOutputFileName2)];
+ });
+
+ return [maybeTruncatePath(fileName, safeOutputFileName2)];
},
desiredTemplate,
defaultTemplate: defaultCutMergedFileTemplate,
@@ -411,17 +407,19 @@ export async function generateMergedFileNames({ template: desiredTemplate, isCus
generate: async ({ template, safeOutputFileName: safeOutputFileName2, sanitizeName }) => {
const { name: inputFileNameWithoutExt } = parsePath(firstFile.path);
- return [await interpolateTruncatedOutFileName(template, {
+ const fileName = await interpolateOutFileName(template, {
epochMs,
sourceFiles,
inputFileNameWithoutExt,
ext: getOutFileExtension({ isCustomFormatSelected, outFormat: fileFormat, filePath: firstFile.path }),
exportCount,
segLabels: sourceFiles.map((file) => sanitizeName(basename(file.path))),
- }, safeOutputFileName2)];
+ });
+
+ return [maybeTruncatePath(fileName, safeOutputFileName2)];
},
desiredTemplate,
- defaultTemplate: defaultCutMergedFileTemplate,
+ defaultTemplate: defaultMergedFileTemplate,
filePath: firstFile.path,
outputDir,
maxLabelLength,