mirror of https://github.com/mifi/lossless-cut
refactor
parent
21a2c767ac
commit
523bdb7daa
@ -0,0 +1,43 @@
|
|||||||
|
import { useState, useCallback, useRef, useEffect } from 'react';
|
||||||
|
import useDebounceOld from 'react-use/lib/useDebounce'; // Want to phase out this
|
||||||
|
|
||||||
|
import { readFrames, findNearestKeyFrameTime as ffmpegFindNearestKeyFrameTime } from '../ffmpeg';
|
||||||
|
|
||||||
|
|
||||||
|
export default ({ keyframesEnabled, filePath, commandedTime, mainVideoStream, detectedFps, ffmpegExtractWindow }) => {
|
||||||
|
const readingKeyframesPromise = useRef();
|
||||||
|
const [neighbouringFrames, setNeighbouringFrames] = useState([]);
|
||||||
|
|
||||||
|
const findNearestKeyFrameTime = useCallback(({ time, direction }) => ffmpegFindNearestKeyFrameTime({ frames: neighbouringFrames, time, direction, fps: detectedFps }), [neighbouringFrames, detectedFps]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
setNeighbouringFrames([]);
|
||||||
|
}, [filePath]);
|
||||||
|
|
||||||
|
useDebounceOld(() => {
|
||||||
|
// We still want to calculate keyframes even if not shouldShowKeyframes because maybe we want to be able to step to the closest keyframe
|
||||||
|
const shouldRun = () => keyframesEnabled && filePath && mainVideoStream && commandedTime != null;
|
||||||
|
|
||||||
|
async function run() {
|
||||||
|
if (!shouldRun() || readingKeyframesPromise.current) return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const promise = readFrames({ filePath, aroundTime: commandedTime, stream: mainVideoStream.index, window: ffmpegExtractWindow });
|
||||||
|
readingKeyframesPromise.current = promise;
|
||||||
|
const newFrames = await promise;
|
||||||
|
if (!shouldRun()) return;
|
||||||
|
// console.log(newFrames);
|
||||||
|
setNeighbouringFrames(newFrames);
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Failed to read keyframes', err);
|
||||||
|
} finally {
|
||||||
|
readingKeyframesPromise.current = undefined;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
run();
|
||||||
|
}, 500, [keyframesEnabled, filePath, commandedTime, mainVideoStream]);
|
||||||
|
|
||||||
|
return {
|
||||||
|
neighbouringFrames, findNearestKeyFrameTime,
|
||||||
|
};
|
||||||
|
};
|
||||||
@ -0,0 +1,40 @@
|
|||||||
|
import { useState, useRef, useEffect } from 'react';
|
||||||
|
import useDebounceOld from 'react-use/lib/useDebounce'; // Want to phase out this
|
||||||
|
import { waveformColor } from '../colors';
|
||||||
|
|
||||||
|
import { renderWaveformPng } from '../ffmpeg';
|
||||||
|
|
||||||
|
export default ({ filePath, commandedTime, zoomedDuration, waveformEnabled, mainAudioStream, shouldShowWaveform, ffmpegExtractWindow }) => {
|
||||||
|
const creatingWaveformPromise = useRef();
|
||||||
|
const [waveform, setWaveform] = useState();
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
setWaveform();
|
||||||
|
}, [filePath]);
|
||||||
|
|
||||||
|
useDebounceOld(() => {
|
||||||
|
const shouldRun = () => filePath && mainAudioStream && commandedTime != null && shouldShowWaveform && waveformEnabled;
|
||||||
|
|
||||||
|
async function run() {
|
||||||
|
if (!shouldRun() || creatingWaveformPromise.current) return;
|
||||||
|
try {
|
||||||
|
const promise = renderWaveformPng({ filePath, aroundTime: commandedTime, window: ffmpegExtractWindow, color: waveformColor });
|
||||||
|
creatingWaveformPromise.current = promise;
|
||||||
|
if (!shouldRun()) return;
|
||||||
|
const wf = await promise;
|
||||||
|
setWaveform(wf);
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Failed to render waveform', err);
|
||||||
|
} finally {
|
||||||
|
creatingWaveformPromise.current = undefined;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
run();
|
||||||
|
}, 500, [filePath, commandedTime, zoomedDuration, waveformEnabled, mainAudioStream, shouldShowWaveform]);
|
||||||
|
|
||||||
|
// Cleanup old
|
||||||
|
useEffect(() => () => waveform && URL.revokeObjectURL(waveform.url), [waveform]);
|
||||||
|
|
||||||
|
return { waveform };
|
||||||
|
};
|
||||||
Loading…
Reference in New Issue