The swiss army knife of lossless video/audio editing
Keep file extension when truncating long output file names
Claude64a9da37d340
diff --git a/src/common/util.test.ts b/src/common/util.test.tsindex 597836c2..6f462a1c 100644--- a/src/common/util.test.ts+++ b/src/common/util.test.ts@@ -1,7 +1,7 @@// eslint-disable-next-line import/no-extraneous-dependenciesimport { describe, expect, test } from 'vitest';-import { getFixChannelLayoutFilter, hasCustomChannelLayout } from './util.js';+import { getFixChannelLayoutFilter, hasCustomChannelLayout, truncateFileName } from './util.js';describe('hasCustomChannelLayout', () => {test('detects layouts that ffmpeg could not express natively', () => {@@ -44,3 +44,17 @@ describe('getFixChannelLayoutFilter', () => {expect(getFixChannelLayoutFilter({ channels: 0, channelLayout: '4 channels (UNSD+UNSD+UNSD+UNSD)' })).toBeUndefined();});});++describe('truncateFileName', () => {+ test('keeps short names as is', () => {+ expect(truncateFileName('video.mp4', 10)).toBe('video.mp4');+ });+ test('keeps the extension when truncating', () => {+ expect(truncateFileName('a very long name.mp4', 10)).toBe('a very.mp4');+ expect(truncateFileName('a.b.c.mkv', 7)).toBe('a.b.mkv');+ });+ test('truncates names without extension', () => {+ expect(truncateFileName('abcdefghij', 5)).toBe('abcde');+ expect(truncateFileName('.hiddenfile', 5)).toBe('.hidd');+ });+});diff --git a/src/common/util.ts b/src/common/util.tsindex d15359ba..9b63b224 100644--- a/src/common/util.ts+++ b/src/common/util.ts@@ -10,6 +10,14 @@ export const getHwaccelArgs = (hwaccel: FfmpegHwAccel) => (hwaccel !== 'none' ?// I don't remember why 5 was chosen initially, but if we don't truncate, ffmpeg can sometimes give an error when too many decimal places are used in the time argument, see:export const formatFfmpegNumber = (time: number) => time.toFixed(6);+// Truncate a file name to `maxLength`, but keep its extension (if any)+export function truncateFileName(name: string, maxLength: number) {+ if (name.length <= maxLength) return name;+ const extIndex = name.lastIndexOf('.');+ const ext = extIndex > 0 ? name.slice(extIndex) : '';+ return name.slice(0, Math.max(0, maxLength - ext.length)) + ext;+}+export function parseRatio(str: string, char = '/') {const split = str.split(char);if (split.length !== 2) return undefined;diff --git a/src/renderer/src/util/outputNameTemplate.ts b/src/renderer/src/util/outputNameTemplate.tsindex d43be543..63027f93 100644--- a/src/renderer/src/util/outputNameTemplate.ts+++ b/src/renderer/src/util/outputNameTemplate.ts@@ -13,6 +13,7 @@ import type { FileStats, FormatTimecode, SegmentToExport } from '../types';import safeishEval from '../worker/eval';import { UserFacingError } from '../../errors';import type { FileFfprobeMeta } from '../ffmpeg';+import { truncateFileName } from '../../../common/util';export const segNumVariable = 'SEG_NUM';@@ -230,7 +231,7 @@ function maybeTruncatePath(fileName: string, truncate: boolean) {return [...rest,// If sanitation is enabled, make sure filename (last seg of the path) is not too long- truncate ? lastSeg!.slice(0, maxFileNameLength) : lastSeg,+ truncate ? truncateFileName(lastSeg!, maxFileNameLength) : lastSeg,].join(pathSep);}