Skip to content

🎥 Make videos programmatically with React

decrement last frame by 1 frame always

Jonny Burgerf9c5412ff018
diff --git a/packages/renderer/src/assets/get-video-stream-duration.ts b/packages/renderer/src/assets/get-video-stream-duration.ts
index d93cffca42..6f24811b28 100644
--- a/packages/renderer/src/assets/get-video-stream-duration.ts
+++ b/packages/renderer/src/assets/get-video-stream-duration.ts
@@ -17,7 +17,7 @@ async function getVideoStreamDurationUnlimited(
const args = [
['-v', 'error'],
['-select_streams', 'v:0'],
- ['-show_entries', 'stream=duration'],
+ ['-show_entries', 'stream=duration,r_frame_rate'],
[src],
]
.reduce<(string | null)[]>((acc, val) => acc.concat(val), [])
@@ -26,9 +26,11 @@ async function getVideoStreamDurationUnlimited(
const task = await execa(ffprobeExecutable ?? 'ffprobe', args);
const duration = task.stdout.match(/duration=([0-9.]+)/);
+ const fps = task.stdout.match(/r_frame_rate=([0-9.]+)\/([0-9.]+)/);
const result: VideoDurationResult = {
duration: duration ? parseFloat(duration[1]) : null,
+ fps: fps ? parseInt(fps[1], 10) / parseInt(fps[2], 10) : null,
};
downloadMap.videoDurationResultCache[src] = result;
diff --git a/packages/renderer/src/extract-frame-from-video.ts b/packages/renderer/src/extract-frame-from-video.ts
index 8a114f5f92..f0c2f84bdd 100644
--- a/packages/renderer/src/extract-frame-from-video.ts
+++ b/packages/renderer/src/extract-frame-from-video.ts
@@ -132,11 +132,12 @@ const getLastFrameOfVideoFastUnlimited = async (
return fromCache;
}
- const {duration} = await getVideoStreamDuration(
+ const {duration, fps} = await getVideoStreamDuration(
downloadMap,
src,
ffprobeExecutable
);
+
if (duration === null) {
throw new Error(
`Could not determine the duration of ${src} using FFMPEG. The file is not supported.`
@@ -155,7 +156,7 @@ const getLastFrameOfVideoFastUnlimited = async (
return last;
}
- const actualOffset = `${duration * 1000 - offset - 10}ms`;
+ const actualOffset = `${duration * 1000 - offset}ms`;
const {stdout, stderr} = execa(
ffmpegExecutable ?? 'ffmpeg',
[
@@ -212,7 +213,8 @@ const getLastFrameOfVideoFastUnlimited = async (
if (isEmpty) {
const unlimited = await getLastFrameOfVideoFastUnlimited({
ffmpegExecutable,
- offset: offset + 10,
+ // Decrement in 10ms increments, or 1 frame (e.g. fps = 25 --> 40ms)
+ offset: offset + 1000 / (fps === null ? 10 : fps),
src,
ffprobeExecutable,
imageFormat: options.imageFormat,