Skip to content

🎥 Make videos programmatically with React

write a test!

Jonny Burger0b5409f011c0
diff --git a/packages/video/src/extract-frame-via-broadcast-channel.ts b/packages/video/src/extract-frame-via-broadcast-channel.ts
index d355d4c01a..25d4616b39 100644
--- a/packages/video/src/extract-frame-via-broadcast-channel.ts
+++ b/packages/video/src/extract-frame-via-broadcast-channel.ts
@@ -38,7 +38,7 @@ if (window.remotion_broadcastChannel && window.remotion_isMainTab) {
timeInSeconds: data.timeInSeconds,
logLevel: data.logLevel,
durationInSeconds: data.durationInSeconds,
- shouldRenderAudio: data.shouldRenderAudio,
+ includeAudio: data.shouldRenderAudio,
});
const videoFrame = frame;
@@ -98,7 +98,7 @@ export const extractFrameViaBroadcastChannel = async ({
src,
timeInSeconds,
durationInSeconds,
- shouldRenderAudio,
+ includeAudio: shouldRenderAudio,
});
return {
diff --git a/packages/video/src/extract-frame.ts b/packages/video/src/extract-frame.ts
index 885e8ba537..006870d57d 100644
--- a/packages/video/src/extract-frame.ts
+++ b/packages/video/src/extract-frame.ts
@@ -7,7 +7,7 @@ import {getSinks, type GetSink} from './get-frames-since-keyframe';
import {makeKeyframeManager} from './keyframe-manager';
import type {LogLevel} from './log';
-const keyframeManager = makeKeyframeManager();
+export const keyframeManager = makeKeyframeManager();
const sinkPromise: Record<string, Promise<GetSink>> = {};
export const extractFrame = async ({
@@ -182,13 +182,13 @@ export const extractFrameAndAudio = async ({
timeInSeconds,
logLevel,
durationInSeconds,
- shouldRenderAudio,
+ includeAudio,
}: {
src: string;
timeInSeconds: number;
logLevel: LogLevel;
durationInSeconds: number;
- shouldRenderAudio: boolean;
+ includeAudio: boolean;
}): Promise<{
frame: VideoFrame | null;
audio: PcmS16AudioData | null;
@@ -199,7 +199,7 @@ export const extractFrameAndAudio = async ({
timeInSeconds,
logLevel,
}),
- shouldRenderAudio
+ includeAudio
? extractAudio({
src,
timeInSeconds,
diff --git a/packages/video/src/keyframe-manager.ts b/packages/video/src/keyframe-manager.ts
index 2446ae7968..db19e40844 100644
--- a/packages/video/src/keyframe-manager.ts
+++ b/packages/video/src/keyframe-manager.ts
@@ -48,6 +48,24 @@ export const makeKeyframeManager = () => {
);
};
+ const getCacheStats = async () => {
+ let count = 0;
+ let totalSize = 0;
+ for (const src in sources) {
+ for (const bank in sources[src]) {
+ const v = await sources[src][bank];
+ const {length, size} = v.getOpenFrameCount();
+ count += length;
+ totalSize += size;
+ if (size === 0) {
+ continue;
+ }
+ }
+ }
+
+ return {count, totalSize};
+ };
+
const clearKeyframeBanksBeforeTime = async ({
timestampInSeconds,
src,
@@ -189,6 +207,7 @@ export const makeKeyframeManager = () => {
return {
requestKeyframeBank,
addKeyframeBank,
+ getCacheStats,
};
};
diff --git a/packages/video/src/test/browser.test.ts b/packages/video/src/test/browser.test.ts
new file mode 100644
index 0000000000..cebd74864b
--- /dev/null
+++ b/packages/video/src/test/browser.test.ts
@@ -0,0 +1,31 @@
+import {assert, expect, test} from 'vitest';
+import {extractFrameAndAudio, keyframeManager} from '../extract-frame';
+
+test('Should be able to extract a frame', async () => {
+ expect(navigator.userAgent).toBeDefined();
+ expect(navigator.userAgent).toContain('Chrome');
+
+ const {audio, frame} = await extractFrameAndAudio({
+ src: 'https://parser.media/video.mp4',
+ timeInSeconds: 1,
+ durationInSeconds: 1 / 30,
+ logLevel: 'info',
+ includeAudio: true,
+ });
+
+ assert(frame);
+ expect(frame.timestamp).toBe(1_000_000);
+
+ assert(audio);
+ expect(audio.sampleRate).toBe(48000);
+
+ // duration = 1 / 30
+ // channels = 2
+ // bits = 16
+ // sampleRate = 48000
+ // 1 / 30 * 2 * 2 * 48000 = 6400
+ expect(audio.data.byteLength).toBe(6400);
+
+ const cacheStats = await keyframeManager.getCacheStats();
+ expect(cacheStats.count).toBe(26);
+});
diff --git a/packages/video/vitest.config.ts b/packages/video/vitest.config.ts
new file mode 100644
index 0000000000..a442ac7e74
--- /dev/null
+++ b/packages/video/vitest.config.ts
@@ -0,0 +1,11 @@
+import {defineConfig} from 'vitest/config';
+
+export default defineConfig({
+ test: {
+ browser: {
+ provider: 'webdriverio',
+ instances: [{browser: 'chrome'}],
+ headless: true,
+ },
+ },
+});