Skip to content

The swiss army knife of lossless video/audio editing

Keep leading non-keyframe audio packets when cutting audio-only files

Claude70f2663a7a7c
diff --git a/docs/troubleshooting.md b/docs/troubleshooting.md
index c1da2955..e383cd36 100644
--- a/docs/troubleshooting.md
+++ b/docs/troubleshooting.md
@@ -20,6 +20,7 @@ Each segment's *start cut time* will normally (but not always) be "rounded" to t
- Your mileage may vary when it comes to *Keyframe cut mode*. Most common video files need *Keyframe cut* enabled, but you may need to try both values. [ffmpeg](https://trac.ffmpeg.org/wiki/Seeking) also has documentation about these two seek/cut modes. In `ffmpeg`, *Keyframe cut* corresponds to `-ss` *before* `-i`.
- Try to change `avoid_negative_ts` (in export options).
- Try also to set the **start**-cutpoint a few frames **before or after** the nearest keyframe (may also solve audio sync issues).
+- For **audio-only** files (e.g. `.m4a`) where the cut starts several seconds early with *Keyframe cut mode* on, and late with it off, the file probably has a sync sample table that only flags some audio packets as keyframes. Disable *Keyframe cut mode*, or first export the whole file without cutting (this remuxes it and drops the table) and then cut the exported file.
- You may try to enable the experimental "Smart cut" mode to allow cutting between keyframes. However it will not work for many files.
- Currently, the only way to review the exported file (to check the actual cutpoints) is to run the export (possibly with only one segment enabled to speed up) and then manually check the output file. See also [#1887](https://github.com/mifi/lossless-cut/issues/1887)
diff --git a/src/main/ffmpeg.ts b/src/main/ffmpeg.ts
index 4ef96948..aa78073e 100644
--- a/src/main/ffmpeg.ts
+++ b/src/main/ffmpeg.ts
@@ -236,6 +236,9 @@ export async function renderWaveformPng({ filePath, start, duration, resample, c
'-ar', String(resample),
] : [
'-c', 'copy',
+ // we seek after the input, so keep leading non-keyframe packets, or else the waveform is shifted
+ // for audio files where not all packets are flagged as keyframes (see isCopyingOnlyAudioStreams in renderer)
+ '-copyinkf',
]),
'-f', 'matroska', // mpegts doesn't support vorbis etc
'-',
diff --git a/src/renderer/src/hooks/useFfmpegOperations.ts b/src/renderer/src/hooks/useFfmpegOperations.ts
index 7f2d885b..1703ec6c 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, getMapStreamsArgs, getStreamIdsToCopy } from '../util/streams';
+import { getEffectiveAvoidNegativeTs, getMapStreamsArgs, getStreamIdsToCopy, isCopyingOnlyAudioStreams } from '../util/streams';
import { needsSmartCut, getCodecParams } from '../smartcut';
import { getGuaranteedSegments, isDurationValid } from '../segments';
import type { FFprobeStream } from '../../../common/ffprobe';
@@ -302,6 +302,10 @@ function useFfmpegOperations({ filePath, treatInputFileModifiedTimeAsStart, trea
const effectiveAvoidNegativeTs = getEffectiveAvoidNegativeTs({ avoidNegativeTs, allFilesMeta, copyFileStreams: copyFileStreamsFiltered });
const avoidNegativeTsArgs = cuttingStart && effectiveAvoidNegativeTs && ssBeforeInput ? ['-avoid_negative_ts', String(effectiveAvoidNegativeTs)] : [];
+ // When seeking after the input (keyframe cut mode off), keep leading non-keyframe packets for audio-only exports,
+ // or else the cut starts late for audio files where not all packets are flagged as keyframes. See isCopyingOnlyAudioStreams
+ const copyInitialNonKeyframesArgs = cuttingStart && !ssBeforeInput && isCopyingOnlyAudioStreams({ allFilesMeta, copyFileStreams: copyFileStreamsFiltered }) ? ['-copyinkf'] : [];
+
// If cutting multiple files, `-ss` must be before `-i`, regardless of `ssBeforeInput` choice
// and it seems that `-t` must be after `-i` #896
const inputFilesArgs = copyFileStreamsFiltered.length > 1
@@ -464,6 +468,8 @@ function useFfmpegOperations({ filePath, treatInputFileModifiedTimeAsStart, trea
...avoidNegativeTsArgs,
+ ...copyInitialNonKeyframesArgs,
+
...mapStreamsArgs,
...getPreserveMetadata(),
diff --git a/src/renderer/src/util/streams.test.ts b/src/renderer/src/util/streams.test.ts
index 8ff92c76..32b08694 100644
--- a/src/renderer/src/util/streams.test.ts
+++ b/src/renderer/src/util/streams.test.ts
@@ -1,6 +1,6 @@
import { test, expect } from 'vitest';
-import { getEffectiveAvoidNegativeTs, getMapStreamsArgs, getStreamIdsToCopy } from './streams';
+import { getEffectiveAvoidNegativeTs, getMapStreamsArgs, getStreamIdsToCopy, isCopyingOnlyAudioStreams } from './streams';
import type { FFprobeStreamDisposition } from '../../../common/ffprobe';
import type { LiteFFprobeStream } from '../types';
@@ -202,3 +202,19 @@ test('getEffectiveAvoidNegativeTs, leaves other values alone', () => {
expect(getEffectiveAvoidNegativeTs({ avoidNegativeTs: 'disabled', allFilesMeta, copyFileStreams })).toBe('disabled');
expect(getEffectiveAvoidNegativeTs({ avoidNegativeTs: undefined, allFilesMeta, copyFileStreams })).toBe(undefined);
});
+
+// see `streams1`: 0 is cover art, 1 and 4 are audio, 2 and 3 are video, 7 is subtitle
+test('isCopyingOnlyAudioStreams, true for audio (with or without cover art)', () => {
+ expect(isCopyingOnlyAudioStreams({ allFilesMeta, copyFileStreams: [{ path, streamIds: [1] }] })).toBe(true);
+ expect(isCopyingOnlyAudioStreams({ allFilesMeta, copyFileStreams: [{ path, streamIds: [1, 4] }] })).toBe(true);
+ expect(isCopyingOnlyAudioStreams({ allFilesMeta, copyFileStreams: [{ path, streamIds: [0, 1] }] })).toBe(true);
+ expect(isCopyingOnlyAudioStreams({ allFilesMeta, copyFileStreams: [{ path, streamIds: [0] }] })).toBe(true); // cover art alone is a single keyframe, so harmless
+});
+
+test('isCopyingOnlyAudioStreams, false when copying video, subtitles, unknown streams or nothing', () => {
+ expect(isCopyingOnlyAudioStreams({ allFilesMeta, copyFileStreams: [{ path, streamIds: [1, 2] }] })).toBe(false);
+ expect(isCopyingOnlyAudioStreams({ allFilesMeta, copyFileStreams: [{ path, streamIds: [1, 7] }] })).toBe(false);
+ expect(isCopyingOnlyAudioStreams({ allFilesMeta, copyFileStreams: [{ path, streamIds: [1, 99] }] })).toBe(false);
+ expect(isCopyingOnlyAudioStreams({ allFilesMeta, copyFileStreams: [{ path, streamIds: [] }] })).toBe(false);
+ expect(isCopyingOnlyAudioStreams({ allFilesMeta, copyFileStreams: [] })).toBe(false);
+});
diff --git a/src/renderer/src/util/streams.ts b/src/renderer/src/util/streams.ts
index 250e1d93..ba657399 100644
--- a/src/renderer/src/util/streams.ts
+++ b/src/renderer/src/util/streams.ts
@@ -263,14 +263,35 @@ export function isStreamThumbnail(stream: Pick<FFprobeStream, 'codec_type' | 'di
return stream && stream.codec_type === 'video' && stream.disposition?.[attachedPicDisposition] === 1;
}
+/** Resolves every stream that is going to be copied to the output. Unknown streams resolve to `undefined`. */
+const getCopiedStreams = ({ allFilesMeta, copyFileStreams }: {
+ allFilesMeta: AllFilesStreamMeta,
+ copyFileStreams: CopyfileStreams,
+}) => copyFileStreams.flatMap(({ path, streamIds }) => streamIds.map((streamId) => getStreamById({ allFilesMeta, streamId, path })));
+
function isCopyingThumbnailStream({ allFilesMeta, copyFileStreams }: {
allFilesMeta: AllFilesStreamMeta,
copyFileStreams: CopyfileStreams,
}) {
- return copyFileStreams.some(({ path, streamIds }) => streamIds.some((streamId) => {
- const stream = getStreamById({ allFilesMeta, streamId, path });
- return stream != null && isStreamThumbnail(stream);
- }));
+ return getCopiedStreams({ allFilesMeta, copyFileStreams }).some((stream) => stream != null && isStreamThumbnail(stream));
+}
+
+/**
+ * Whether every stream being copied is audio (cover art is allowed, as it is a single keyframe anyway).
+ *
+ * Used to decide whether it is safe to pass `-copyinkf` to ffmpeg. When cutting with `-ss` *after* `-i`
+ * (keyframe cut mode off), ffmpeg's stream copy discards packets until it sees one flagged as a keyframe.
+ * Audio packets are normally all keyframes, but some audio-only MP4/M4A files carry a sync sample table
+ * (`stss`) that only flags a packet every few seconds. The cut then starts seconds late (and with `-ss`
+ * before `-i`, seconds early). `-copyinkf` makes ffmpeg keep the leading non-keyframe packets, which is
+ * harmless for audio, but would produce broken frames for video, so we only do it for audio-only exports.
+ */
+export function isCopyingOnlyAudioStreams({ allFilesMeta, copyFileStreams }: {
+ allFilesMeta: AllFilesStreamMeta,
+ copyFileStreams: CopyfileStreams,
+}) {
+ const streams = getCopiedStreams({ allFilesMeta, copyFileStreams });
+ return streams.length > 0 && streams.every((stream) => stream != null && (stream.codec_type === 'audio' || isStreamThumbnail(stream)));
}
/**