Skip to content

The swiss army knife of lossless video/audio editing

Truncate long file names in the middle instead of re-evaluating the template

Claude7618e199d0e3
diff --git a/src/common/util.test.ts b/src/common/util.test.ts
index 0fd36841..e4b39be4 100644
--- a/src/common/util.test.ts
+++ b/src/common/util.test.ts
@@ -1,7 +1,7 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import { describe, expect, test } from 'vitest';
-import { getFixChannelLayoutFilter, hasCustomChannelLayout, truncateFileName, truncateUtf8 } from './util.js';
+import { getFixChannelLayoutFilter, hasCustomChannelLayout, truncateFileName } from './util.js';
describe('hasCustomChannelLayout', () => {
test('detects layouts that ffmpeg could not express natively', () => {
@@ -49,27 +49,20 @@ 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('cuts out the middle and keeps the extension', () => {
+ expect(truncateFileName('abcdefghij-seg1.mp4', 14)).toBe('abc…seg1.mp4'); // 3 + 3 (…) + 4 + 4
+ expect(truncateFileName('a.b.c.d.e.mkv', 12)).toBe('a.…d.e.mkv');
});
test('truncates names without extension', () => {
- expect(truncateFileName('abcdefghij', 5)).toBe('abcde');
- expect(truncateFileName('.hiddenfile', 5)).toBe('.hidd');
+ expect(truncateFileName('abcdefghij', 7)).toBe('ab…ij');
});
test('ignores a dot too early in the name to be an extension', () => {
- expect(truncateFileName(`Dr. ${'a'.repeat(20)}`, 10)).toBe('Dr. aaaaaa');
+ expect(truncateFileName(`Dr. ${'a'.repeat(20)}`, 13)).toBe('Dr. a…aaaaa');
});
- test('counts UTF-8 bytes and does not split characters', () => {
- expect(truncateFileName('日本語の動画.mp4', 13)).toBe('日本語.mp4'); // 3 bytes per character
- expect(truncateFileName('😀😀😀.mkv', 13)).toBe('😀😀.mkv'); // 4 bytes per emoji
- });
-});
-
-describe('truncateUtf8', () => {
- test('truncates to whole characters', () => {
- expect(truncateUtf8('abc', 5)).toBe('abc');
- expect(truncateUtf8('😀😀', 5)).toBe('😀');
- expect(truncateUtf8('åäö', 3)).toBe('å');
+ test('counts UTF-8 bytes, or UTF-16 code units, and never splits characters', () => {
+ expect(truncateFileName('日本語の動画です.mp4', 16)).toBe('日…です.mp4'); // 3 bytes per character
+ expect(truncateFileName('日本語の動画です.mp4', 8, true)).toBe('日…です.mp4');
+ expect(truncateFileName('😀😀😀😀.mkv', 16)).toBe('😀…😀.mkv'); // 4 bytes, or 2 code units, per emoji
+ expect(truncateFileName('😀😀😀😀.mkv', 10, true)).toBe('😀…😀.mkv');
});
});
diff --git a/src/common/util.ts b/src/common/util.ts
index 80f8f5a8..b6860862 100644
--- a/src/common/util.ts
+++ b/src/common/util.ts
@@ -18,29 +18,38 @@ function getCharUtf8Length(char: string) {
return 4;
}
-// file systems limit file name length in bytes (e.g. ext4) or UTF-16 code units (NTFS); UTF-8 bytes is never less than either
-export const getUtf8Length = (str: string) => [...str].reduce((acc, char) => acc + getCharUtf8Length(char), 0);
+// File systems limit the length of file names in UTF-8 bytes (e.g. ext4, APFS) or UTF-16 code units (NTFS)
+const getCharLength = (char: string, utf16: boolean) => (utf16 ? char.length : getCharUtf8Length(char));
-// Truncate to at most `maxBytes` UTF-8 bytes, without splitting a character
-export function truncateUtf8(str: string, maxBytes: number) {
- if (getUtf8Length(str) <= maxBytes) return str;
- let ret = '';
- let bytes = 0;
- for (const char of str) {
- bytes += getCharUtf8Length(char);
- if (bytes > maxBytes) break;
- ret += char;
+function takeChars(chars: string[], maxLength: number, utf16: boolean) {
+ const ret: string[] = [];
+ let length = 0;
+ for (const char of chars) {
+ length += getCharLength(char, utf16);
+ if (length > maxLength) break;
+ ret.push(char);
}
return ret;
}
-// Truncate a file name to `maxBytes` UTF-8 bytes, but keep its extension (if any)
-export function truncateFileName(name: string, maxBytes: number) {
- if (getUtf8Length(name) <= maxBytes) return name;
+const ellipsis = '…';
+
+// Truncate a file name to `maxLength`, keeping its extension (if any), and without splitting characters.
+// We cut out the middle, because the end of the name often has the parts that make it unique (e.g. cut times, segment number)
+export function truncateFileName(name: string, maxLength: number, utf16 = false) {
+ const getLength = (str: string) => [...str].reduce((acc, char) => acc + getCharLength(char, utf16), 0);
+ if (getLength(name) <= maxLength) return name;
+
const extIndex = name.lastIndexOf('.');
// only treat a short suffix as an extension, so that a dot early in the name doesn't make us exceed the limit
const ext = extIndex > 0 && name.length - extIndex <= 10 ? name.slice(extIndex) : '';
- return truncateUtf8(name.slice(0, name.length - ext.length), maxBytes - getUtf8Length(ext)) + ext;
+ const stem = name.slice(0, name.length - ext.length);
+ const chars = [...stem];
+
+ const budget = maxLength - getLength(ext) - getLength(ellipsis);
+ const head = takeChars(chars, Math.floor(budget / 2), utf16);
+ const tail = takeChars(chars.toReversed(), budget - getLength(head.join('')), utf16).reverse();
+ return head.join('') + ellipsis + tail.join('') + ext;
}
export function parseRatio(str: string, char = '/') {