Skip to content

🎥 Make videos programmatically with React

Clean up example and mimic AudioTesting.

Pablo Cuadrado94fb45ded8f7
diff --git a/packages/example/src/OfflineAudioBuffer/OfflineAudioBufferExample.tsx b/packages/example/src/OfflineAudioBuffer/OfflineAudioBufferExample.tsx
deleted file mode 100644
index 2ee671e9d1..0000000000
--- a/packages/example/src/OfflineAudioBuffer/OfflineAudioBufferExample.tsx
+++ /dev/null
@@ -1,74 +0,0 @@
-import {useEffect, useState} from 'react';
-import {Audio, continueRender, delayRender} from 'remotion';
-
-import toWav from 'audiobuffer-to-wav';
-
-function uint8ToBase64(buffer: ArrayBuffer) {
- let binary = '';
- const bytes = new Uint8Array(buffer);
- const len = bytes.byteLength;
- for (let i = 0; i < len; i++) {
- binary += String.fromCharCode(bytes[i]);
- }
- return window.btoa(binary);
-}
-
-export const OfflineAudioBufferExample: React.FC = () => {
- const [handle] = useState(() => delayRender());
- const [audioDataURL, setAudioDataURL] = useState('');
- const C4_FREQUENCY = 261.63;
- const sampleRate = 44100;
- const lengthInSeconds = 5;
-
- const renderAudio = async () => {
- const offlineContext = new OfflineAudioContext({
- numberOfChannels: 2,
- length: sampleRate * lengthInSeconds,
- sampleRate,
- });
- const oscillatorNode = offlineContext.createOscillator();
- const gainNode = offlineContext.createGain();
- oscillatorNode.connect(gainNode);
- gainNode.connect(offlineContext.destination);
- gainNode.gain.setValueAtTime(.5, offlineContext.currentTime);
-
- oscillatorNode.type = "sine";
- oscillatorNode.frequency.value = C4_FREQUENCY;
-
- const {currentTime} = offlineContext;
- oscillatorNode.start(currentTime);
- oscillatorNode.stop(currentTime + 3);
-
- const buffer = await offlineContext.startRendering();
-
- const wavAsArrayBuffer = toWav(buffer);
- const arrayBufferAsBase64 = uint8ToBase64(wavAsArrayBuffer);
- setAudioDataURL('data:audio/wav;base64,' + arrayBufferAsBase64);
-
- continueRender(handle);
- };
-
- useEffect(() => {
- renderAudio();
- }, []);
-
- return (
- <div
- style={{
- fontFamily: 'Helvetica, Arial',
- fontSize: 50,
- background: '#908800',
- color: 'white',
- textAlign: 'center',
- position: 'absolute',
- bottom: 50,
- zIndex: 99999,
- padding: '20px',
- width: '100%'
- }}
- >
- {audioDataURL !== '' && <Audio fromAudioBuffer src={audioDataURL} /> }
- Render sound from offline audio buffer
- </div>
- );
-};
diff --git a/packages/example/src/OfflineAudioBuffer/index.tsx b/packages/example/src/OfflineAudioBuffer/index.tsx
index 42ceb7ecf0..50c2ee2f39 100644
--- a/packages/example/src/OfflineAudioBuffer/index.tsx
+++ b/packages/example/src/OfflineAudioBuffer/index.tsx
@@ -1,15 +1,93 @@
-import {Sequence} from 'remotion';
+import {interpolate} from 'remotion'
+import {useEffect, useState} from 'react';
+import {Audio, Sequence, continueRender, delayRender, useVideoConfig} from 'remotion';
-import {OfflineAudioBufferExample} from './OfflineAudioBufferExample';
+import toWav from 'audiobuffer-to-wav';
+
+function uint8ToBase64(buffer: ArrayBuffer) {
+ let binary = '';
+ const bytes = new Uint8Array(buffer);
+ const len = bytes.byteLength;
+ for (let i = 0; i < len; i++) {
+ binary += String.fromCharCode(bytes[i]);
+ }
+ return window.btoa(binary);
+}
+
+export const OfflineAudioBufferExample: React.FC = () => {
+ const [handle] = useState(() => delayRender());
+ const [audioDataURL, setAudioDataURL] = useState('');
+ const { fps } = useVideoConfig();
+ const C4_FREQUENCY = 261.63;
+ const sampleRate = 44100;
+ const audioDurationInFrames = 300;
+ const lengthInSeconds = audioDurationInFrames / fps;
+
+ const renderAudio = async () => {
+ const offlineContext = new OfflineAudioContext({
+ numberOfChannels: 2,
+ length: sampleRate * lengthInSeconds,
+ sampleRate,
+ });
+ const oscillatorNode = offlineContext.createOscillator();
+ const gainNode = offlineContext.createGain();
+ oscillatorNode.connect(gainNode);
+ gainNode.connect(offlineContext.destination);
+ gainNode.gain.setValueAtTime(.5, offlineContext.currentTime);
+
+ oscillatorNode.type = "sine";
+ oscillatorNode.frequency.value = C4_FREQUENCY;
+
+ const {currentTime} = offlineContext;
+ oscillatorNode.start(currentTime);
+ oscillatorNode.stop(currentTime + 3);
+
+ const buffer = await offlineContext.startRendering();
+
+ const wavAsArrayBuffer = toWav(buffer);
+ const arrayBufferAsBase64 = uint8ToBase64(wavAsArrayBuffer);
+ setAudioDataURL('data:audio/wav;base64,' + arrayBufferAsBase64);
+
+ continueRender(handle);
+ };
+
+ useEffect(() => {
+ renderAudio();
+ }, []);
-const OfflineAudioBuffer: React.FC = () => {
return (
- <div style={{background: '#444456', width: '100%', height: '100%'}}>
- <Sequence from={0} durationInFrames={150}>
- <OfflineAudioBufferExample />
- </Sequence>
+ <div
+ style={{
+ fontFamily: 'Helvetica, Arial',
+ fontSize: 50,
+ background: '#908800',
+ color: 'white',
+ textAlign: 'center',
+ position: 'absolute',
+ bottom: 50,
+ zIndex: 99999,
+ padding: '20px',
+ width: '100%'
+ }}
+ >
+ {audioDataURL !== '' &&
+ <Sequence from={100} durationInFrames={100}>
+ <Audio
+ startFrom={0}
+ endAt={100}
+ src={audioDataURL}
+ volume={(f) =>
+ interpolate(f, [0, 50, 100], [0, 1, 0], {
+ extrapolateLeft: 'clamp',
+ extrapolateRight: 'clamp',
+ })
+ }
+ />
+ </Sequence>
+ }
+ Render sound from offline audio buffer
</div>
);
};
-export default OfflineAudioBuffer;
+export default OfflineAudioBufferExample;
diff --git a/packages/example/src/Video.tsx b/packages/example/src/Video.tsx
index 4842265bff..0efd0b8944 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={150}
+ durationInFrames={300}
/>
<Still id="Orb" component={OrbScene} width={2000} height={2000} />
</>