Skip to content

🎥 Make videos programmatically with React

bare minimum example

Jonny Burgerd51c93862d48
diff --git a/packages/example/src/OfflineAudioBuffer/index.tsx b/packages/example/src/OfflineAudioBuffer/index.tsx
index 78e289ce6f..69da3ed2d6 100644
--- a/packages/example/src/OfflineAudioBuffer/index.tsx
+++ b/packages/example/src/OfflineAudioBuffer/index.tsx
@@ -1,4 +1,4 @@
-import {interpolate} from 'remotion';
+import {AbsoluteFill, interpolate} from 'remotion';
import {useCallback, useEffect, useMemo, useState} from 'react';
import {
Audio,
@@ -11,13 +11,12 @@ import {audioBufferToDataUrl} from '@remotion/media-utils';
const C4_FREQUENCY = 261.63;
const sampleRate = 44100;
-const audioDurationInFrames = 300;
export const OfflineAudioBufferExample: React.FC = () => {
const [handle] = useState(() => delayRender());
- const [audioBuffer, setAudioBuffer] = useState<AudioBuffer | null>(null);
- const {fps} = useVideoConfig();
- const lengthInSeconds = audioDurationInFrames / fps;
+ const [audioBuffer, setAudioBuffer] = useState<string | null>(null);
+ const {fps, durationInFrames} = useVideoConfig();
+ const lengthInSeconds = durationInFrames / fps;
const renderAudio = useCallback(async () => {
const offlineContext = new OfflineAudioContext({
@@ -39,55 +38,42 @@ export const OfflineAudioBufferExample: React.FC = () => {
oscillatorNode.stop(currentTime + lengthInSeconds);
const buffer = await offlineContext.startRendering();
- setAudioBuffer(buffer);
+ setAudioBuffer(audioBufferToDataUrl(buffer));
continueRender(handle);
}, [handle, lengthInSeconds]);
- const audioBufferSrc = useMemo(() => {
- if (audioBuffer) {
- const arrayBufferAsBase64 = audioBufferToDataUrl(audioBuffer);
- return arrayBufferAsBase64;
- }
- return null;
- }, [audioBuffer]);
-
useEffect(() => {
renderAudio();
}, [renderAudio]);
return (
- <div
- style={{
- fontFamily: 'Helvetica, Arial',
- fontSize: 50,
- background: '#DC136C',
- color: 'white',
- textAlign: 'center',
- position: 'absolute',
- bottom: 50,
- zIndex: 99999,
- padding: '20px',
- width: '100%',
- }}
- >
- {audioBufferSrc && (
- <Sequence from={100} durationInFrames={100}>
- <Audio
- src={audioBufferSrc}
- startFrom={0}
- endAt={100}
- volume={(f) =>
- interpolate(f, [0, 50, 100], [0, 1, 0], {
- extrapolateLeft: 'clamp',
- extrapolateRight: 'clamp',
- })
- }
- />
- </Sequence>
+ <AbsoluteFill>
+ {audioBuffer && (
+ <Audio
+ src={audioBuffer}
+ startFrom={0}
+ endAt={100}
+ volume={(f) =>
+ interpolate(f, [0, 50, 100], [0, 1, 0], {
+ extrapolateLeft: 'clamp',
+ extrapolateRight: 'clamp',
+ })
+ }
+ />
)}
- Render sound from offline audio buffer
- </div>
+ <AbsoluteFill
+ style={{
+ fontFamily: 'Helvetica, Arial',
+ fontSize: 50,
+ color: 'white',
+ justifyContent: 'center',
+ textAlign: 'center',
+ }}
+ >
+ Render sound from offline audio buffer
+ </AbsoluteFill>
+ </AbsoluteFill>
);
};
diff --git a/packages/example/src/Video.tsx b/packages/example/src/Video.tsx
index 0efd0b8944..408f8f4e28 100644
--- a/packages/example/src/Video.tsx
+++ b/packages/example/src/Video.tsx
@@ -288,7 +288,7 @@ export const Index: React.FC = () => {
width={1080}
height={1080}
fps={30}
- durationInFrames={300}
+ durationInFrames={100}
/>
<Still id="Orb" component={OrbScene} width={2000} height={2000} />
</>