The swiss army knife of lossless video/audio editing
Also fix custom channel layouts that aren't a standard speaker set
Claude541877ebfb60
diff --git a/src/common/util.test.ts b/src/common/util.test.tsindex 9c38f3a3..597836c2 100644--- a/src/common/util.test.ts+++ b/src/common/util.test.ts@@ -1,23 +1,28 @@// eslint-disable-next-line import/no-extraneous-dependenciesimport { describe, expect, test } from 'vitest';-import { getFixChannelLayoutFilter, hasUnsupportedChannelLayout } from './util.js';+import { getFixChannelLayoutFilter, hasCustomChannelLayout } from './util.js';-describe('hasUnsupportedChannelLayout', () => {- test('detects unused/unknown channels', () => {+describe('hasCustomChannelLayout', () => {+ test('detects layouts that ffmpeg could not express natively', () => {// e.g. DV/DVCPRO .mov captured by Final Cut / iMovie- expect(hasUnsupportedChannelLayout('4 channels (UNSD+UNSD+UNSD+UNSD)')).toBe(true);- expect(hasUnsupportedChannelLayout('3 channels (FL+FR+UNK)')).toBe(true);+ expect(hasCustomChannelLayout('4 channels (UNSD+UNSD+UNSD+UNSD)')).toBe(true);+ expect(hasCustomChannelLayout('3 channels (FL+FR+UNK)')).toBe(true);+ // known positions, but not a standard speaker set (e.g. a surround stem pair)+ expect(hasCustomChannelLayout('2 channels (BL+BR)')).toBe(true);+ expect(hasCustomChannelLayout('2 channels (SL+SR)')).toBe(true);+ expect(hasCustomChannelLayout('4 channels (FL+FR+FC+BL)')).toBe(true);});test('accepts layouts that swresample supports', () => {- expect(hasUnsupportedChannelLayout('stereo')).toBe(false);- expect(hasUnsupportedChannelLayout('mono')).toBe(false);- expect(hasUnsupportedChannelLayout('5.1(side)')).toBe(false);- expect(hasUnsupportedChannelLayout('2.1')).toBe(false);+ expect(hasCustomChannelLayout('stereo')).toBe(false);+ expect(hasCustomChannelLayout('mono')).toBe(false);+ expect(hasCustomChannelLayout('5.1(side)')).toBe(false);+ expect(hasCustomChannelLayout('2.1')).toBe(false);+ expect(hasCustomChannelLayout('quad')).toBe(false);// unspecified layouts are fine, ffmpeg just uses a default downmix- expect(hasUnsupportedChannelLayout('4 channels')).toBe(false);- expect(hasUnsupportedChannelLayout(undefined)).toBe(false);+ expect(hasCustomChannelLayout('4 channels')).toBe(false);+ expect(hasCustomChannelLayout(undefined)).toBe(false);});});@@ -25,6 +30,7 @@ describe('getFixChannelLayoutFilter', () => {test('relabels all channels of an unsupported layout', () => {expect(getFixChannelLayoutFilter({ channels: 4, channelLayout: '4 channels (UNSD+UNSD+UNSD+UNSD)' })).toBe('channelmap=0|1|2|3');expect(getFixChannelLayoutFilter({ channels: 3, channelLayout: '3 channels (FL+FR+UNK)' })).toBe('channelmap=0|1|2');+ expect(getFixChannelLayoutFilter({ channels: 2, channelLayout: '2 channels (BL+BR)' })).toBe('channelmap=0|1');expect(getFixChannelLayoutFilter({ channels: 1, channelLayout: '1 channels (UNSD)' })).toBe('channelmap=0');});diff --git a/src/common/util.ts b/src/common/util.tsindex d0bba7d6..f1f115f8 100644--- a/src/common/util.ts+++ b/src/common/util.ts@@ -20,23 +20,31 @@ export function parseRatio(str: string, char = '/') {return num / den;}-// ffmpeg's swresample cannot handle channel layouts that contain unknown ("UNK") or unused ("UNSD")-// channels, which ffprobe describes like "4 channels (UNSD+UNSD+UNSD+UNSD)". Such layouts occur in-// e.g. DV/DVCPRO .mov files, whose 'chan' atom labels every channel as "unused". Any operation that-// needs to resample or downmix such a stream then fails with:+// ffmpeg's swresample only accepts channel layouts that it considers "sane" (see sane_layout() in+// libswresample/rematrix.c): a native layout, or a custom one whose channels form a standard speaker+// set. Anything else is rejected outright when the resampler initializes, so any operation that+// resamples or downmixes such a stream fails with:// [SWR] Input channel layout '4 channels (UNSD+UNSD+UNSD+UNSD)' is not supported-// (swresample only accepts native or fully-specified custom layouts, see swr_init in libswresample)-export const hasUnsupportedChannelLayout = (channelLayout: string | undefined) => (- channelLayout != null && /\b(?:UNK|UNSD)\b/.test(channelLayout)+// There is no flag to relax this check.+//+// ffprobe describes a layout it could not express natively as "N channels (A+B+...)", e.g.+// "4 channels (UNSD+UNSD+UNSD+UNSD)" for DV/DVCPRO .mov files, whose 'chan' atom labels every channel+// as unused, or "2 channels (BL+BR)" for a surround stem pair. Layouts that do form a standard set are+// reported under their native name instead ("stereo", "5.1"), and those always work.+// A plain "N channels" (no positions at all) is also fine, which is what the fix below produces.+const customChannelLayoutRegex = /^\d+ channels \(/;++export const hasCustomChannelLayout = (channelLayout: string | undefined) => (+ channelLayout != null && customChannelLayoutRegex.test(channelLayout));-// The `channelmap` filter re-labels the channels without touching the samples, which turns the-// layout into a plain "N channels" (unspecified) layout that swresample does support. The stream-// then behaves exactly as if it had carried no channel layout information at all.+// The `channelmap` filter re-labels the channels without touching the samples, which turns the layout+// into a plain "N channels" (unspecified) layout. swresample handles those by skipping the rematrixing+// step entirely, so the stream then behaves exactly as if it had carried no channel layout information.export function getFixChannelLayoutFilter({ channels, channelLayout }: {channels?: number | undefined,channelLayout?: string | undefined,}) {- if (channels == null || channels <= 0 || !hasUnsupportedChannelLayout(channelLayout)) return undefined;+ if (channels == null || channels <= 0 || !hasCustomChannelLayout(channelLayout)) return undefined;return `channelmap=${Array.from({ length: channels }, (_, i) => i).join('|')}`;}diff --git a/src/renderer/src/hooks/useFfmpegOperations.ts b/src/renderer/src/hooks/useFfmpegOperations.tsindex 50963837..7f2d885b 100644--- a/src/renderer/src/hooks/useFfmpegOperations.ts+++ b/src/renderer/src/hooks/useFfmpegOperations.ts@@ -15,7 +15,7 @@ import { deleteDispositionValue, type AllFilesMeta, type Chapter, type CopyfileSimport type { LossyMode } from '../../../main';import { UserFacingError } from '../../errors';import mainApi from '../mainApi';-import { formatFfmpegNumber, getFixChannelLayoutFilter, getHwaccelArgs, hasUnsupportedChannelLayout } from '../../../common/util';+import { formatFfmpegNumber, getFixChannelLayoutFilter, getHwaccelArgs, hasCustomChannelLayout } from '../../../common/util';const { join, resolve, dirname } = window.require('node:path');const { writeFile, mkdir, access, constants: { W_OK } } = window.require('node:fs/promises');@@ -898,7 +898,7 @@ function useFfmpegOperations({ filePath, treatInputFileModifiedTimeAsStart, treaif (audio != null && audio !== 'copy') {try {const audioStreams = (await readFileFfprobeMeta(filePathArg)).streams.filter((s) => s.codec_type === 'audio');- const unsupportedStream = audioStreams.find((s) => hasUnsupportedChannelLayout(s.channel_layout));+ const unsupportedStream = audioStreams.find((s) => hasCustomChannelLayout(s.channel_layout));const sameChannelCount = new Set(audioStreams.map((s) => s.channels)).size === 1;if (unsupportedStream != null && sameChannelCount) {const filter = getFixChannelLayoutFilter({ channels: unsupportedStream.channels, channelLayout: unsupportedStream.channel_layout });