The swiss army knife of lossless video/audio editing
fix logic issue with smart cut
Mikael Finstad9130924e14eb
diff --git a/src/App.jsx b/src/App.jsxindex a07bc665..c6474926 100644--- a/src/App.jsx+++ b/src/App.jsx@@ -187,10 +187,6 @@ const App = memo(() => {ffmpegSetCustomFfPath(customFfPath);}, [customFfPath]);- const {- concatFiles, html5ifyDummy, cutMultiple, autoConcatCutSegments, html5ify, fixInvalidDuration,- } = useFfmpegOperations({ filePath, enableTransferTimestamps });-const outSegTemplateOrDefault = outSegTemplate || defaultOutSegTemplate;useEffect(() => {@@ -348,8 +344,8 @@ const App = memo(() => {}, [isFileOpened]);const {- cutSegments, cutSegmentsHistory, createSegmentsFromKeyframes, shuffleSegments, detectBlackScenes, detectSilentScenes, detectSceneChanges, removeCutSegment, invertAllSegments, fillSegmentsGaps, combineOverlappingSegments, shiftAllSegmentTimes, alignSegmentTimesToKeyframes, onViewSegmentTags, updateSegOrder, updateSegOrders, reorderSegsByStartTime, addSegment, setCutStart, setCutEnd, onLabelSegment, splitCurrentSegment, createNumSegments, createFixedDurationSegments, createRandomSegments, apparentCutSegments, haveInvalidSegs, currentSegIndexSafe, currentCutSeg, currentApparentCutSeg, inverseCutSegments, clearSegments, loadCutSegments, isSegmentSelected, setCutTime, getSegApparentEnd, setCurrentSegIndex, onLabelSelectedSegments, deselectAllSegments, selectAllSegments, selectOnlyCurrentSegment, toggleCurrentSegmentSelected, removeSelectedSegments, setDeselectedSegmentIds, onSelectSegmentsByLabel, toggleSegmentSelected, selectOnlySegment, getApparentCutSegmentById, selectedSegments, selectedSegmentsOrInverse, nonFilteredSegmentsOrInverse,- } = useSegments({ filePath, workingRef, setWorking, setCutProgress, mainVideoStream, duration, getRelevantTime, maxLabelLength, checkFileOpened, invertCutSegments });+ cutSegments, cutSegmentsHistory, createSegmentsFromKeyframes, shuffleSegments, detectBlackScenes, detectSilentScenes, detectSceneChanges, removeCutSegment, invertAllSegments, fillSegmentsGaps, combineOverlappingSegments, shiftAllSegmentTimes, alignSegmentTimesToKeyframes, onViewSegmentTags, updateSegOrder, updateSegOrders, reorderSegsByStartTime, addSegment, setCutStart, setCutEnd, onLabelSegment, splitCurrentSegment, createNumSegments, createFixedDurationSegments, createRandomSegments, apparentCutSegments, haveInvalidSegs, currentSegIndexSafe, currentCutSeg, currentApparentCutSeg, inverseCutSegments, clearSegments, loadCutSegments, isSegmentSelected, setCutTime, setCurrentSegIndex, onLabelSelectedSegments, deselectAllSegments, selectAllSegments, selectOnlyCurrentSegment, toggleCurrentSegmentSelected, removeSelectedSegments, setDeselectedSegmentIds, onSelectSegmentsByLabel, toggleSegmentSelected, selectOnlySegment, getApparentCutSegmentById, selectedSegments, selectedSegmentsOrInverse, nonFilteredSegmentsOrInverse, segmentsToExport,+ } = useSegments({ filePath, workingRef, setWorking, setCutProgress, mainVideoStream, duration, getRelevantTime, maxLabelLength, checkFileOpened, invertCutSegments, segmentsToChaptersOnly });const jumpSegStart = useCallback((index) => userSeekAbs(apparentCutSegments[index].start), [apparentCutSegments, userSeekAbs]);const jumpSegEnd = useCallback((index) => userSeekAbs(apparentCutSegments[index].end), [apparentCutSegments, userSeekAbs]);@@ -679,6 +675,13 @@ const App = memo(() => {if (!hideAllNotifications) toast.fire({ icon: 'info', text: i18n.t('Loaded existing preview file: {{ fileName }}', { fileName }) });}, [hideAllNotifications]);+ const areWeCutting = useMemo(() => segmentsToExport.some(({ start, end }) => isCuttingStart(start) || isCuttingEnd(end, duration)), [duration, segmentsToExport]);+ const needSmartCut = !!(areWeCutting && enableSmartCut);++ const {+ concatFiles, html5ifyDummy, cutMultiple, autoConcatCutSegments, html5ify, fixInvalidDuration,+ } = useFfmpegOperations({ filePath, enableTransferTimestamps, needSmartCut });+const html5ifyAndLoad = useCallback(async (cod, fp, speed, hv, ha) => {const usesDummyVideo = ['fastest-audio', 'fastest-audio-remux', 'fastest'].includes(speed);console.log('html5ifyAndLoad', { speed, hasVideo: hv, hasAudio: ha, usesDummyVideo });@@ -1024,15 +1027,6 @@ const App = memo(() => {}}, [isFileOpened, cleanupChoices, askForCleanupChoices, cleanupFiles, setWorking]);- const segmentsToExport = useMemo(() => {- if (!segmentsToChaptersOnly) return selectedSegmentsOrInverse;- // segmentsToChaptersOnly is a special mode where all segments will be simply written out as chapters to one file: https://github.com/mifi/lossless-cut/issues/993#issuecomment-1037927595- // Chapters export mode: Emulate a single segment with no cuts (full timeline)- return [{ start: 0, end: getSegApparentEnd({}) }];- }, [selectedSegmentsOrInverse, getSegApparentEnd, segmentsToChaptersOnly]);-- const areWeCutting = useMemo(() => segmentsToExport.some(({ start, end }) => isCuttingStart(start) || isCuttingEnd(end, duration)), [duration, segmentsToExport]);-const generateOutSegFileNames = useCallback(({ segments = segmentsToExport, template, forceSafeOutputFileName }) => (segments.map((segment, i) => {const { start, end, name = '' } = segment;@@ -1124,7 +1118,6 @@ const App = memo(() => {dispositionByStreamId,chapters: chaptersToAdd,detectedFps,- enableSmartCut,enableOverwriteOutput,});@@ -1207,7 +1200,7 @@ const App = memo(() => {setWorking();setCutProgress();}- }, [numStreamsToCopy, setWorking, segmentsToChaptersOnly, outSegTemplateOrDefault, generateOutSegFileNames, segmentsToExport, getOutSegError, cutMultiple, outputDir, customOutDir, fileFormat, duration, isRotationSet, effectiveRotation, copyFileStreams, allFilesMeta, keyframeCut, shortestFlag, ffmpegExperimental, preserveMovData, preserveMetadataOnMerge, movFastStart, avoidNegativeTs, customTagsByFile, customTagsByStreamId, dispositionByStreamId, detectedFps, enableSmartCut, enableOverwriteOutput, willMerge, exportConfirmEnabled, mainFileFormatData, mainStreams, exportExtraStreams, areWeCutting, hideAllNotifications, cleanupChoices, cleanupFiles, selectedSegmentsOrInverse, segmentsToChapters, invertCutSegments, autoConcatCutSegments, isCustomFormatSelected, autoDeleteMergedSegments, nonCopiedExtraStreams, filePath, handleExportFailed]);+ }, [numStreamsToCopy, setWorking, segmentsToChaptersOnly, outSegTemplateOrDefault, generateOutSegFileNames, segmentsToExport, getOutSegError, cutMultiple, outputDir, customOutDir, fileFormat, duration, isRotationSet, effectiveRotation, copyFileStreams, allFilesMeta, keyframeCut, shortestFlag, ffmpegExperimental, preserveMovData, preserveMetadataOnMerge, movFastStart, avoidNegativeTs, customTagsByFile, customTagsByStreamId, dispositionByStreamId, detectedFps, enableOverwriteOutput, willMerge, exportConfirmEnabled, mainFileFormatData, mainStreams, exportExtraStreams, areWeCutting, hideAllNotifications, cleanupChoices, cleanupFiles, selectedSegmentsOrInverse, segmentsToChapters, invertCutSegments, autoConcatCutSegments, isCustomFormatSelected, autoDeleteMergedSegments, nonCopiedExtraStreams, filePath, handleExportFailed]);const onExportPress = useCallback(async () => {if (!filePath || workingRef.current || segmentsToExport.length < 1) return;@@ -2394,7 +2387,7 @@ const App = memo(() => {)}</SideSheet>- <ExportConfirm filePath={filePath} areWeCutting={areWeCutting} nonFilteredSegmentsOrInverse={nonFilteredSegmentsOrInverse} selectedSegments={selectedSegmentsOrInverse} segmentsToExport={segmentsToExport} willMerge={willMerge} visible={exportConfirmVisible} onClosePress={closeExportConfirm} onExportConfirm={onExportConfirm} renderOutFmt={renderOutFmt} outputDir={outputDir} numStreamsTotal={numStreamsTotal} numStreamsToCopy={numStreamsToCopy} setStreamsSelectorShown={setStreamsSelectorShown} outFormat={fileFormat} setOutSegTemplate={setOutSegTemplate} outSegTemplate={outSegTemplateOrDefault} generateOutSegFileNames={generateOutSegFileNames} currentSegIndexSafe={currentSegIndexSafe} getOutSegError={getOutSegError} mainCopiedThumbnailStreams={mainCopiedThumbnailStreams} />+ <ExportConfirm filePath={filePath} areWeCutting={areWeCutting} nonFilteredSegmentsOrInverse={nonFilteredSegmentsOrInverse} selectedSegments={selectedSegmentsOrInverse} segmentsToExport={segmentsToExport} willMerge={willMerge} visible={exportConfirmVisible} onClosePress={closeExportConfirm} onExportConfirm={onExportConfirm} renderOutFmt={renderOutFmt} outputDir={outputDir} numStreamsTotal={numStreamsTotal} numStreamsToCopy={numStreamsToCopy} setStreamsSelectorShown={setStreamsSelectorShown} outFormat={fileFormat} setOutSegTemplate={setOutSegTemplate} outSegTemplate={outSegTemplateOrDefault} generateOutSegFileNames={generateOutSegFileNames} currentSegIndexSafe={currentSegIndexSafe} getOutSegError={getOutSegError} mainCopiedThumbnailStreams={mainCopiedThumbnailStreams} needSmartCut={needSmartCut} /><LastCommandsSheetvisible={lastCommandsVisible}diff --git a/src/ExportConfirm.jsx b/src/ExportConfirm.jsxindex c0d548b8..ef065182 100644--- a/src/ExportConfirm.jsx+++ b/src/ExportConfirm.jsx@@ -45,7 +45,7 @@ const ExportConfirm = memo(({areWeCutting, selectedSegments, segmentsToExport, willMerge, visible, onClosePress, onExportConfirm,outFormat, renderOutFmt, outputDir, numStreamsTotal, numStreamsToCopy, setStreamsSelectorShown, outSegTemplate,setOutSegTemplate, generateOutSegFileNames, filePath, currentSegIndexSafe, getOutSegError, nonFilteredSegmentsOrInverse,- mainCopiedThumbnailStreams,+ mainCopiedThumbnailStreams, needSmartCut,}) => {const { t } = useTranslation();@@ -186,9 +186,9 @@ const ExportConfirm = memo(({<li>{t('Smart cut (experimental):')} <Button height={20} onClick={() => setEnableSmartCut((v) => !v)}>{enableSmartCut ? t('Yes') : t('No')}</Button><HelpIcon onClick={onSmartCutHelpPress} />- {enableSmartCut && <WarningSignIcon verticalAlign="middle" color="warning" marginLeft=".3em" title={i18n.t('Experimental functionality has been activated!')} />}+ {needSmartCut && <WarningSignIcon verticalAlign="middle" color="warning" marginLeft=".3em" title={i18n.t('Experimental functionality has been activated!')} />}</li>- {!enableSmartCut && (+ {!needSmartCut && (<li>{t('Cut mode:')} <KeyframeCutButton /><HelpIcon onClick={onKeyframeCutHelpPress} /> {!keyframeCut && <span style={warningStyle}>{t('Note: Keyframe cut is recommended for most common files')}</span>}@@ -210,7 +210,7 @@ const ExportConfirm = memo(({</>)}- {!enableSmartCut && (+ {!needSmartCut && (<li>"avoid_negative_ts"<Select height={20} value={avoidNegativeTs} onChange={(e) => setAvoidNegativeTs(e.target.value)} style={{ marginLeft: 5 }}>diff --git a/src/hooks/useFfmpegOperations.js b/src/hooks/useFfmpegOperations.jsindex a90cd6fd..f9e05204 100644--- a/src/hooks/useFfmpegOperations.js+++ b/src/hooks/useFfmpegOperations.js@@ -55,7 +55,7 @@ const tryDeleteFiles = async (paths) => pMap(paths, (path) => {fs.unlink(path).catch((err) => console.error('Failed to delete', path, err));}, { concurrency: 5 });-function useFfmpegOperations({ filePath, enableTransferTimestamps }) {+function useFfmpegOperations({ filePath, enableTransferTimestamps, needSmartCut }) {const optionalTransferTimestamps = useCallback(async (...args) => {if (enableTransferTimestamps) await transferTimestamps(...args);}, [enableTransferTimestamps]);@@ -324,7 +324,7 @@ function useFfmpegOperations({ filePath, enableTransferTimestamps }) {outputDir, customOutDir, segments, segmentsFileNames, videoDuration, rotation, detectedFps,onProgress: onTotalProgress, keyframeCut, copyFileStreams, allFilesMeta, outFormat,appendFfmpegCommandLog, shortestFlag, ffmpegExperimental, preserveMovData, movFastStart, avoidNegativeTs,- customTagsByFile, customTagsByStreamId, dispositionByStreamId, chapters, preserveMetadataOnMerge, enableSmartCut,+ customTagsByFile, customTagsByStreamId, dispositionByStreamId, chapters, preserveMetadataOnMerge,enableOverwriteOutput,}) => {console.log('customTagsByFile', customTagsByFile);@@ -350,7 +350,7 @@ function useFfmpegOperations({ filePath, enableTransferTimestamps }) {async function maybeSmartCutSegment({ start: desiredCutFrom, end: cutTo }, i) {const getSegmentOutPath = () => join(outputDir, segmentsFileNames[i]);- if (!enableSmartCut) {+ if (!needSmartCut) {// old fashioned wayconst outPath = getSegmentOutPath();await checkOverwrite(outPath);@@ -365,9 +365,9 @@ function useFfmpegOperations({ filePath, enableTransferTimestamps }) {const streamsToCopyFromMainFile = copyFileStreams.find(({ path }) => path === filePath).streamIds.map((streamId) => streams.find((stream) => stream.index === streamId));- const { cutFrom: encodeCutTo, needsSmartCut, videoCodec, videoBitrate, videoStreamIndex, videoTimebase } = await getSmartCutParams({ path: filePath, videoDuration, desiredCutFrom, streams: streamsToCopyFromMainFile });+ const { cutFrom: encodeCutTo, segmentNeedsSmartCut, videoCodec, videoBitrate, videoStreamIndex, videoTimebase } = await getSmartCutParams({ path: filePath, videoDuration, desiredCutFrom, streams: streamsToCopyFromMainFile });- if (needsSmartCut && !detectedFps) throw new Error('Smart cut is not possible when FPS is unknown');+ if (segmentNeedsSmartCut && !detectedFps) throw new Error('Smart cut is not possible when FPS is unknown');console.log('Smart cut on video stream', videoStreamIndex);@@ -385,7 +385,7 @@ function useFfmpegOperations({ filePath, enableTransferTimestamps }) {// If we are cutting within two keyframes, just encode the whole part and return that// See https://github.com/mifi/lossless-cut/pull/1267#issuecomment-1236381740- if (needsSmartCut && encodeCutTo > cutTo) {+ if (segmentNeedsSmartCut && encodeCutTo > cutTo) {const outPath = getSegmentOutPath();await checkOverwrite(outPath);await cutEncodeSmartPartWrapper({ cutFrom: desiredCutFrom, cutTo, outPath });@@ -394,7 +394,7 @@ function useFfmpegOperations({ filePath, enableTransferTimestamps }) {const ext = getOutFileExtension({ isCustomFormatSelected: true, outFormat, filePath });- const smartCutMainPartOutPath = needsSmartCut+ const smartCutMainPartOutPath = segmentNeedsSmartCut? getSuffixedOutPath({ customOutDir, filePath, nameSuffix: `smartcut-segment-copy-${i}${ext}` }): getSegmentOutPath();@@ -402,7 +402,7 @@ function useFfmpegOperations({ filePath, enableTransferTimestamps }) {const smartCutSegmentsToConcat = [smartCutEncodedPartOutPath, smartCutMainPartOutPath];- if (!needsSmartCut) await checkOverwrite(smartCutMainPartOutPath);+ if (!segmentNeedsSmartCut) await checkOverwrite(smartCutMainPartOutPath);// for smart cut we need to use keyframe cut here, and no avoid_negative_tsawait cutSingle({@@ -410,7 +410,7 @@ function useFfmpegOperations({ filePath, enableTransferTimestamps }) {});// OK, just return the single cut file (we may need smart cut in other segments though)- if (!needsSmartCut) return smartCutMainPartOutPath;+ if (!segmentNeedsSmartCut) return smartCutMainPartOutPath;try {const frameDuration = 1 / detectedFps;@@ -438,7 +438,7 @@ function useFfmpegOperations({ filePath, enableTransferTimestamps }) {} finally {if (chaptersPath) await tryDeleteFiles([chaptersPath]);}- }, [concatFiles, cutSingle, filePath]);+ }, [concatFiles, cutSingle, filePath, needSmartCut]);const autoConcatCutSegments = useCallback(async ({ customOutDir, isCustomFormatSelected, outFormat, segmentPaths, ffmpegExperimental, onProgress, preserveMovData, movFastStart, autoDeleteMergedSegments, chapterNames, preserveMetadataOnMerge, appendFfmpegCommandLog }) => {const ext = getOutFileExtension({ isCustomFormatSelected, outFormat, filePath });diff --git a/src/hooks/useSegments.js b/src/hooks/useSegments.jsindex 1ef41112..19eeeb96 100644--- a/src/hooks/useSegments.js+++ b/src/hooks/useSegments.js@@ -18,7 +18,7 @@ import { maxSegmentsAllowed } from '../util/constants';export default ({filePath, workingRef, setWorking, setCutProgress, mainVideoStream,- duration, getRelevantTime, maxLabelLength, checkFileOpened, invertCutSegments,+ duration, getRelevantTime, maxLabelLength, checkFileOpened, invertCutSegments, segmentsToChaptersOnly,}) => {// Segment related stateconst segCounterRef = useRef(0);@@ -441,6 +441,14 @@ export default ({const selectedSegmentsOrInverse = useMemo(() => (invertCutSegments ? inverseCutSegments : selectedSegments), [inverseCutSegments, invertCutSegments, selectedSegments]);const nonFilteredSegmentsOrInverse = useMemo(() => (invertCutSegments ? inverseCutSegments : apparentCutSegments), [invertCutSegments, inverseCutSegments, apparentCutSegments]);+ const segmentsToExport = useMemo(() => {+ // segmentsToChaptersOnly is a special mode where all segments will be simply written out as chapters to one file: https://github.com/mifi/lossless-cut/issues/993#issuecomment-1037927595+ // Chapters export mode: Emulate a single segment with no cuts (full timeline)+ if (segmentsToChaptersOnly) return [{ start: 0, end: getSegApparentEnd({}) }];++ return selectedSegmentsOrInverse;+ }, [selectedSegmentsOrInverse, getSegApparentEnd, segmentsToChaptersOnly]);+const removeSelectedSegments = useCallback(() => removeSegments(selectedSegmentsRaw.map((seg) => seg.segId)), [removeSegments, selectedSegmentsRaw]);const selectOnlySegment = useCallback((seg) => setDeselectedSegmentIds(Object.fromEntries(cutSegments.filter((s) => s.segId !== seg.segId).map((s) => [s.segId, true]))), [cutSegments]);@@ -490,7 +498,8 @@ export default ({selectedSegments,selectedSegmentsOrInverse,nonFilteredSegmentsOrInverse,- getSegApparentEnd,+ segmentsToExport,+setCurrentSegIndex,setDeselectedSegmentIds,diff --git a/src/smartcut.js b/src/smartcut.jsindex 9d315891..748629f2 100644--- a/src/smartcut.js+++ b/src/smartcut.js@@ -24,7 +24,7 @@ export async function getSmartCutParams({ path, videoDuration, desiredCutFrom, sreturn {cutFrom: keyframeAtExactTime.time,videoStreamIndex: videoStream.index,- needsSmartCut: false,+ segmentNeedsSmartCut: false,};}@@ -59,7 +59,7 @@ export async function getSmartCutParams({ path, videoDuration, desiredCutFrom, sreturn {cutFrom: nextKeyframe.time,videoStreamIndex: videoStream.index,- needsSmartCut: true,+ segmentNeedsSmartCut: true,videoCodec,videoBitrate: Math.floor(videoBitrate),videoTimebase: timebase,