Skip to content

The swiss army knife of lossless video/audio editing

Compensate ffmpeg's B-frame seek offset for Matroska keyframe cuts

Claude1c4bb77196f2
diff --git a/src/renderer/src/hooks/useFfmpegOperations.ts b/src/renderer/src/hooks/useFfmpegOperations.ts
index ab00db12..88118da7 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, isCopyingOnlyAudioStreams } from '../util/streams';
+import { getEffectiveAvoidNegativeTs, getKeyframeCutSeekCompensation, getMapStreamsArgs, getStreamIdsToCopy, isCopyingOnlyAudioStreams } from '../util/streams';
import { needsSmartCut, getCodecParams } from '../smartcut';
import { getGuaranteedSegments, isDurationValid } from '../segments';
import type { FFprobeStream } from '../../../common/ffprobe';
@@ -290,12 +290,19 @@ function useFfmpegOperations({ filePath, treatInputFileModifiedTimeAsStart, trea
let cutDuration = cutToWithAdjustment - cutFromWithAdjustment;
if (detectedFps != null) cutDuration = Math.max(cutDuration, frameDuration); // ensure at least one frame duration
- // Don't cut if not needed: https://github.com/mifi/lossless-cut/issues/50
- const cutFromArgs = cuttingStart ? ['-ss', formatFfmpegNumber(cutFromWithAdjustment)] : [];
- const cutToArgs = cuttingEnd ? ['-t', formatFfmpegNumber(cutDuration)] : [];
-
const copyFileStreamsFiltered = copyFileStreams.filter(({ streamIds }) => streamIds.length > 0);
+ // Undo ffmpeg's earlier seek for Matroska with B-frames, so the cut doesn't start one keyframe too early (#1216)
+ // Shifting both `-ss` and `-t` keeps the end time unchanged. Only for a single input with `-ss` before `-i`.
+ const singleInputMeta = copyFileStreamsFiltered.length === 1 ? allFilesMeta[copyFileStreamsFiltered[0]!.path] : undefined;
+ const seekCompensation = ssBeforeInput && cuttingStart && singleInputMeta != null
+ ? getKeyframeCutSeekCompensation({ formatName: singleInputMeta.format.format_name, streams: singleInputMeta.streams, cutFrom: cutFromWithAdjustment, cutDuration: cuttingEnd ? cutDuration : undefined })
+ : 0;
+
+ // Don't cut if not needed: https://github.com/mifi/lossless-cut/issues/50
+ const cutFromArgs = cuttingStart ? ['-ss', formatFfmpegNumber(cutFromWithAdjustment + seekCompensation)] : [];
+ const cutToArgs = cuttingEnd ? ['-t', formatFfmpegNumber(cutDuration - seekCompensation)] : [];
+
// remove -avoid_negative_ts make_zero when not cutting start (no -ss), or else some videos get blank first frame in QuickLook
// note: `make_zero`/`make_non_negative` get downgraded to `auto` when copying a cover art stream, or else the
// output gets the segment's end time as its duration, see https://github.com/mifi/lossless-cut/issues/3009
diff --git a/src/renderer/src/types.ts b/src/renderer/src/types.ts
index 166e16d2..9acb613a 100644
--- a/src/renderer/src/types.ts
+++ b/src/renderer/src/types.ts
@@ -142,7 +142,7 @@ export type CopyfileStreams = {
export interface Chapter { start: number, end: number, name?: string | undefined }
-export type LiteFFprobeStream = Pick<FFprobeStream, 'index' | 'codec_type' | 'codec_tag' | 'codec_name' | 'disposition' | 'tags' | 'sample_rate' | 'time_base'>;
+export type LiteFFprobeStream = Pick<FFprobeStream, 'index' | 'codec_type' | 'codec_tag' | 'codec_name' | 'disposition' | 'tags' | 'sample_rate' | 'time_base' | 'has_b_frames'>;
export interface FileStats {
size: number | bigint,
diff --git a/src/renderer/src/util/streams.test.ts b/src/renderer/src/util/streams.test.ts
index 32b08694..2ea48fa2 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, isCopyingOnlyAudioStreams } from './streams';
+import { getEffectiveAvoidNegativeTs, getKeyframeCutSeekCompensation, getMapStreamsArgs, getStreamIdsToCopy, isCopyingOnlyAudioStreams } from './streams';
import type { FFprobeStreamDisposition } from '../../../common/ffprobe';
import type { LiteFFprobeStream } from '../types';
@@ -218,3 +218,20 @@ test('isCopyingOnlyAudioStreams, false when copying video, subtitles, unknown st
expect(isCopyingOnlyAudioStreams({ allFilesMeta, copyFileStreams: [{ path, streamIds: [] }] })).toBe(false);
expect(isCopyingOnlyAudioStreams({ allFilesMeta, copyFileStreams: [] })).toBe(false);
});
+
+test('getKeyframeCutSeekCompensation', () => {
+ const bFrames = [{ has_b_frames: 2 }, {}];
+ const compensation = 3 / 23 + 0.001;
+
+ expect(getKeyframeCutSeekCompensation({ formatName: 'matroska,webm', streams: bFrames, cutFrom: 6, cutDuration: 4 })).toBe(compensation);
+ expect(getKeyframeCutSeekCompensation({ formatName: 'matroska,webm', streams: bFrames, cutFrom: 6, cutDuration: undefined })).toBe(compensation);
+ // no B-frames
+ expect(getKeyframeCutSeekCompensation({ formatName: 'matroska,webm', streams: [{ has_b_frames: 0 }, {}], cutFrom: 6, cutDuration: 4 })).toBe(0);
+ // formats with AVFMT_SEEK_TO_PTS, or not handled
+ expect(getKeyframeCutSeekCompensation({ formatName: 'mov,mp4,m4a,3gp,3g2,mj2', streams: bFrames, cutFrom: 6, cutDuration: 4 })).toBe(0);
+ expect(getKeyframeCutSeekCompensation({ formatName: undefined, streams: bFrames, cutFrom: 6, cutDuration: 4 })).toBe(0);
+ // not cutting start
+ expect(getKeyframeCutSeekCompensation({ formatName: 'matroska,webm', streams: bFrames, cutFrom: 0, cutDuration: 4 })).toBe(0);
+ // segment too short, `-t` would become <= 0
+ expect(getKeyframeCutSeekCompensation({ formatName: 'matroska,webm', streams: bFrames, cutFrom: 6, cutDuration: 0.1 })).toBe(0);
+});
diff --git a/src/renderer/src/util/streams.ts b/src/renderer/src/util/streams.ts
index ba657399..176f5acd 100644
--- a/src/renderer/src/util/streams.ts
+++ b/src/renderer/src/util/streams.ts
@@ -315,6 +315,25 @@ export function getEffectiveAvoidNegativeTs({ avoidNegativeTs, allFilesMeta, cop
return 'auto';
}
+/**
+ * For input formats without AVFMT_SEEK_TO_PTS (e.g. Matroska/WebM), when any stream has B-frames, ffmpeg seeks
+ * 3/23s earlier than the input `-ss` (`dts_heuristic` in fftools/ffmpeg_demux.c). With `-ss` before `-i`, a keyframe cut
+ * starting on (or up to ~0.13s after) a keyframe then starts from the *previous* keyframe. Returns how much to add to the
+ * input `-ss` (and subtract from `-t`) to undo that. See https://github.com/mifi/lossless-cut/issues/1216
+ */
+export function getKeyframeCutSeekCompensation({ formatName, streams, cutFrom, cutDuration }: {
+ formatName: string | undefined,
+ streams: Pick<FFprobeStream, 'has_b_frames'>[],
+ cutFrom: number,
+ cutDuration: number | undefined,
+}) {
+ const compensation = 3 / 23 + 0.001;
+ if (cutFrom <= 0 || !(formatName ?? '').split(',').some((f) => isMatroska(f))) return 0;
+ if (!streams.some((stream) => (stream.has_b_frames ?? 0) > 0)) return 0;
+ if (cutDuration != null && cutDuration <= compensation) return 0; // would make `-t` <= 0
+ return compensation;
+}
+
export const getAudioStreams = <T extends Pick<FFprobeStream, 'codec_type'>>(streams: T[]) => streams.filter((stream) => stream.codec_type === 'audio');
export const getRealVideoStreams = <T extends Pick<FFprobeStream, 'codec_type' | 'disposition'>>(streams: T[]) => streams.filter((stream) => stream.codec_type === 'video' && !isStreamThumbnail(stream));
export const getSubtitleStreams = <T extends Pick<FFprobeStream, 'codec_type'>>(streams: T[]) => streams.filter((stream) => stream.codec_type === 'subtitle');