Skip to content

The swiss army knife of lossless video/audio editing

Set FLAC frame size explicitly, to avoid bigger files with ffmpeg 9

Claudebcca435656cb
diff --git a/src/renderer/src/hooks/useFfmpegOperations.ts b/src/renderer/src/hooks/useFfmpegOperations.ts
index ad3ab312..f6831ae0 100644
--- a/src/renderer/src/hooks/useFfmpegOperations.ts
+++ b/src/renderer/src/hooks/useFfmpegOperations.ts
@@ -6,7 +6,7 @@ import i18n from 'i18next';
import { getSuffixedOutPath, transferTimestamps, getOutFileExtension, getOutDir, getHtml5ifiedPath, unlinkWithRetry, getFrameDuration, isMac, html5ifiedPrefix, html5dummySuffix, assertFileExists } from '../util';
import { isCuttingStart, isCuttingEnd, runFfmpegWithProgress, getFfCommandLine, getDuration, createChaptersFromSegments, readFileFfprobeMeta, getExperimentalArgs, getVideoTimescaleArgs, logStdoutStderr, runFfmpegConcat, RefuseOverwriteError, runFfmpeg } from '../ffmpeg';
-import { getEffectiveAvoidNegativeTs, getKeyframeCutSeekCompensation, getMapStreamsArgs, getStreamIdsToCopy, isCopyingOnlyAudioStreams, isCropActive, isAspectRatioActive } from '../util/streams';
+import { flacFrameSize, getEffectiveAvoidNegativeTs, getKeyframeCutSeekCompensation, getMapStreamsArgs, getStreamIdsToCopy, isCopyingOnlyAudioStreams, isCropActive, isAspectRatioActive } from '../util/streams';
import { needsSmartCut, getCodecParams } from '../smartcut';
import { getGuaranteedSegments, isDurationValid } from '../segments';
import type { FFprobeStream } from '../../../common/ffprobe';
@@ -788,7 +788,7 @@ function useFfmpegOperations({ filePath, treatInputFileModifiedTimeAsStart, trea
// This is just a fast way of generating an empty dummy file
'-f', 'lavfi', '-i', 'anullsrc=channel_layout=stereo:sample_rate=44100',
'-t', String(duration),
- '-acodec', 'flac',
+ '-acodec', 'flac', '-frame_size', flacFrameSize,
'-y', outPath,
];
@@ -884,7 +884,7 @@ function useFfmpegOperations({ filePath, treatInputFileModifiedTimeAsStart, trea
if (isMac) {
audioArgs = ['-acodec', 'aac_at', '-b:a', '192k'];
} else {
- audioArgs = ['-acodec', 'flac'];
+ audioArgs = ['-acodec', 'flac', '-frame_size', flacFrameSize];
}
break;
}
@@ -893,7 +893,7 @@ function useFfmpegOperations({ filePath, treatInputFileModifiedTimeAsStart, trea
if (isMac) {
audioArgs = ['-acodec', 'aac_at', '-ar', '44100', '-ac', '2', '-b:a', '96k'];
} else {
- audioArgs = ['-acodec', 'flac', '-ar', '11025', '-ac', '2'];
+ audioArgs = ['-acodec', 'flac', '-frame_size', flacFrameSize, '-ar', '11025', '-ac', '2'];
}
break;
}
diff --git a/src/renderer/src/util/streams.ts b/src/renderer/src/util/streams.ts
index 825e4eeb..1d2aa14b 100644
--- a/src/renderer/src/util/streams.ts
+++ b/src/renderer/src/util/streams.ts
@@ -6,6 +6,10 @@ import type { FileStream } from '../ffmpeg';
// taken from `ffmpeg -codecs`
+// Since ffmpeg 9, the CLI forces fixed-frame-size encoders like FLAC to use the input's frame size (e.g. 1024 for AAC),
+// which makes FLAC files much bigger. 4608 is FLAC's standard block size, and compresses best also at low sample rates.
+export const flacFrameSize = '4608';
+
export const pcmAudioCodecs = [
'adpcm_4xm',
'adpcm_adx',
@@ -187,6 +191,7 @@ function getPerStreamFlags({ stream, outputIndex, outFormat, manuallyCopyDisposi
addArgs(`-ar:${outputIndex}`, '48000'); // maybe technically not lossless?
} else if (outFormat === 'flac' && needFlac && stream.codec_name === 'flac') { // https://github.com/mifi/lossless-cut/issues/1809
addCodecArgs('flac'); // lossless because flac is a lossless codec
+ addArgs(`-frame_size:${outputIndex}`, flacFrameSize);
} else {
addCodecArgs('copy');
}