The swiss army knife of lossless video/audio editing
align transferTimestamps
Mikael Finstad25050653c415
diff --git a/src/renderer/src/App.tsx b/src/renderer/src/App.tsxindex a3e959bd..9e853afe 100644--- a/src/renderer/src/App.tsx+++ b/src/renderer/src/App.tsx@@ -649,7 +649,7 @@ function App() {concatFiles, html5ifyDummy, cutMultiple, concatCutSegments, html5ify, fixInvalidDuration, extractStreams, tryDeleteFiles,} = useFfmpegOperations({ filePath, treatInputFileModifiedTimeAsStart, treatOutputFileModifiedTimeAsStart, needSmartCut, enableOverwriteOutput, outputPlaybackRate, cutFromAdjustmentFrames, cutToAdjustmentFrames, appendLastCommandsLog, smartCutCustomBitrate: smartCutBitrate, appendFfmpegCommandLog });- const { captureFrameFromTag, captureFrameFromFfmpeg, captureFramesRange } = useFrameCapture({ appendFfmpegCommandLog, formatTimecode, treatOutputFileModifiedTimeAsStart });+ const { captureFrameFromTag, captureFrameFromFfmpeg, captureFramesRange } = useFrameCapture({ appendFfmpegCommandLog, formatTimecode, treatInputFileModifiedTimeAsStart, treatOutputFileModifiedTimeAsStart });const html5ifyAndLoad = useCallback(async (cod: string | undefined, fp: string, speed: Html5ifyMode, hv: boolean, ha: boolean) => {const usesDummyVideo = speed === 'fastest';diff --git a/src/renderer/src/hooks/useFfmpegOperations.ts b/src/renderer/src/hooks/useFfmpegOperations.tsindex 4fdf2353..69bbce4f 100644--- a/src/renderer/src/hooks/useFfmpegOperations.ts+++ b/src/renderer/src/hooks/useFfmpegOperations.ts@@ -78,7 +78,7 @@ async function pathExists(path: string) {function useFfmpegOperations({ filePath, treatInputFileModifiedTimeAsStart, treatOutputFileModifiedTimeAsStart, needSmartCut, enableOverwriteOutput, outputPlaybackRate, cutFromAdjustmentFrames, cutToAdjustmentFrames, appendLastCommandsLog, smartCutCustomBitrate, appendFfmpegCommandLog }: {filePath: string | undefined,- treatInputFileModifiedTimeAsStart: boolean | null | undefined,+ treatInputFileModifiedTimeAsStart: boolean,treatOutputFileModifiedTimeAsStart: boolean | null | undefined,enableOverwriteOutput: boolean,needSmartCut: boolean,@@ -223,13 +223,13 @@ function useFfmpegOperations({ filePath, treatInputFileModifiedTimeAsStart, treaconst result = await runFfmpegConcat({ ffmpegArgs, concatTxt, totalDuration, onProgress });logStdoutStderr(result);- await transferTimestamps({ inPath: metadataFromPath, outPath, treatOutputFileModifiedTimeAsStart });+ await transferTimestamps({ inPath: metadataFromPath, outPath, treatInputFileModifiedTimeAsStart, treatOutputFileModifiedTimeAsStart, duration: totalDuration });return { haveExcludedStreams: excludedStreamIds.length > 0 };} finally {if (chaptersPath) await tryDeleteFiles([chaptersPath]);}- }, [appendLastCommandsLog, shouldSkipExistingFile, treatOutputFileModifiedTimeAsStart]);+ }, [appendLastCommandsLog, shouldSkipExistingFile, treatInputFileModifiedTimeAsStart, treatOutputFileModifiedTimeAsStart]);const losslessCutSingle = useCallback(async ({keyframeCut: ssBeforeInput, avoidNegativeTs, copyFileStreams, cutFrom, cutTo, chaptersPath, onProgress, outPath,@@ -771,9 +771,9 @@ function useFfmpegOperations({ filePath, treatInputFileModifiedTimeAsStart, treaconsole.log(new TextDecoder().decode(stdout));invariant(outPath != null);- await transferTimestamps({ inPath: filePathArg, outPath, treatOutputFileModifiedTimeAsStart });+ await transferTimestamps({ inPath: filePathArg, outPath, duration, treatInputFileModifiedTimeAsStart, treatOutputFileModifiedTimeAsStart });return outPath;- }, [appendFfmpegCommandLog, treatOutputFileModifiedTimeAsStart]);+ }, [appendFfmpegCommandLog, treatInputFileModifiedTimeAsStart, treatOutputFileModifiedTimeAsStart]);// This is just used to load something into the player with correct duration,// so that the user can seek and then we render frames using ffmpeg & MediaSource@@ -800,8 +800,8 @@ function useFfmpegOperations({ filePath, treatInputFileModifiedTimeAsStart, treaconst result = await runFfmpegWithProgress({ ffmpegArgs, duration, onProgress });logStdoutStderr(result);- await transferTimestamps({ inPath: filePathArg, outPath, treatOutputFileModifiedTimeAsStart });- }, [appendFfmpegCommandLog, treatOutputFileModifiedTimeAsStart]);+ await transferTimestamps({ inPath: filePathArg, outPath, duration, treatInputFileModifiedTimeAsStart, treatOutputFileModifiedTimeAsStart });+ }, [appendFfmpegCommandLog, treatInputFileModifiedTimeAsStart, treatOutputFileModifiedTimeAsStart]);// https://stackoverflow.com/questions/34118013/how-to-determine-webm-duration-using-ffprobeconst fixInvalidDuration = useCallback(async ({ fileFormat, customOutDir, onProgress }: {@@ -831,10 +831,10 @@ function useFfmpegOperations({ filePath, treatInputFileModifiedTimeAsStart, treaconst result = await runFfmpegWithProgress({ ffmpegArgs, onProgress });logStdoutStderr(result);- await transferTimestamps({ inPath: filePath, outPath, treatOutputFileModifiedTimeAsStart });+ await transferTimestamps({ inPath: filePath, outPath, duration: undefined, treatInputFileModifiedTimeAsStart, treatOutputFileModifiedTimeAsStart });return outPath;- }, [appendFfmpegCommandLog, filePath, treatOutputFileModifiedTimeAsStart]);+ }, [appendFfmpegCommandLog, filePath, treatInputFileModifiedTimeAsStart, treatOutputFileModifiedTimeAsStart]);function getPreferredCodecFormat(stream: LiteFFprobeStream) {const map = {diff --git a/src/renderer/src/hooks/useFrameCapture.ts b/src/renderer/src/hooks/useFrameCapture.tsindex 7077ecff..9329d7d2 100644--- a/src/renderer/src/hooks/useFrameCapture.ts+++ b/src/renderer/src/hooks/useFrameCapture.ts@@ -25,9 +25,10 @@ function getFrameFromVideo(video: HTMLVideoElement, format: CaptureFormat, qualireturn dataUriToBuffer(dataUri);}-export default ({ appendFfmpegCommandLog, formatTimecode, treatOutputFileModifiedTimeAsStart }: {+export default ({ appendFfmpegCommandLog, formatTimecode, treatInputFileModifiedTimeAsStart, treatOutputFileModifiedTimeAsStart }: {appendFfmpegCommandLog: (args: string[]) => void,formatTimecode: FormatTimecode,+ treatInputFileModifiedTimeAsStart: boolean,treatOutputFileModifiedTimeAsStart?: boolean | undefined | null,}) => {const captureFramesRange = useCallback(async ({ customOutDir, filePath, fps, fromTime, toTime, estimatedMaxNumFiles, captureFormat, quality, filter, onProgress, outputTimestamps }: {@@ -102,9 +103,9 @@ export default ({ appendFfmpegCommandLog, formatTimecode, treatOutputFileModifieconst args = await ffmpeg.captureFrame({ timestamp: time, videoPath: filePath, outPath, quality });appendFfmpegCommandLog(args);- await transferTimestamps({ inPath: filePath, outPath, cutFrom: time, treatOutputFileModifiedTimeAsStart });+ await transferTimestamps({ inPath: filePath, outPath, cutFrom: time, cutTo: time, duration: 0, treatInputFileModifiedTimeAsStart, treatOutputFileModifiedTimeAsStart });return outPath;- }, [appendFfmpegCommandLog, formatTimecode, treatOutputFileModifiedTimeAsStart]);+ }, [appendFfmpegCommandLog, formatTimecode, treatInputFileModifiedTimeAsStart, treatOutputFileModifiedTimeAsStart]);const captureFrameFromTag = useCallback(async ({ customOutDir, filePath, time, captureFormat, quality, video }: {customOutDir?: string | undefined,@@ -122,9 +123,9 @@ export default ({ appendFfmpegCommandLog, formatTimecode, treatOutputFileModifieconst outPath = getSuffixedOutPath({ customOutDir, filePath, nameSuffix: `${timecode}.${ext}` });await writeFile(outPath, buf);- await transferTimestamps({ inPath: filePath, outPath, cutFrom: time, treatOutputFileModifiedTimeAsStart });+ await transferTimestamps({ inPath: filePath, outPath, cutFrom: time, cutTo: time, duration: 0, treatInputFileModifiedTimeAsStart, treatOutputFileModifiedTimeAsStart });return outPath;- }, [formatTimecode, treatOutputFileModifiedTimeAsStart]);+ }, [formatTimecode, treatInputFileModifiedTimeAsStart, treatOutputFileModifiedTimeAsStart]);return {captureFramesRange,diff --git a/src/renderer/src/util.ts b/src/renderer/src/util.tsindex f00ce8ba..c663bf8b 100644--- a/src/renderer/src/util.ts+++ b/src/renderer/src/util.ts@@ -133,10 +133,18 @@ export const utimesWithRetry = async (path: string, atime: number, mtime: numberexport const getFrameDuration = (fps?: number) => 1 / (fps ?? 30);-export async function transferTimestamps({ inPath, outPath, cutFrom = 0, cutTo = 0, duration = 0, treatInputFileModifiedTimeAsStart = true, treatOutputFileModifiedTimeAsStart }: {- inPath: string, outPath: string, cutFrom?: number | undefined, cutTo?: number | undefined, duration?: number | undefined, treatInputFileModifiedTimeAsStart?: boolean | null | undefined, treatOutputFileModifiedTimeAsStart: boolean | null | undefined+export async function transferTimestamps({ inPath, outPath, cutFrom = 0, cutTo: cutToIn, duration = 0, treatInputFileModifiedTimeAsStart, treatOutputFileModifiedTimeAsStart }: {+ inPath: string,+ outPath: string,+ cutFrom?: number | undefined,+ cutTo?: number | undefined,+ duration: number | undefined,+ treatInputFileModifiedTimeAsStart: boolean,+ treatOutputFileModifiedTimeAsStart: boolean | null | undefined,}) {- if (treatOutputFileModifiedTimeAsStart == null) return; // null means disabled;+ if (treatOutputFileModifiedTimeAsStart == null) return; // null means time transfer is disabled (use current time);++ const cutTo = cutToIn ?? duration;// see https://github.com/mifi/lossless-cut/issues/1017#issuecomment-1049097115function calculateTime(fileTime: number) {