1
0
mirror of https://github.com/mifi/lossless-cut.git synced 2024-11-22 10:22:31 +01:00

append also smart cut to last commands log

https://github.com/mifi/lossless-cut/issues/1997#issuecomment-2113149895
This commit is contained in:
Mikael Finstad 2024-05-15 22:42:38 +02:00
parent 9079aeeff2
commit 58adc59c9d
No known key found for this signature in database
GPG Key ID: 25AB36E3E81CBC26
3 changed files with 21 additions and 17 deletions

View File

@ -249,7 +249,7 @@ function App() {
}); });
}, [zoomedDuration]); }, [zoomedDuration]);
const appendFfmpegCommandLog = useCallback((command: string) => { const appendLastCommandsLog = useCallback((command: string) => {
setFfmpegCommandLog((old) => [...old, { command, time: new Date() }]); setFfmpegCommandLog((old) => [...old, { command, time: new Date() }]);
}, []); }, []);
@ -836,7 +836,7 @@ function App() {
const { const {
concatFiles, html5ifyDummy, cutMultiple, autoConcatCutSegments, html5ify, fixInvalidDuration, concatFiles, html5ifyDummy, cutMultiple, autoConcatCutSegments, html5ify, fixInvalidDuration,
} = useFfmpegOperations({ filePath, treatInputFileModifiedTimeAsStart, treatOutputFileModifiedTimeAsStart, needSmartCut, enableOverwriteOutput, outputPlaybackRate, cutFromAdjustmentFrames, appendFfmpegCommandLog }); } = useFfmpegOperations({ filePath, treatInputFileModifiedTimeAsStart, treatOutputFileModifiedTimeAsStart, needSmartCut, enableOverwriteOutput, outputPlaybackRate, cutFromAdjustmentFrames, appendLastCommandsLog });
const html5ifyAndLoad = useCallback(async (cod: string | undefined, fp: string, speed: Html5ifyMode, hv: boolean, ha: boolean) => { const html5ifyAndLoad = useCallback(async (cod: string | undefined, fp: string, speed: Html5ifyMode, hv: boolean, ha: boolean) => {
const usesDummyVideo = speed === 'fastest'; const usesDummyVideo = speed === 'fastest';

View File

@ -705,4 +705,6 @@ export async function cutEncodeSmartPart({ filePath, cutFrom, cutTo, outPath, ou
]; ];
await runFfmpeg(ffmpegArgs); await runFfmpeg(ffmpegArgs);
return ffmpegArgs;
} }

View File

@ -60,7 +60,7 @@ async function tryDeleteFiles(paths: string[]) {
return pMap(paths, (path) => unlinkWithRetry(path).catch((err) => console.error('Failed to delete', path, err)), { concurrency: 5 }); return pMap(paths, (path) => unlinkWithRetry(path).catch((err) => console.error('Failed to delete', path, err)), { concurrency: 5 });
} }
function useFfmpegOperations({ filePath, treatInputFileModifiedTimeAsStart, treatOutputFileModifiedTimeAsStart, needSmartCut, enableOverwriteOutput, outputPlaybackRate, cutFromAdjustmentFrames, appendFfmpegCommandLog }: { function useFfmpegOperations({ filePath, treatInputFileModifiedTimeAsStart, treatOutputFileModifiedTimeAsStart, needSmartCut, enableOverwriteOutput, outputPlaybackRate, cutFromAdjustmentFrames, appendLastCommandsLog }: {
filePath: string | undefined, filePath: string | undefined,
treatInputFileModifiedTimeAsStart: boolean | null | undefined, treatInputFileModifiedTimeAsStart: boolean | null | undefined,
treatOutputFileModifiedTimeAsStart: boolean | null | undefined, treatOutputFileModifiedTimeAsStart: boolean | null | undefined,
@ -68,9 +68,11 @@ function useFfmpegOperations({ filePath, treatInputFileModifiedTimeAsStart, trea
needSmartCut: boolean, needSmartCut: boolean,
outputPlaybackRate: number, outputPlaybackRate: number,
cutFromAdjustmentFrames: number, cutFromAdjustmentFrames: number,
appendFfmpegCommandLog: (a: string) => void, appendLastCommandsLog: (a: string) => void,
}) { }) {
const shouldSkipExistingFile = useCallback(async (path) => { const appendFfmpegCommandLog = useCallback((args: string[]) => appendLastCommandsLog(getFfCommandLine('ffmpeg', args)), [appendLastCommandsLog]);
const shouldSkipExistingFile = useCallback(async (path: string) => {
const skip = !enableOverwriteOutput && await pathExists(path); const skip = !enableOverwriteOutput && await pathExists(path);
if (skip) console.log('Not overwriting existing file', path); if (skip) console.log('Not overwriting existing file', path);
return skip; return skip;
@ -88,7 +90,7 @@ function useFfmpegOperations({ filePath, treatInputFileModifiedTimeAsStart, trea
const durations = await pMap(paths, getDuration, { concurrency: 1 }); const durations = await pMap(paths, getDuration, { concurrency: 1 });
const totalDuration = sum(durations); const totalDuration = sum(durations);
let chaptersPath; let chaptersPath: string | undefined;
if (chapters) { if (chapters) {
const chaptersWithNames = chapters.map((chapter, i) => ({ ...chapter, name: chapter.name || `Chapter ${i + 1}` })); const chaptersWithNames = chapters.map((chapter, i) => ({ ...chapter, name: chapter.name || `Chapter ${i + 1}` }));
invariant(outDir != null); invariant(outDir != null);
@ -115,13 +117,13 @@ function useFfmpegOperations({ filePath, treatInputFileModifiedTimeAsStart, trea
'-i', '-', '-i', '-',
]); ]);
let metadataSourceIndex; let metadataSourceIndex: number | undefined;
if (preserveMetadataOnMerge) { if (preserveMetadataOnMerge) {
// If preserve metadata, add the first file (we will get metadata from this input) // If preserve metadata, add the first file (we will get metadata from this input)
metadataSourceIndex = addInput(['-i', metadataFromPath]); metadataSourceIndex = addInput(['-i', metadataFromPath]);
} }
let chaptersInputIndex; let chaptersInputIndex: number | undefined;
if (chaptersPath) { if (chaptersPath) {
// if chapters, add chapters source file // if chapters, add chapters source file
chaptersInputIndex = addInput(getChaptersInputArgs(chaptersPath)); chaptersInputIndex = addInput(getChaptersInputArgs(chaptersPath));
@ -148,9 +150,9 @@ function useFfmpegOperations({ filePath, treatInputFileModifiedTimeAsStart, trea
// -map_metadata 0 with concat demuxer doesn't transfer metadata from the concat'ed file input (index 0) when merging. // -map_metadata 0 with concat demuxer doesn't transfer metadata from the concat'ed file input (index 0) when merging.
// So we use the first file file (index 1) for metadata // So we use the first file file (index 1) for metadata
// Can only do this if allStreams (-map 0) is set // Can only do this if allStreams (-map 0) is set
...(metadataSourceIndex != null ? ['-map_metadata', metadataSourceIndex] : []), ...(metadataSourceIndex != null ? ['-map_metadata', String(metadataSourceIndex)] : []),
...(chaptersInputIndex != null ? ['-map_chapters', chaptersInputIndex] : []), ...(chaptersInputIndex != null ? ['-map_chapters', String(chaptersInputIndex)] : []),
...getMovFlags({ preserveMovData, movFastStart }), ...getMovFlags({ preserveMovData, movFastStart }),
...getMatroskaFlags(), ...getMatroskaFlags(),
@ -175,7 +177,7 @@ function useFfmpegOperations({ filePath, treatInputFileModifiedTimeAsStart, trea
const fullCommandLine = `echo -e "${concatTxt.replace(/\n/, '\\n')}" | ${ffmpegCommandLine}`; const fullCommandLine = `echo -e "${concatTxt.replace(/\n/, '\\n')}" | ${ffmpegCommandLine}`;
console.log(fullCommandLine); console.log(fullCommandLine);
appendFfmpegCommandLog(fullCommandLine); appendLastCommandsLog(fullCommandLine);
const result = await runFfmpegConcat({ ffmpegArgs, concatTxt, totalDuration, onProgress }); const result = await runFfmpegConcat({ ffmpegArgs, concatTxt, totalDuration, onProgress });
logStdoutStderr(result); logStdoutStderr(result);
@ -186,7 +188,7 @@ function useFfmpegOperations({ filePath, treatInputFileModifiedTimeAsStart, trea
} finally { } finally {
if (chaptersPath) await tryDeleteFiles([chaptersPath]); if (chaptersPath) await tryDeleteFiles([chaptersPath]);
} }
}, [appendFfmpegCommandLog, shouldSkipExistingFile, treatOutputFileModifiedTimeAsStart]); }, [appendLastCommandsLog, shouldSkipExistingFile, treatOutputFileModifiedTimeAsStart]);
const losslessCutSingle = useCallback(async ({ const losslessCutSingle = useCallback(async ({
keyframeCut: ssBeforeInput, avoidNegativeTs, copyFileStreams, cutFrom, cutTo, chaptersPath, onProgress, outPath, keyframeCut: ssBeforeInput, avoidNegativeTs, copyFileStreams, cutFrom, cutTo, chaptersPath, onProgress, outPath,
@ -334,10 +336,7 @@ function useFfmpegOperations({ filePath, treatInputFileModifiedTimeAsStart, trea
'-f', outFormat, '-y', outPath, '-f', outFormat, '-y', outPath,
]; ];
const ffmpegCommandLine = getFfCommandLine('ffmpeg', ffmpegArgs); appendFfmpegCommandLog(ffmpegArgs);
// console.log(ffmpegCommandLine);
appendFfmpegCommandLog(ffmpegCommandLine);
const result = await runFfmpegWithProgress({ ffmpegArgs, duration: cutDuration, onProgress }); const result = await runFfmpegWithProgress({ ffmpegArgs, duration: cutDuration, onProgress });
logStdoutStderr(result); logStdoutStderr(result);
@ -434,7 +433,8 @@ function useFfmpegOperations({ filePath, treatInputFileModifiedTimeAsStart, trea
if (videoCodec == null || videoBitrate == null || videoTimebase == null) throw new Error(); if (videoCodec == null || videoBitrate == null || videoTimebase == null) throw new Error();
invariant(filePath != null); invariant(filePath != null);
invariant(outFormat != null); invariant(outFormat != null);
await cutEncodeSmartPart({ filePath, cutFrom, cutTo, outPath, outFormat, videoCodec, videoBitrate, videoStreamIndex, videoTimebase, allFilesMeta, copyFileStreams: copyFileStreamsFiltered, ffmpegExperimental }); const args = await cutEncodeSmartPart({ filePath, cutFrom, cutTo, outPath, outFormat, videoCodec, videoBitrate, videoStreamIndex, videoTimebase, allFilesMeta, copyFileStreams: copyFileStreamsFiltered, ffmpegExperimental });
appendFfmpegCommandLog(args);
} }
// If we are cutting within two keyframes, just encode the whole part and return that // If we are cutting within two keyframes, just encode the whole part and return that
@ -568,6 +568,8 @@ function useFfmpegOperations({ filePath, treatInputFileModifiedTimeAsStart, trea
'-y', outPath, '-y', outPath,
]; ];
appendFfmpegCommandLog(ffmpegArgs);
const result = await runFfmpegWithProgress({ ffmpegArgs, duration, onProgress }); const result = await runFfmpegWithProgress({ ffmpegArgs, duration, onProgress });
logStdoutStderr(result); logStdoutStderr(result);