🎥 Make videos programmatically with React
alright
Jonny Burger9f404f030bcd
diff --git a/packages/docs/components/AudioWaveformExamples.tsx b/packages/docs/components/AudioWaveformExamples.tsxindex 348767e1fb..fad6709af0 100644--- a/packages/docs/components/AudioWaveformExamples.tsx+++ b/packages/docs/components/AudioWaveformExamples.tsx@@ -29,10 +29,10 @@ const BaseExample: React.FC = () => {const p = smoothenSvgPath({points: waveform.map((x, i) => {- return [- (i / (waveform.length - 1)) * width,- (x - 0.5) * 300 + height / 2,- ];+ return {+ x: (i / (waveform.length - 1)) * width,+ y: (x - 0.5) * 300 + height / 2,+ };}),});@@ -74,10 +74,10 @@ const MovingExample: React.FC = () => {const p = smoothenSvgPath({points: waveform.map((x, i) => {- return [- (i / (waveform.length - 1)) * width,- (x - 0.5) * 300 + height / 2,- ];+ return {+ x: (i / (waveform.length - 1)) * width,+ y: (x - 0.5) * 300 + height / 2,+ };}),});@@ -119,10 +119,10 @@ const PosterizedExample: React.FC = () => {const p = smoothenSvgPath({points: waveform.map((x, i) => {- return [- (i / (waveform.length - 1)) * width,- (x - 0.5) * 300 + height / 2,- ];+ return {+ x: (i / (waveform.length - 1)) * width,+ y: (x - 0.5) * 300 + height / 2,+ };}),});diff --git a/packages/example/src/Root.tsx b/packages/example/src/Root.tsxindex 114993b5e2..68580a0d80 100644--- a/packages/example/src/Root.tsx+++ b/packages/example/src/Root.tsx@@ -121,6 +121,8 @@ import {AnimatedImages} from './AnimatedImage/Avif';import {ParseAndDownloadMedia} from './ParseAndDownloadMedia';import {SmoothTextTransition} from './SmoothTextTransition';import {Seek} from './StudioApis/Seek';+import {VoiceVisualization} from './voice-visualization';+import {TwoChannelVoiceVisualization} from './voice-visualization/two-channel';class Vector2 {readonly x: number;@@ -1534,6 +1536,24 @@ export const Index: React.FC = () => {/></Folder><SmoothTextTransition />+ <Folder name="voice-visualization">+ <Composition+ id="voice-visualization"+ component={VoiceVisualization}+ width={1080}+ height={1080}+ fps={30}+ durationInFrames={100}+ />+ <Composition+ id="two-tone"+ component={TwoChannelVoiceVisualization}+ width={1080}+ height={1080}+ fps={30}+ durationInFrames={100}+ />+ </Folder></>);};diff --git a/packages/example/src/voice-visualization/index.tsx b/packages/example/src/voice-visualization/index.tsxindex bdafa2dc5a..67f536e289 100644--- a/packages/example/src/voice-visualization/index.tsx+++ b/packages/example/src/voice-visualization/index.tsx@@ -4,61 +4,64 @@ import {visualizeAudioWaveform,} from '@remotion/media-utils';import React from 'react';-import {AbsoluteFill, Audio, useCurrentFrame, useVideoConfig} from 'remotion';-import styled from 'styled-components';+import {Audio, useCurrentFrame, useVideoConfig} from 'remotion';import voice from '../resources/voice-short.mp3';-const FullSize = styled(AbsoluteFill)`- display: flex;- justify-content: center;- align-items: center;-`;+const padding = 80;-const VoiceVisualization: React.FC = () => {+export const VoiceVisualization: React.FC = () => {const frame = useCurrentFrame();const {fps} = useVideoConfig();const audioDataVoice = useAudioData(voice);- const {width, height} = useVideoConfig();+ const {width} = useVideoConfig();++ const height = 600;if (!audioDataVoice) {return null;}- const posterized = Math.round(frame / 3) * 3;+ const posterized = Math.round(frame / 1) * 1;const waveform = visualizeAudioWaveform({fps,frame: posterized,audioData: audioDataVoice,- numberOfSamples: 16,- windowInSeconds: 1 / fps,+ numberOfSamples: 40,+ windowInSeconds: 1,channel: 0,});- const p = smoothenSvgPath(- waveform.map((y, i) => {- return [- (i / (waveform.length - 1)) * width,- height / 2 + (y * height) / 2,- ];- })- );+ const p = smoothenSvgPath({+ points: waveform.map((y, i) => {+ return {+ x: (i / (waveform.length - 1)) * width,+ y: height / 2 + (y * height) / 2,+ };+ }),+ });return (- <div style={{flex: 1}}>+ <div style={{flex: 1, padding}} className="flex-1 bg-white"><Audio src={voice} />- <FullSize>+ <div className="flex justify-center items-center"><svg- style={{backgroundColor: 'white'}}viewBox={`0 0 ${width} ${height}`}- width={width}+ style={{+ overflow: 'visible',+ }}+ width={width - padding * 2}height={height}>- <path fill="none" stroke="#0b84f3" strokeWidth={10} d={p} />+ <path+ strokeLinecap="round"+ fill="none"+ stroke="#222"+ strokeWidth={10}+ d={p}+ /></svg>- </FullSize>+ </div></div>);};--export default VoiceVisualization;diff --git a/packages/media-utils/src/create-smooth-svg-path.ts b/packages/media-utils/src/create-smooth-svg-path.tsindex 2e57cf77ef..96502bf02d 100644--- a/packages/media-utils/src/create-smooth-svg-path.ts+++ b/packages/media-utils/src/create-smooth-svg-path.ts@@ -1,18 +1,23 @@-const line = (pointA: [number, number], pointB: [number, number]) => {- const lengthX = pointB[0] - pointA[0];- const lengthY = pointB[1] - pointA[1];+const line = (pointA: Point, pointB: Point) => {+ const lengthX = pointB.x - pointA.x;+ const lengthY = pointB.y - pointA.y;return {length: Math.sqrt(lengthX ** 2 + lengthY ** 2),angle: Math.atan2(lengthY, lengthX),};};-const controlPoint = (- current: [number, number],- previous: [number, number],- next: [number, number],- reverse: boolean,-): [number, number] => {+const controlPoint = ({+ current,+ previous,+ next,+ reverse,+}: {+ current: Point;+ previous: Point;+ next: Point;+ reverse: boolean;+}): Point => {const p = previous || current;const n = next || current;// The smoothing ratio@@ -23,30 +28,42 @@ const controlPoint = (const angle = o.angle + (reverse ? Math.PI : 0);const length = o.length * smoothing;- const x = current[0] + Math.cos(angle) * length;- const y = current[1] + Math.sin(angle) * length;+ const x = current.x + Math.cos(angle) * length;+ const y = current.y + Math.sin(angle) * length;- return [x, y];+ return {x, y};};-export const createSmoothSvgPath = ({points}: {points: [number, number][]}) => {- return points.reduce(- (acc: string, current: [number, number], i: number, a) => {- if (i === 0) {- return `M ${current[0]},${current[1]}`;- }+export type Point = {+ x: number;+ y: number;+};++export const createSmoothSvgPath = ({points}: {points: Point[]}) => {+ return points.reduce((acc: string, current: Point, i: number, a) => {+ if (i === 0) {+ return `M ${current.x},${current.y}`;+ }- const [x, y] = current;+ const {x, y} = current;- const previous = a[i - 1];- const twoPrevious = a[i - 2];- const next = a[i + 1];+ const previous = a[i - 1];+ const twoPrevious = a[i - 2];+ const next = a[i + 1];- const [cp1x, cp1y] = controlPoint(previous, twoPrevious, current, false);- const [cp2x, cp2y] = controlPoint(current, previous, next, true);+ const {x: cp1x, y: cp1y} = controlPoint({+ current: previous,+ previous: twoPrevious,+ next: current,+ reverse: false,+ });+ const {x: cp2x, y: cp2y} = controlPoint({+ current,+ previous,+ next,+ reverse: true,+ });- return `${acc} C ${cp1x},${cp1y} ${cp2x},${cp2y} ${x},${y}`;- },- '',- );+ return `${acc} C ${cp1x},${cp1y} ${cp2x},${cp2y} ${x},${y}`;+ }, '');};