Skip to content

The swiss army knife of lossless video/audio editing

Use -itsoffset instead of shortening -t for the seek compensation

Claude8fc83b7b271a
diff --git a/src/renderer/src/hooks/useFfmpegOperations.ts b/src/renderer/src/hooks/useFfmpegOperations.ts
index 88118da7..622ca030 100644
--- a/src/renderer/src/hooks/useFfmpegOperations.ts
+++ b/src/renderer/src/hooks/useFfmpegOperations.ts
@@ -293,15 +293,16 @@ function useFfmpegOperations({ filePath, treatInputFileModifiedTimeAsStart, trea
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`.
+ // `-itsoffset` keeps the output's zero point (and so `-t`) at the original cut start. 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 })
+ ? getKeyframeCutSeekCompensation({ formatName: singleInputMeta.format.format_name, streams: singleInputMeta.streams, cutFrom: cutFromWithAdjustment })
: 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)] : [];
+ const seekCompensationArgs = seekCompensation > 0 ? ['-itsoffset', formatFfmpegNumber(seekCompensation)] : [];
+ const cutToArgs = cuttingEnd ? ['-t', formatFfmpegNumber(cutDuration)] : [];
// 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
@@ -334,7 +335,7 @@ function useFfmpegOperations({ filePath, treatInputFileModifiedTimeAsStart, trea
];
})
: [
- ...(ssBeforeInput ? cutFromArgs : []),
+ ...(ssBeforeInput ? [...cutFromArgs, ...seekCompensationArgs] : []),
'-i', copyFileStreamsFiltered[0]!.path,
...(!ssBeforeInput ? cutFromArgs : []),
...cutToArgs,
diff --git a/src/renderer/src/util/streams.test.ts b/src/renderer/src/util/streams.test.ts
index 2ea48fa2..9d9c5bca 100644
--- a/src/renderer/src/util/streams.test.ts
+++ b/src/renderer/src/util/streams.test.ts
@@ -223,15 +223,12 @@ 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);
+ expect(getKeyframeCutSeekCompensation({ formatName: 'matroska,webm', streams: bFrames, cutFrom: 6 })).toBe(compensation);
// no B-frames
- expect(getKeyframeCutSeekCompensation({ formatName: 'matroska,webm', streams: [{ has_b_frames: 0 }, {}], cutFrom: 6, cutDuration: 4 })).toBe(0);
+ expect(getKeyframeCutSeekCompensation({ formatName: 'matroska,webm', streams: [{ has_b_frames: 0 }, {}], cutFrom: 6 })).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);
+ expect(getKeyframeCutSeekCompensation({ formatName: 'mov,mp4,m4a,3gp,3g2,mj2', streams: bFrames, cutFrom: 6 })).toBe(0);
+ expect(getKeyframeCutSeekCompensation({ formatName: undefined, streams: bFrames, cutFrom: 6 })).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);
+ expect(getKeyframeCutSeekCompensation({ formatName: 'matroska,webm', streams: bFrames, cutFrom: 0 })).toBe(0);
});
diff --git a/src/renderer/src/util/streams.ts b/src/renderer/src/util/streams.ts
index 176f5acd..c3433847 100644
--- a/src/renderer/src/util/streams.ts
+++ b/src/renderer/src/util/streams.ts
@@ -319,19 +319,16 @@ export function getEffectiveAvoidNegativeTs({ avoidNegativeTs, allFilesMeta, cop
* 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
+ * input `-ss` (and pass as `-itsoffset`) to undo that. See https://github.com/mifi/lossless-cut/issues/1216
*/
-export function getKeyframeCutSeekCompensation({ formatName, streams, cutFrom, cutDuration }: {
+export function getKeyframeCutSeekCompensation({ formatName, streams, cutFrom }: {
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;
+ return 3 / 23 + 0.001;
}
export const getAudioStreams = <T extends Pick<FFprobeStream, 'codec_type'>>(streams: T[]) => streams.filter((stream) => stream.codec_type === 'audio');