Skip to content

🎥 Make videos programmatically with React

add tests

Jonny Burger4b662f940092
diff --git a/packages/example/src/AudioSmoothness/BufferInterruption.tsx b/packages/example/src/AudioSmoothness/BufferInterruption.tsx
new file mode 100644
index 0000000000..ec4f346580
--- /dev/null
+++ b/packages/example/src/AudioSmoothness/BufferInterruption.tsx
@@ -0,0 +1,72 @@
+import {Video} from '@remotion/media';
+import React, {useEffect, useState} from 'react';
+import {AbsoluteFill, Composition, Sequence, useBufferState} from 'remotion';
+import {calculateMetadataFn} from './NewVideo';
+
+const src = 'https://remotion.media/video.mp4';
+
+const BUFFER_START_FRAME = 60;
+const BUFFER_DURATION_MS = 3000;
+
+const Interrupter: React.FC = () => {
+ const buffer = useBufferState();
+ const [block] = useState(() => buffer.delayPlayback());
+
+ useEffect(() => {
+ const timeout = setTimeout(() => {
+ block.unblock();
+ }, BUFFER_DURATION_MS);
+
+ return () => {
+ clearTimeout(timeout);
+ block.unblock();
+ };
+ }, [block]);
+
+ return (
+ <AbsoluteFill
+ style={{
+ background: 'rgba(255, 0, 0, 0.4)',
+ display: 'flex',
+ alignItems: 'flex-start',
+ justifyContent: 'flex-start',
+ padding: 20,
+ }}
+ >
+ <div
+ style={{
+ background: 'red',
+ color: 'white',
+ padding: '8px 16px',
+ fontFamily: 'sans-serif',
+ fontSize: 24,
+ borderRadius: 4,
+ }}
+ >
+ Buffering for {BUFFER_DURATION_MS}ms (triggered at frame{' '}
+ {BUFFER_START_FRAME})
+ </div>
+ </AbsoluteFill>
+ );
+};
+
+const Component: React.FC = () => {
+ return (
+ <>
+ <Video src={src} debugAudioScheduling />
+ <Sequence from={BUFFER_START_FRAME} layout="none">
+ <Interrupter />
+ </Sequence>
+ </>
+ );
+};
+
+export const AudioSmoothnessBufferInterruptionComp: React.FC = () => {
+ return (
+ <Composition
+ component={Component}
+ id="audio-smoothness-buffer-interruption"
+ calculateMetadata={calculateMetadataFn}
+ />
+ );
+};
diff --git a/packages/example/src/AudioSmoothness/NewVideo.tsx b/packages/example/src/AudioSmoothness/NewVideo.tsx
new file mode 100644
index 0000000000..ec60dee0e0
--- /dev/null
+++ b/packages/example/src/AudioSmoothness/NewVideo.tsx
@@ -0,0 +1,33 @@
+import {Video} from '@remotion/media';
+import React from 'react';
+import {CalculateMetadataFunction, Composition} from 'remotion';
+import {getMediaMetadata} from '../get-media-metadata';
+
+const src = 'https://remotion.media/video.mp4';
+
+export const calculateMetadataFn: CalculateMetadataFunction<
+ Record<string, unknown>
+> = async () => {
+ const {durationInSeconds, dimensions, fps} = await getMediaMetadata(src);
+
+ return {
+ durationInFrames: Math.round(durationInSeconds * fps!),
+ fps: fps!,
+ width: dimensions!.width,
+ height: dimensions!.height,
+ };
+};
+
+const Component: React.FC = () => {
+ return <Video src={src} debugAudioScheduling />;
+};
+
+export const AudioSmoothnessNewVideoComp: React.FC = () => {
+ return (
+ <Composition
+ component={Component}
+ id="audio-smoothness-new-video"
+ calculateMetadata={calculateMetadataFn}
+ />
+ );
+};
diff --git a/packages/example/src/AudioSmoothness/SlicedVideo.tsx b/packages/example/src/AudioSmoothness/SlicedVideo.tsx
new file mode 100644
index 0000000000..ca32140cf2
--- /dev/null
+++ b/packages/example/src/AudioSmoothness/SlicedVideo.tsx
@@ -0,0 +1,43 @@
+import {Audio} from '@remotion/media';
+import React from 'react';
+import {Composition, Sequence, useVideoConfig} from 'remotion';
+
+const SLICE_DURATION_FRAMES = 6; // 0.1sec at 30fps
+const NUM_SLICES = 2;
+const PREMOUNT_SEC = 0.5;
+
+const SlicedVideo: React.FC = () => {
+ const src = 'https://remotion.media/video.mp4';
+ const {fps} = useVideoConfig();
+
+ return (
+ <Sequence premountFor={45}>
+ {new Array(NUM_SLICES).fill(0).map((_, i) => {
+ const from = i * SLICE_DURATION_FRAMES;
+ return (
+ <Sequence
+ key={i}
+ from={from}
+ durationInFrames={SLICE_DURATION_FRAMES}
+ premountFor={PREMOUNT_SEC * fps}
+ >
+ <Audio src={src} trimBefore={from} debugAudioScheduling />
+ </Sequence>
+ );
+ })}
+ </Sequence>
+ );
+};
+
+export const AudioSmoothnessSlicedVideoComp: React.FC = () => {
+ return (
+ <Composition
+ id="audio-smoothness-sliced-video"
+ component={SlicedVideo}
+ fps={30}
+ height={1080}
+ durationInFrames={300}
+ width={1920}
+ />
+ );
+};
diff --git a/packages/example/src/Root.tsx b/packages/example/src/Root.tsx
index b87e15daa7..dc3cc94ef2 100644
--- a/packages/example/src/Root.tsx
+++ b/packages/example/src/Root.tsx
@@ -72,7 +72,6 @@ import StarTest from './Shapes/StarTest';
import TriangleTest from './Shapes/TriangleTest';
import {SimpleImg} from './SimpleImg';
import {SkipZeroFrame} from './SkipZeroFrame';
-import {SlicedVideo} from './SlicedVideo';
import {BaseSpring, SpringWithDuration} from './Spring/base-spring';
import {SeriesTesting} from './StaggerTesting';
import {StaticDemo} from './StaticServer';
@@ -124,6 +123,9 @@ import {ThreeDCheck} from './3DCheck';
import {ThreeDContext} from './3DContext';
import {ThreeDSvgContent} from './3DSvgContent';
import {AnimatedImages} from './AnimatedImage/Avif';
+import {AudioSmoothnessBufferInterruptionComp} from './AudioSmoothness/BufferInterruption';
+import {AudioSmoothnessNewVideoComp} from './AudioSmoothness/NewVideo';
+import {AudioSmoothnessSlicedVideoComp} from './AudioSmoothness/SlicedVideo';
import Amplify from './AudioTesting/Amplify';
import {BrowserTest} from './BrowserTest';
import {EdgeBlur} from './EdgeBlur/EdgeBlur';
@@ -1453,14 +1455,11 @@ export const Index: React.FC = () => {
durationInFrames={300}
width={1080}
/>
- <Composition
- id="sliced-video"
- component={SlicedVideo}
- fps={30}
- height={1080}
- durationInFrames={300}
- width={1920}
- />
+ </Folder>
+ <Folder name="AudioSmoothness">
+ <AudioSmoothnessNewVideoComp />
+ <AudioSmoothnessSlicedVideoComp />
+ <AudioSmoothnessBufferInterruptionComp />
</Folder>
<Folder name="Postmount">
<Composition
diff --git a/packages/example/src/SlicedVideo/index.tsx b/packages/example/src/SlicedVideo/index.tsx
deleted file mode 100644
index 852346d8aa..0000000000
--- a/packages/example/src/SlicedVideo/index.tsx
+++ /dev/null
@@ -1,35 +0,0 @@
-import {Video} from '@remotion/media';
-import React from 'react';
-import {Sequence, useVideoConfig} from 'remotion';
-
-const SLICE_DURATION_FRAMES = 3; // 0.1sec at 30fps
-const NUM_SLICES = 100;
-const PREMOUNT_SEC = 0.5;
-
-export const SlicedVideo: React.FC = () => {
- const src = 'https://remotion.media/video.mp4';
- const {fps} = useVideoConfig();
-
- return (
- <Sequence premountFor={45}>
- {new Array(NUM_SLICES).fill(0).map((_, i) => {
- const from = i * SLICE_DURATION_FRAMES;
- return (
- <Sequence
- key={i}
- from={from}
- durationInFrames={SLICE_DURATION_FRAMES}
- premountFor={PREMOUNT_SEC * fps}
- >
- <Video
- src={src}
- trimBefore={from}
- debugAudioScheduling
- _experimentalInitiallyDrawCachedFrame
- />
- </Sequence>
- );
- })}
- </Sequence>
- );
-};