Skip to content

🎥 Make videos programmatically with React

extract try to extract fast

Jonny Burgerfc57e037ef4e
diff --git a/packages/renderer/src/assets/download-map.ts b/packages/renderer/src/assets/download-map.ts
index c28cdc0a47..9649423478 100644
--- a/packages/renderer/src/assets/download-map.ts
+++ b/packages/renderer/src/assets/download-map.ts
@@ -16,9 +16,11 @@ type EncodingStatus =
export type SpecialVCodecForTransparency = 'vp9' | 'vp8' | 'none';
+export type NeedsResize = [number, number] | null;
+
export type Vp9Result = {
specialVcodec: SpecialVCodecForTransparency;
- needsResize: [number, number] | null;
+ needsResize: NeedsResize;
};
export type VideoDurationResult = {
duration: number | null;
diff --git a/packages/renderer/src/extract-frame-from-video.ts b/packages/renderer/src/extract-frame-from-video.ts
index cb267a88c8..5bf5bd82d8 100644
--- a/packages/renderer/src/extract-frame-from-video.ts
+++ b/packages/renderer/src/extract-frame-from-video.ts
@@ -2,6 +2,7 @@ import execa from 'execa';
import type {OffthreadVideoImageFormat} from 'remotion';
import type {
DownloadMap,
+ NeedsResize,
SpecialVCodecForTransparency,
} from './assets/download-map';
import {getVideoStreamDuration} from './assets/get-video-stream-duration';
@@ -186,25 +187,94 @@ const getLastFrameOfVideoFastUnlimited = async (
}
const actualOffset = `${duration * 1000 - offset}ms`;
+ const [stdErr, stdoutBuffer] = await tryToExtractFrameOfVideoFast({
+ actualOffset,
+ ffmpegExecutable,
+ imageFormat: options.imageFormat,
+ needsResize: options.needsResize,
+ remotionRoot: options.remotionRoot,
+ specialVCodecForTransparency: options.specialVCodecForTransparency,
+ src,
+ });
+
+ const isEmpty = stdErr.includes('Output file is empty');
+ if (isEmpty) {
+ const unlimited = await getLastFrameOfVideoFastUnlimited({
+ ffmpegExecutable,
+ // Decrement in 10ms increments, or 1 frame (e.g. fps = 25 --> 40ms)
+ offset: offset + (fps === null ? 10 : 1000 / fps),
+ src,
+ ffprobeExecutable,
+ imageFormat: options.imageFormat,
+ specialVCodecForTransparency: options.specialVCodecForTransparency,
+ needsResize: options.needsResize,
+ downloadMap: options.downloadMap,
+ remotionRoot: options.remotionRoot,
+ });
+
+ return unlimited;
+ }
+
+ return stdoutBuffer;
+};
+
+export const getLastFrameOfVideo = async (
+ options: LastFrameOptions
+): Promise<Buffer> => {
+ const result = await lastFrameLimit(
+ getLastFrameOfVideoFastUnlimited,
+ options
+ );
+ setLastFrameInCache(options, result);
+
+ return result;
+};
+
+type Options = {
+ time: number;
+ src: string;
+ ffmpegExecutable: FfmpegExecutable;
+ ffprobeExecutable: FfmpegExecutable;
+ imageFormat: OffthreadVideoImageFormat;
+ downloadMap: DownloadMap;
+ remotionRoot: string;
+};
+
+const tryToExtractFrameOfVideoFast = async ({
+ ffmpegExecutable,
+ remotionRoot,
+ specialVCodecForTransparency,
+ imageFormat,
+ needsResize,
+ src,
+ actualOffset,
+}: {
+ ffmpegExecutable: FfmpegExecutable;
+ remotionRoot: string;
+ imageFormat: OffthreadVideoImageFormat;
+ needsResize: NeedsResize;
+ src: string;
+ specialVCodecForTransparency: SpecialVCodecForTransparency;
+ actualOffset: string;
+}) => {
const {stdout, stderr} = execa(
- await getExecutableBinary(ffmpegExecutable, options.remotionRoot, 'ffmpeg'),
+ await getExecutableBinary(ffmpegExecutable, remotionRoot, 'ffmpeg'),
[
'-ss',
actualOffset,
- ...determineVcodecFfmepgFlags(options.specialVCodecForTransparency),
+ ...determineVcodecFfmepgFlags(specialVCodecForTransparency),
'-i',
src,
'-frames:v',
'1',
'-c:v',
- options.imageFormat === 'jpeg' ? 'mjpeg' : 'png',
+ imageFormat === 'jpeg' ? 'mjpeg' : 'png',
'-f',
'image2pipe',
- ...determineResizeParams(options.needsResize),
+ ...determineResizeParams(needsResize),
'-',
].filter(truthy)
);
-
if (!stderr) {
throw new Error('unexpectedly did not get stderr');
}
@@ -237,48 +307,7 @@ const getLastFrameOfVideoFastUnlimited = async (
});
const [stdErr, stdoutBuffer] = await Promise.all([stdErrString, stdoutChunk]);
-
- const isEmpty = stdErr.includes('Output file is empty');
- if (isEmpty) {
- const unlimited = await getLastFrameOfVideoFastUnlimited({
- ffmpegExecutable,
- // Decrement in 10ms increments, or 1 frame (e.g. fps = 25 --> 40ms)
- offset: offset + (fps === null ? 10 : 1000 / fps),
- src,
- ffprobeExecutable,
- imageFormat: options.imageFormat,
- specialVCodecForTransparency: options.specialVCodecForTransparency,
- needsResize: options.needsResize,
- downloadMap: options.downloadMap,
- remotionRoot: options.remotionRoot,
- });
-
- return unlimited;
- }
-
- return stdoutBuffer;
-};
-
-export const getLastFrameOfVideo = async (
- options: LastFrameOptions
-): Promise<Buffer> => {
- const result = await lastFrameLimit(
- getLastFrameOfVideoFastUnlimited,
- options
- );
- setLastFrameInCache(options, result);
-
- return result;
-};
-
-type Options = {
- time: number;
- src: string;
- ffmpegExecutable: FfmpegExecutable;
- ffprobeExecutable: FfmpegExecutable;
- imageFormat: OffthreadVideoImageFormat;
- downloadMap: DownloadMap;
- remotionRoot: string;
+ return [stdErr, stdoutBuffer] as const;
};
const extractFrameFromVideoFn = async ({