Skip to content

🎥 Make videos programmatically with React

more scoping

Jonny Burger8be6818f74a0
diff --git a/packages/renderer/src/assets/download-map.ts b/packages/renderer/src/assets/download-map.ts
index d644168582..3f21754bd6 100644
--- a/packages/renderer/src/assets/download-map.ts
+++ b/packages/renderer/src/assets/download-map.ts
@@ -16,6 +16,14 @@ export type Vp9Result = {
specialVcodec: SpecialVCodecForTransparency;
needsResize: [number, number] | null;
};
+export type VideoDurationResult = {
+ duration: number | null;
+};
+
+export type AudioChannelsAndDurationResultCache = {
+ channels: number;
+ duration: number | null;
+};
export type DownloadMap = {
isDownloadingMap: {
@@ -36,8 +44,9 @@ export type DownloadMap = {
lastFrameMap: Record<string, {lastAccessed: number; data: Buffer}>;
isBeyondLastFrameMap: Record<string, number>;
isVp9VideoCache: Record<string, Vp9Result>;
-
ensureFileHasPresentationTimestamp: Record<string, EncodingStatus>;
+ videoDurationResultCache: Record<string, VideoDurationResult>;
+ durationOfAssetCache: Record<string, AudioChannelsAndDurationResultCache>;
};
export type RenderAssetInfo = {
@@ -57,5 +66,7 @@ export const makeDownloadMap = (): DownloadMap => {
isBeyondLastFrameMap: {},
ensureFileHasPresentationTimestamp: {},
isVp9VideoCache: {},
+ videoDurationResultCache: {},
+ durationOfAssetCache: {},
};
};
diff --git a/packages/renderer/src/assets/get-audio-channels.ts b/packages/renderer/src/assets/get-audio-channels.ts
index 25921a20cb..34fddbe1a0 100644
--- a/packages/renderer/src/assets/get-audio-channels.ts
+++ b/packages/renderer/src/assets/get-audio-channels.ts
@@ -1,22 +1,20 @@
import execa from 'execa';
import type {FfmpegExecutable} from '../ffmpeg-executable';
import {pLimit} from '../p-limit';
-
-type Result = {
- channels: number;
- duration: number | null;
-};
-
-const durationOfAssetCache: Record<string, Result> = {};
+import type {
+ AudioChannelsAndDurationResultCache,
+ DownloadMap,
+} from './download-map';
const limit = pLimit(1);
async function getAudioChannelsAndDurationUnlimited(
+ downloadMap: DownloadMap,
src: string,
ffprobeExecutable: FfmpegExecutable
-): Promise<Result> {
- if (durationOfAssetCache[src]) {
- return durationOfAssetCache[src];
+): Promise<AudioChannelsAndDurationResultCache> {
+ if (downloadMap.durationOfAssetCache[src]) {
+ return downloadMap.durationOfAssetCache[src];
}
const args = [
@@ -33,21 +31,22 @@ async function getAudioChannelsAndDurationUnlimited(
const channels = task.stdout.match(/channels=([0-9]+)/);
const duration = task.stdout.match(/duration=([0-9.]+)/);
- const result: Result = {
+ const result: AudioChannelsAndDurationResultCache = {
channels: channels ? parseInt(channels[1], 10) : 0,
duration: duration ? parseFloat(duration[1]) : null,
};
- durationOfAssetCache[src] = result;
+ downloadMap.durationOfAssetCache[src] = result;
return result;
}
export const getAudioChannelsAndDuration = (
+ downloadMap: DownloadMap,
src: string,
ffprobeExecutable: FfmpegExecutable
-): Promise<Result> => {
+): Promise<AudioChannelsAndDurationResultCache> => {
return limit(() =>
- getAudioChannelsAndDurationUnlimited(src, ffprobeExecutable)
+ getAudioChannelsAndDurationUnlimited(downloadMap, src, ffprobeExecutable)
);
};
diff --git a/packages/renderer/src/assets/get-video-stream-duration.ts b/packages/renderer/src/assets/get-video-stream-duration.ts
index 24204882c2..d93cffca42 100644
--- a/packages/renderer/src/assets/get-video-stream-duration.ts
+++ b/packages/renderer/src/assets/get-video-stream-duration.ts
@@ -1,21 +1,17 @@
import execa from 'execa';
import type {FfmpegExecutable} from '../ffmpeg-executable';
import {pLimit} from '../p-limit';
-
-type Result = {
- duration: number | null;
-};
-
-const durationOfAssetCache: Record<string, Result> = {};
+import type {DownloadMap, VideoDurationResult} from './download-map';
const limit = pLimit(1);
async function getVideoStreamDurationUnlimited(
+ downloadMap: DownloadMap,
src: string,
ffprobeExecutable: FfmpegExecutable
-): Promise<Result> {
- if (durationOfAssetCache[src]) {
- return durationOfAssetCache[src];
+): Promise<VideoDurationResult> {
+ if (downloadMap.videoDurationResultCache[src]) {
+ return downloadMap.videoDurationResultCache[src];
}
const args = [
@@ -31,18 +27,21 @@ async function getVideoStreamDurationUnlimited(
const duration = task.stdout.match(/duration=([0-9.]+)/);
- const result: Result = {
+ const result: VideoDurationResult = {
duration: duration ? parseFloat(duration[1]) : null,
};
- durationOfAssetCache[src] = result;
+ downloadMap.videoDurationResultCache[src] = result;
return result;
}
export const getVideoStreamDuration = (
+ downloadMap: DownloadMap,
src: string,
ffprobeExecutable: FfmpegExecutable
-): Promise<Result> => {
- return limit(() => getVideoStreamDurationUnlimited(src, ffprobeExecutable));
+): Promise<VideoDurationResult> => {
+ return limit(() =>
+ getVideoStreamDurationUnlimited(downloadMap, src, ffprobeExecutable)
+ );
};
diff --git a/packages/renderer/src/extract-frame-from-video.ts b/packages/renderer/src/extract-frame-from-video.ts
index 893bd171a3..8a114f5f92 100644
--- a/packages/renderer/src/extract-frame-from-video.ts
+++ b/packages/renderer/src/extract-frame-from-video.ts
@@ -125,13 +125,18 @@ const getFrameOfVideoSlow = async ({
const getLastFrameOfVideoFastUnlimited = async (
options: LastFrameOptions
): Promise<Buffer> => {
- const {ffmpegExecutable, ffprobeExecutable, offset, src} = options;
+ const {ffmpegExecutable, ffprobeExecutable, offset, src, downloadMap} =
+ options;
const fromCache = getLastFrameFromCache({...options, offset: 0});
if (fromCache) {
return fromCache;
}
- const {duration} = await getVideoStreamDuration(src, ffprobeExecutable);
+ const {duration} = 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.`
diff --git a/packages/renderer/src/preprocess-audio-track.ts b/packages/renderer/src/preprocess-audio-track.ts
index 85cb1db626..050aa29d3f 100644
--- a/packages/renderer/src/preprocess-audio-track.ts
+++ b/packages/renderer/src/preprocess-audio-track.ts
@@ -1,4 +1,5 @@
import execa from 'execa';
+import type {DownloadMap} from './assets/download-map';
import {getAudioChannelsAndDuration} from './assets/get-audio-channels';
import type {MediaAsset} from './assets/types';
import {calculateFfmpegFilter} from './calculate-ffmpeg-filters';
@@ -14,6 +15,7 @@ type Options = {
asset: MediaAsset;
expectedFrames: number;
fps: number;
+ downloadMap: DownloadMap;
};
const preprocessAudioTrackUnlimited = async ({
@@ -23,8 +25,10 @@ const preprocessAudioTrackUnlimited = async ({
asset,
expectedFrames,
fps,
+ downloadMap,
}: Options): Promise<string | null> => {
const {channels, duration} = await getAudioChannelsAndDuration(
+ downloadMap,
resolveAssetSrc(asset.src),
ffprobeExecutable
);
diff --git a/packages/renderer/src/stitch-frames-to-video.ts b/packages/renderer/src/stitch-frames-to-video.ts
index 096d86eeea..1789138094 100644
--- a/packages/renderer/src/stitch-frames-to-video.ts
+++ b/packages/renderer/src/stitch-frames-to-video.ts
@@ -134,6 +134,7 @@ const getAssetsData = async ({
asset,
expectedFrames,
fps,
+ downloadMap,
});
preprocessProgress[index] = 1;
updateProgress();
diff --git a/packages/renderer/src/test/get-audio-channels.test.ts b/packages/renderer/src/test/get-audio-channels.test.ts
index bec6af9fb3..f0e427f01d 100644
--- a/packages/renderer/src/test/get-audio-channels.test.ts
+++ b/packages/renderer/src/test/get-audio-channels.test.ts
@@ -1,9 +1,11 @@
import {existsSync} from 'fs';
import path from 'path';
import {expect, test} from 'vitest';
+import {makeDownloadMap} from '../assets/download-map';
import {getAudioChannelsAndDuration} from '../assets/get-audio-channels';
test('Get audio channels for video', async () => {
+ const downloadMap = makeDownloadMap();
const videoWithoutAudio = path.join(
__dirname,
'..',
@@ -15,11 +17,16 @@ test('Get audio channels for video', async () => {
'framer-music.mp4'
);
expect(existsSync(videoWithoutAudio)).toEqual(true);
- const channels = await getAudioChannelsAndDuration(videoWithoutAudio, null);
+ const channels = await getAudioChannelsAndDuration(
+ downloadMap,
+ videoWithoutAudio,
+ null
+ );
expect(channels).toEqual({channels: 2, duration: 10});
}, 90000);
test('Get audio channels for video without music', async () => {
+ const downloadMap = makeDownloadMap();
const videoWithAudio = path.join(
__dirname,
'..',
@@ -31,11 +38,16 @@ test('Get audio channels for video without music', async () => {
'framer.mp4'
);
expect(existsSync(videoWithAudio)).toEqual(true);
- const channels = await getAudioChannelsAndDuration(videoWithAudio, null);
+ const channels = await getAudioChannelsAndDuration(
+ downloadMap,
+ videoWithAudio,
+ null
+ );
expect(channels).toEqual({channels: 0, duration: 3.334});
}, 90000);
test('Get audio channels for video with music', async () => {
+ const downloadMap = makeDownloadMap();
const audio = path.join(
__dirname,
'..',
@@ -47,14 +59,15 @@ test('Get audio channels for video with music', async () => {
'sound1.mp3'
);
expect(existsSync(audio)).toEqual(true);
- const channels = await getAudioChannelsAndDuration(audio, null);
+ const channels = await getAudioChannelsAndDuration(downloadMap, audio, null);
expect(channels).toEqual({channels: 2, duration: 56.529});
}, 90000);
test('Throw error if parsing a non video file', () => {
+ const downloadMap = makeDownloadMap();
const tsFile = path.join(__dirname, '..', 'ffmpeg-flags.ts');
expect(existsSync(tsFile)).toEqual(true);
- expect(() => getAudioChannelsAndDuration(tsFile, null)).rejects.toThrow(
- /Invalid data found when processing input/
- );
+ expect(() =>
+ getAudioChannelsAndDuration(downloadMap, tsFile, null)
+ ).rejects.toThrow(/Invalid data found when processing input/);
});