🎥 Make videos programmatically with React
finish implementation
Jonny Burgerf0bd69674999
diff --git a/packages/renderer/src/assets/download-map.ts b/packages/renderer/src/assets/download-map.tsindex 9649423478..02658ff1fc 100644--- a/packages/renderer/src/assets/download-map.ts+++ b/packages/renderer/src/assets/download-map.ts@@ -19,7 +19,7 @@ export type SpecialVCodecForTransparency = 'vp9' | 'vp8' | 'none';export type NeedsResize = [number, number] | null;export type Vp9Result = {- specialVcodec: SpecialVCodecForTransparency;+ specialVcodecForTransparency: SpecialVCodecForTransparency;needsResize: NeedsResize;};export type VideoDurationResult = {diff --git a/packages/renderer/src/assets/get-video-stream-duration.ts b/packages/renderer/src/assets/get-video-stream-duration.tsindex c602ef25cd..b09677106d 100644--- a/packages/renderer/src/assets/get-video-stream-duration.ts+++ b/packages/renderer/src/assets/get-video-stream-duration.ts@@ -49,16 +49,15 @@ export const parseVideoStreamDuration = (stdout: string) => {return result;};-async function getVideoStreamDurationUnlimited(- downloadMap: DownloadMap,- src: string,- ffprobeExecutable: FfmpegExecutable,- remotionRoot: string-): Promise<VideoDurationResult> {- if (downloadMap.videoDurationResultCache[src]) {- return downloadMap.videoDurationResultCache[src];- }-+export async function getVideoStreamDurationwithoutCache({+ src,+ ffprobeExecutable,+ remotionRoot,+}: {+ src: string;+ ffprobeExecutable: FfmpegExecutable;+ remotionRoot: string;+}) {const args = [['-v', 'error'],['-select_streams', 'v:0'],@@ -73,7 +72,30 @@ async function getVideoStreamDurationUnlimited(args);- return parseVideoStreamDuration(task.stdout);+ const result: VideoDurationResult = parseVideoStreamDuration(task.stdout);++ return result;+}++async function getVideoStreamDurationUnlimited(+ downloadMap: DownloadMap,+ src: string,+ ffprobeExecutable: FfmpegExecutable,+ remotionRoot: string+): Promise<VideoDurationResult> {+ if (downloadMap.videoDurationResultCache[src]) {+ return downloadMap.videoDurationResultCache[src];+ }++ const result: VideoDurationResult = await getVideoStreamDurationwithoutCache({+ src,+ ffprobeExecutable,+ remotionRoot,+ });++ downloadMap.videoDurationResultCache[src] = result;++ return result;}export const getVideoStreamDuration = (diff --git a/packages/renderer/src/extract-frame-from-video.ts b/packages/renderer/src/extract-frame-from-video.tsindex 5bf5bd82d8..f8d734e580 100644--- a/packages/renderer/src/extract-frame-from-video.ts+++ b/packages/renderer/src/extract-frame-from-video.ts@@ -5,12 +5,16 @@ import type {NeedsResize,SpecialVCodecForTransparency,} from './assets/download-map';-import {getVideoStreamDuration} from './assets/get-video-stream-duration';+import {+ getVideoStreamDuration,+ getVideoStreamDurationwithoutCache,+} from './assets/get-video-stream-duration';import {ensurePresentationTimestamps} from './ensure-presentation-timestamp';import type {FfmpegExecutable} from './ffmpeg-executable';import {getExecutableBinary} from './ffmpeg-flags';+import {findRemotionRoot} from './find-closest-package-json';import {frameToFfmpegTimestamp} from './frame-to-ffmpeg-timestamp';-import {getVideoInfo} from './get-video-info';+import {getVideoInfo, getVideoInfoUncached} from './get-video-info';import {isBeyondLastFrame, markAsBeyondLastFrame} from './is-beyond-last-frame';import type {LastFrameOptions} from './last-frame-from-video-cache';import {@@ -24,6 +28,8 @@ import {truthy} from './truthy';const lastFrameLimit = pLimit(1);const mainLimit = pLimit(5);+const ACCEPTABLE_OFFSET_THRESHOLD = 40;+const determineVcodecFfmepgFlags = (vcodecFlag: SpecialVCodecForTransparency) => {@@ -171,7 +177,10 @@ const getLastFrameOfVideoFastUnlimited = async ();}- if (options.specialVCodecForTransparency === 'vp8' || offset > 40) {+ if (+ options.specialVCodecForTransparency === 'vp8' ||+ offset > ACCEPTABLE_OFFSET_THRESHOLD+ ) {const last = await getFrameOfVideoSlow({duration,ffmpegExecutable,@@ -240,6 +249,71 @@ type Options = {remotionRoot: string;};+export const canExtractFramesFast = async ({+ src,+ ffmpegExecutable,+ ffprobeExecutable,+}: {+ src: string;+ ffmpegExecutable: FfmpegExecutable;+ ffprobeExecutable: FfmpegExecutable;+}): Promise<{+ canExtractFramesFast: boolean;+ shouldReencode: boolean;+}> => {+ const remotionRoot = findRemotionRoot();+ const {specialVcodecForTransparency: specialVcodec} =+ await getVideoInfoUncached({+ src,+ ffprobeExecutable,+ remotionRoot,+ });++ if (specialVcodec === 'vp8') {+ return {+ canExtractFramesFast: false,+ shouldReencode: false,+ };+ }++ const {duration} = await getVideoStreamDurationwithoutCache({+ ffprobeExecutable,+ remotionRoot,+ src,+ });+ if (duration === null) {+ throw new Error(+ `Could not determine the duration of ${src} using FFMPEG. The file is not supported.`+ );+ }++ const actualOffset = `${duration * 1000 - ACCEPTABLE_OFFSET_THRESHOLD}ms`;++ const [stdErr] = await tryToExtractFrameOfVideoFast({+ actualOffset,+ ffmpegExecutable,+ imageFormat: 'jpeg',+ // Intentionally leaving needsResize as null, because we don't need to resize+ needsResize: null,+ remotionRoot,+ specialVCodecForTransparency: specialVcodec,+ src,+ });+ const isEmpty = stdErr.includes('Output file is empty');++ if (isEmpty) {+ return {+ canExtractFramesFast: false,+ shouldReencode: true,+ };+ }++ return {+ canExtractFramesFast: true,+ shouldReencode: false,+ };+};+const tryToExtractFrameOfVideoFast = async ({ffmpegExecutable,remotionRoot,@@ -328,12 +402,8 @@ const extractFrameFromVideoFn = async ({ffmpegExecutable,ffprobeExecutable,});- const {specialVcodec, needsResize} = await getVideoInfo(- downloadMap,- src,- ffprobeExecutable,- remotionRoot- );+ const {specialVcodecForTransparency: specialVcodec, needsResize} =+ await getVideoInfo(downloadMap, src, ffprobeExecutable, remotionRoot);if (specialVcodec === 'vp8') {const {fps} = await getVideoStreamDuration(diff --git a/packages/renderer/src/get-video-info.ts b/packages/renderer/src/get-video-info.tsindex 8bcf1b24b4..6809854ba6 100644--- a/packages/renderer/src/get-video-info.ts+++ b/packages/renderer/src/get-video-info.ts@@ -7,16 +7,15 @@ import {pLimit} from './p-limit';const limit = pLimit(1);-async function getVideoInfoUnlimited(- downloadMap: DownloadMap,- src: string,- ffprobeExecutable: FfmpegExecutable,- remotionRoot: string-): Promise<Vp9Result> {- if (typeof downloadMap.isVp9VideoCache[src] !== 'undefined') {- return downloadMap.isVp9VideoCache[src];- }-+export async function getVideoInfoUncached({+ src,+ ffprobeExecutable,+ remotionRoot,+}: {+ src: string;+ ffprobeExecutable: FfmpegExecutable;+ remotionRoot: string;+}): Promise<Vp9Result> {const task = await execa(await getExecutableBinary(ffprobeExecutable, remotionRoot, 'ffprobe'),[src]@@ -53,10 +52,29 @@ async function getVideoInfoUnlimited(}const result: Vp9Result = {- specialVcodec: isVp9 ? 'vp9' : isVp8 ? 'vp8' : 'none',+ specialVcodecForTransparency: isVp9 ? 'vp9' : isVp8 ? 'vp8' : 'none',needsResize,};+ return result;+}++async function getVideoInfoUnlimited(+ downloadMap: DownloadMap,+ src: string,+ ffprobeExecutable: FfmpegExecutable,+ remotionRoot: string+): Promise<Vp9Result> {+ if (typeof downloadMap.isVp9VideoCache[src] !== 'undefined') {+ return downloadMap.isVp9VideoCache[src];+ }++ const result = await getVideoInfoUncached({+ ffprobeExecutable,+ remotionRoot,+ src,+ });+downloadMap.isVp9VideoCache[src] = result;return downloadMap.isVp9VideoCache[src];diff --git a/packages/renderer/src/index.ts b/packages/renderer/src/index.tsindex 41f9d871a8..9cdb53e80b 100644--- a/packages/renderer/src/index.ts+++ b/packages/renderer/src/index.ts@@ -11,6 +11,7 @@ import {deleteDirectory} from './delete-directory';import {ensureOutputDirectory} from './ensure-output-directory';import {symbolicateError} from './error-handling/symbolicate-error';import {SymbolicateableError} from './error-handling/symbolicateable-error';+import {canExtractFramesFast} from './extract-frame-from-video';import {ffmpegHasFeature,getExecutableBinary,@@ -152,6 +153,7 @@ export const RenderInternals = {getExecutableBinary,validateBitrate,getFfmpegVersion,+ canExtractFramesFast,};// Warn of potential performance issues with Apple Silicon (M1 chip under Rosetta)