Skip to content

🎥 Make videos programmatically with React

document everything

Jonny Burgere507515b5e0e
diff --git a/packages/docs/docs/offthreadvideo.md b/packages/docs/docs/offthreadvideo.md
index 7b968111b9..e6a583dbd3 100644
--- a/packages/docs/docs/offthreadvideo.md
+++ b/packages/docs/docs/offthreadvideo.md
@@ -29,12 +29,19 @@ export const MyVideo = () => {
You can also pass a URL as a `src` to load a video remotely.
:::
-## API
+## Props
The props `volume`, `playbackRate` and `muted` are supported and work the same as in [`<Video>`](/docs/video).
The props `onError`, `className` and `style` are supported and get passed to the underlying HTML element. Remember that during render, this is a `<img>` element, and during preview, this is a `<video>` element.
+### `imageFormat`
+
+_Available since v3.0.22_
+
+Either `jpeg` or `png`. Default `jpeg`.
+With `png`, transparent videos can be displayed, however it is around 40% slower, with VP8 videos being [much slower](/docs/slow-method-to-extract-frame).
+
## Performance tips
Avoid embedding a video beyond it's end (for example: Rendering a 5 second video inside 10 second composition). To create parity with the `<Video>` element, the video still display it's last frame in that case. However, to fetch the last frame specifically is a significantly more expensive operation than a frame from a known timestamp.
diff --git a/packages/docs/docs/slow-method-to-extract-frame.md b/packages/docs/docs/slow-method-to-extract-frame.md
new file mode 100644
index 0000000000..099a9ae954
--- /dev/null
+++ b/packages/docs/docs/slow-method-to-extract-frame.md
@@ -0,0 +1,23 @@
+---
+id: slow-method-to-extract-frame
+title: "Slow method to extract frame"
+---
+
+When using the [`<OffthreadVideo>`](/docs/offthreadvideo) component, the following warning message may appear:
+
+```
+Using a slow method to extract the frame at 1000ms of [video].
+```
+
+While it is not an error itself, it warns that the render must do an expensive operation in order to render the video, in which the whole video must be read in order to extract a single frame in it. This warning currently appears in two occasions:
+
+- If a H.264 video in a MP4 container has it's timestamps corrupted so that it cannot be seeked to the exact frame that Remotion wants to extract.
+
+ - **Recommendation**: It might be faster to first re-encode the video using FFMPEG to fix the seeking: `ffmpeg -i inputvideo.mp4 outputvideo.mp4`
+
+- If it's a VP8 video in a WebM container and the selected image format is PNG, we cannot accurately extract the frame using FFMPEG and need to resort to the slow method.
+ - **Recommendation**: Prefer the VP9 codec instead, or switch the image format to JPEG (in which you will lose transparency however)
+
+## See also
+
+- [`<OffthreadVideo>`](/docs/offthreadvideo)
diff --git a/packages/docs/sidebars.js b/packages/docs/sidebars.js
index 2fd9781f2a..269129cf97 100644
--- a/packages/docs/sidebars.js
+++ b/packages/docs/sidebars.js
@@ -262,6 +262,7 @@ module.exports = {
"flickering",
"version-mismatch",
"enametoolong",
+ "slow-method-to-extract-frame",
],
},
{
diff --git a/packages/renderer/src/extract-frame-from-video.ts b/packages/renderer/src/extract-frame-from-video.ts
index 6317748060..f69069f57f 100644
--- a/packages/renderer/src/extract-frame-from-video.ts
+++ b/packages/renderer/src/extract-frame-from-video.ts
@@ -4,7 +4,10 @@ import {getAudioChannelsAndDuration} from './assets/get-audio-channels';
import {ensurePresentationTimestamps} from './ensure-presentation-timestamp';
import {frameToFfmpegTimestamp} from './frame-to-ffmpeg-timestamp';
import {isBeyondLastFrame, markAsBeyondLastFrame} from './is-beyond-last-frame';
-import {checkIfIsVp9Video} from './is-vp9-video';
+import {
+ getSpecialVCodecForTransparency,
+ SpecialVCodecForTransparency,
+} from './is-vp9-video';
import {
getLastFrameFromCache,
LastFrameOptions,
@@ -15,31 +18,41 @@ import {pLimit} from './p-limit';
const lastFrameLimit = pLimit(1);
const mainLimit = pLimit(5);
+export const determineVcodecFfmepgFlags = (
+ vcodecFlag: SpecialVCodecForTransparency
+) => {
+ return [
+ vcodecFlag === 'vp9' ? '-vcodec' : null,
+ vcodecFlag === 'vp9' ? 'libvpx-vp9' : null,
+ vcodecFlag === 'vp8' ? '-vcodec' : null,
+ vcodecFlag === 'vp8' ? 'libvpx' : null,
+ ].filter(Internals.truthy);
+};
+
// Uses no seeking, therefore the whole video has to be decoded. This is a last resort and should only happen
// if the video is corrupted
-const getLastFrameOfVideoSlow = async ({
+const getFrameOfVideoSlow = async ({
src,
- duration,
+ timestamp,
ffmpegExecutable,
imageFormat,
- isVp9Video,
+ specialVCodecForTransparency,
}: {
ffmpegExecutable: FfmpegExecutable;
src: string;
- duration: number;
+ timestamp: number;
imageFormat: OffthreadVideoImageFormat;
- isVp9Video: boolean;
+ specialVCodecForTransparency: SpecialVCodecForTransparency;
}) => {
console.warn(
- `\nUsing a slow method to determine the last frame of ${src}. The render can be sped up by re-encoding the video properly.`
+ `\nUsing a slow method to extract the frame at ${timestamp}ms of ${src}. See https://remotion.dev/docs/slow-method-to-extract-frame for advice`
);
- const actualOffset = `-${duration * 1000}ms`;
+ const actualOffset = `-${timestamp * 1000}ms`;
const command = [
'-itsoffset',
actualOffset,
- isVp9Video ? '-vcodec' : null,
- isVp9Video ? 'libvpx-vp9' : null,
+ ...determineVcodecFfmepgFlags(specialVCodecForTransparency),
'-i',
src,
'-frames:v',
@@ -108,13 +121,13 @@ const getLastFrameOfVideoFastUnlimited = async (
);
}
- if (offset > 40) {
- const last = await getLastFrameOfVideoSlow({
- duration,
+ if (options.specialVCodecForTransparency === 'vp8' || offset > 40) {
+ const last = await getFrameOfVideoSlow({
+ timestamp: duration,
ffmpegExecutable,
src,
imageFormat: options.imageFormat,
- isVp9Video: options.isVp9Video,
+ specialVCodecForTransparency: options.specialVCodecForTransparency,
});
return last;
}
@@ -125,8 +138,7 @@ const getLastFrameOfVideoFastUnlimited = async (
[
'-ss',
actualOffset,
- options.isVp9Video ? '-vcodec' : null,
- options.isVp9Video ? 'libvpx-vp9' : null,
+ ...determineVcodecFfmepgFlags(options.specialVCodecForTransparency),
'-i',
src,
'-frames:v',
@@ -180,7 +192,7 @@ const getLastFrameOfVideoFastUnlimited = async (
src,
ffprobeExecutable,
imageFormat: options.imageFormat,
- isVp9Video: options.isVp9Video,
+ specialVCodecForTransparency: options.specialVCodecForTransparency,
});
return unlimited;
@@ -217,7 +229,20 @@ const extractFrameFromVideoFn = async ({
imageFormat,
}: Options): Promise<Buffer> => {
await ensurePresentationTimestamps(src);
- const isVp9Video = await checkIfIsVp9Video(src, ffprobeExecutable);
+ const specialVCodecForTransparency: SpecialVCodecForTransparency =
+ imageFormat === 'jpeg'
+ ? 'none'
+ : await getSpecialVCodecForTransparency(src, ffprobeExecutable);
+
+ if (specialVCodecForTransparency === 'vp8') {
+ return getFrameOfVideoSlow({
+ ffmpegExecutable,
+ imageFormat,
+ specialVCodecForTransparency,
+ src,
+ timestamp: time,
+ });
+ }
if (isBeyondLastFrame(src, time)) {
const lastFrame = await getLastFrameOfVideo({
@@ -226,7 +251,7 @@ const extractFrameFromVideoFn = async ({
offset: 0,
src,
imageFormat,
- isVp9Video,
+ specialVCodecForTransparency,
});
return lastFrame;
}
@@ -237,8 +262,7 @@ const extractFrameFromVideoFn = async ({
[
'-ss',
ffmpegTimestamp,
- isVp9Video ? '-vcodec' : null,
- isVp9Video ? 'libvpx-vp9' : null,
+ ...determineVcodecFfmepgFlags(specialVCodecForTransparency),
'-i',
src,
'-frames:v',
@@ -292,7 +316,7 @@ const extractFrameFromVideoFn = async ({
offset: 0,
src,
imageFormat,
- isVp9Video,
+ specialVCodecForTransparency,
});
return last;
diff --git a/packages/renderer/src/is-vp9-video.ts b/packages/renderer/src/is-vp9-video.ts
index 7266152489..f0c81ca446 100644
--- a/packages/renderer/src/is-vp9-video.ts
+++ b/packages/renderer/src/is-vp9-video.ts
@@ -2,30 +2,35 @@ import execa from 'execa';
import {FfmpegExecutable} from 'remotion';
import {pLimit} from './p-limit';
-const isVp9VideoCache: Record<string, boolean> = {};
+export type SpecialVCodecForTransparency = 'vp9' | 'vp8' | 'none';
+
+const isVp9VideoCache: Record<string, SpecialVCodecForTransparency> = {};
const limit = pLimit(1);
-async function isVp9VideoUnlimited(
+async function getSpecialVCodecForTransparencyUnlimited(
src: string,
ffprobeExecutable: FfmpegExecutable
-): Promise<boolean> {
+): Promise<SpecialVCodecForTransparency> {
if (typeof isVp9VideoCache[src] !== 'undefined') {
return isVp9VideoCache[src];
}
const task = await execa(ffprobeExecutable ?? 'ffprobe', [src]);
- const result = task.stderr.includes('Video: vp9');
+ const isVp9 = task.stderr.includes('Video: vp9');
+ const isVp8 = task.stderr.includes('Video: vp8');
- isVp9VideoCache[src] = result;
+ isVp9VideoCache[src] = isVp9 ? 'vp9' : isVp8 ? 'vp8' : 'none';
- return result;
+ return isVp9VideoCache[src];
}
-export const checkIfIsVp9Video = (
+export const getSpecialVCodecForTransparency = (
src: string,
ffprobeExecutable: FfmpegExecutable
-): Promise<boolean> => {
- return limit(() => isVp9VideoUnlimited(src, ffprobeExecutable));
+): Promise<SpecialVCodecForTransparency> => {
+ return limit(() =>
+ getSpecialVCodecForTransparencyUnlimited(src, ffprobeExecutable)
+ );
};
diff --git a/packages/renderer/src/last-frame-from-video-cache.ts b/packages/renderer/src/last-frame-from-video-cache.ts
index 0efc84a67c..bf9d467794 100644
--- a/packages/renderer/src/last-frame-from-video-cache.ts
+++ b/packages/renderer/src/last-frame-from-video-cache.ts
@@ -1,13 +1,14 @@
// OffthreadVideo requires sometimes that the last frame of a video gets extracted, however, this can be slow. We allocate a cache for it but that can be garbage collected
import {FfmpegExecutable, OffthreadVideoImageFormat} from 'remotion';
+import {SpecialVCodecForTransparency} from './is-vp9-video';
export type LastFrameOptions = {
ffmpegExecutable: FfmpegExecutable;
ffprobeExecutable: FfmpegExecutable;
offset: number;
src: string;
- isVp9Video: boolean;
+ specialVCodecForTransparency: SpecialVCodecForTransparency;
imageFormat: OffthreadVideoImageFormat;
};
diff --git a/packages/renderer/src/test/get-last-frame-of-corrupted-video.test.ts b/packages/renderer/src/test/get-last-frame-of-corrupted-video.test.ts
index 04e50553f5..cc36811639 100644
--- a/packages/renderer/src/test/get-last-frame-of-corrupted-video.test.ts
+++ b/packages/renderer/src/test/get-last-frame-of-corrupted-video.test.ts
@@ -16,6 +16,6 @@ test('Get last frame of corrupted video', async () => {
'corrupted.mp4'
),
imageFormat: 'png',
- isVp9Video: false,
+ specialVCodecForTransparency: 'none',
});
}, 90000);
diff --git a/packages/renderer/src/test/last-frame-video-cache.test.ts b/packages/renderer/src/test/last-frame-video-cache.test.ts
index 72500018c7..727e52db5d 100644
--- a/packages/renderer/src/test/last-frame-video-cache.test.ts
+++ b/packages/renderer/src/test/last-frame-video-cache.test.ts
@@ -13,7 +13,7 @@ const makeKey = (id: string): LastFrameOptions => {
offset: 10,
src: id,
imageFormat: 'jpeg',
- isVp9Video: false,
+ specialVCodecForTransparency: 'none',
};
};