Skip to content

The swiss army knife of lossless video/audio editing

improve black detect

Mikael Finstad0803858575a4
diff --git a/src/App.jsx b/src/App.jsx
index 43e915a3..565cdf1a 100644
--- a/src/App.jsx
+++ b/src/App.jsx
@@ -1757,9 +1757,9 @@ const App = memo(() => {
setWorking(i18n.t('Detecting black scenes'));
setCutProgress(0);
- const blackSegments = await blackDetect({ filePath, duration, onProgress: setCutProgress });
+ const blackSegments = await blackDetect({ filePath, duration, onProgress: setCutProgress, from: currentApparentCutSeg.start, to: currentApparentCutSeg.end });
console.log('blackSegments', blackSegments);
- loadCutSegments(blackSegments.map(({ blackStart, blackEnd }) => ({ start: blackStart, end: blackEnd })));
+ loadCutSegments(blackSegments.map(({ blackStart, blackEnd }) => ({ start: blackStart, end: blackEnd })), true);
} catch (err) {
errorToast(i18n.t('Failed to detect black scenes'));
console.error('Failed to detect black scenes', err);
@@ -1767,7 +1767,7 @@ const App = memo(() => {
setWorking();
setCutProgress();
}
- }, [duration, filePath, setWorking, loadCutSegments]);
+ }, [filePath, setWorking, duration, currentApparentCutSeg, loadCutSegments]);
const userHtml5ifyCurrentFile = useCallback(async () => {
if (!filePath) return;
diff --git a/src/ffmpeg.js b/src/ffmpeg.js
index 79f72417..ff45edae 100644
--- a/src/ffmpeg.js
+++ b/src/ffmpeg.js
@@ -532,8 +532,14 @@ export async function renderWaveformPng({ filePath, aroundTime, window, color })
}
}
-export async function blackDetect({ filePath, duration, minInterval = 0.05, onProgress }) {
- const args = ['-hide_banner', '-i', filePath, '-vf', `blackdetect=d=${minInterval}`, '-an', '-f', 'null', '-'];
+export async function blackDetect({ filePath, duration, minInterval = 0.05, onProgress, from, to }) {
+ const args = [
+ '-hide_banner',
+ ...(from != null ? ['-ss', from.toFixed(5)] : []),
+ '-i', filePath,
+ ...(to != null ? ['-t', (to - from).toFixed(5)] : []),
+ '-vf', `blackdetect=d=${minInterval}`, '-an', '-f', 'null', '-',
+ ];
const process = execa(getFfmpegPath(), args, { encoding: null, buffer: false });
const blackSegments = [];
@@ -549,7 +555,8 @@ export async function blackDetect({ filePath, duration, minInterval = 0.05, onPr
handleProgress(process, duration, onProgress, customMatcher);
await process;
- return blackSegments;
+ const offset = from != null ? from : 0;
+ return blackSegments.map(({ blackStart, blackEnd }) => ({ blackStart: blackStart + offset, blackEnd: blackEnd + offset }));
}
export async function extractWaveform({ filePath, outPath }) {