Skip to content

🎥 Make videos programmatically with React

audio visualization example

Jonny Burger300d95477b16
diff --git a/packages/example/src/AudioVisualization/index.tsx b/packages/example/src/AudioVisualization/index.tsx
new file mode 100644
index 0000000000..b642dc9884
--- /dev/null
+++ b/packages/example/src/AudioVisualization/index.tsx
@@ -0,0 +1,240 @@
+import {useAudioData, visualizeAudio} from '@remotion/media-utils';
+import {transparentize} from 'polished';
+import React from 'react';
+import {
+ AbsoluteFill,
+ Audio,
+ Img,
+ interpolate,
+ useCurrentFrame,
+ useVideoConfig,
+} from 'remotion';
+import styled from 'styled-components';
+import DropDots from '../DropDots/DropDots';
+import music from '../resources/sound1.mp3';
+
+const Background = styled(Img)`
+ height: 100%;
+ width: 120%;
+ margin-left: -15%;
+`;
+
+const Blur = styled(AbsoluteFill)`
+ backdrop-filter: blur(5px);
+`;
+
+const FullSize = styled(AbsoluteFill)`
+ display: flex;
+ justify-content: center;
+ align-items: center;
+`;
+
+const Orb = styled.div`
+ height: 400px;
+ width: 400px;
+ border-radius: 200px;
+ display: flex;
+ justify-content: center;
+ align-items: center;
+ font-size: 70px;
+ font-family: --apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen,
+ Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
+ font-weight: bold;
+ text-transform: lowercase;
+ flex-direction: column;
+`;
+
+const Text: React.FC<{
+ color: string;
+ transform: string;
+ blur: number;
+}> = ({color, transform, blur}) => {
+ const frame = useCurrentFrame();
+ const cool = (offset: number) => Math.sin((frame + offset) / 10);
+ return (
+ <AbsoluteFill
+ style={{
+ textAlign: 'center',
+ display: 'flex',
+ justifyContent: 'center',
+ alignItems: 'center',
+ color,
+ transform,
+ filter: `blur(${blur}px)`,
+ }}
+ >
+ <div
+ style={{
+ transform: `translateY(${cool(0) * 8}px)`,
+ }}
+ >
+ Remotion
+ </div>
+ <div
+ style={{
+ transform: `translateY(${cool(5) * 8}px)`,
+ }}
+ >
+ Nation
+ </div>
+ </AbsoluteFill>
+ );
+};
+
+const AudioVisualization: React.FC = () => {
+ const frame = useCurrentFrame();
+ const {width, height, fps} = useVideoConfig();
+ const metadata = useAudioData(music);
+
+ if (!metadata) {
+ return null;
+ }
+ const visualization = visualizeAudio({
+ fps,
+ frame,
+ metadata,
+ numberOfSamples: 32,
+ });
+
+ const scale =
+ 1 +
+ interpolate(visualization[1], [0.14, 1], [0, 0.6], {
+ extrapolateLeft: 'clamp',
+ });
+
+ const backgroundScale =
+ 1 + interpolate(visualization[visualization.length - 1], [0, 0.7], [0, 1]);
+
+ const circlesOut = visualization.slice(4);
+
+ const rgbEffect = interpolate(
+ visualization[Math.floor(visualization.length / 3)],
+ [0, 0.5],
+ [0, 30]
+ );
+
+ const dropStart = 1989;
+ const dropEnd = 3310;
+
+ const dayInterpolation = [dropStart - 5, dropStart, dropEnd, dropEnd + 5];
+
+ const day = interpolate(frame, dayInterpolation, [1, 0, 0, 1], {
+ extrapolateLeft: 'clamp',
+ extrapolateRight: 'clamp',
+ });
+ const night = interpolate(frame, dayInterpolation, [0, 1, 1, 0], {
+ extrapolateLeft: 'clamp',
+ extrapolateRight: 'clamp',
+ });
+
+ const orbRgb = Math.round(interpolate(day, [0, 1], [30, 255]));
+ const textRgb = Math.round(interpolate(night, [0, 1], [0, 255]));
+ const orbBackground = `rgb(${orbRgb}, ${orbRgb}, ${orbRgb})`;
+ const textColor = `rgba(${textRgb}, ${textRgb}, ${textRgb}, 0.8)`;
+
+ const onlySeconds = circlesOut.filter((x, i) => i % 2 === 0);
+ const circlesToUse = [...onlySeconds, ...onlySeconds];
+ return (
+ <div style={{flex: 1}}>
+ <AbsoluteFill>
+ <AbsoluteFill>
+ <Background
+ style={{transform: `scale(${backgroundScale})`, opacity: night}}
+ src="https://fast-cdn.dynamicwallpaper.club/wallpapers%2Feq8ggec3apr%2Fthumbs%2F800%2F0.jpg?generation=1614257969409557&alt=media"
+ />
+ </AbsoluteFill>
+ <AbsoluteFill>
+ <Background
+ style={{transform: `scale(${backgroundScale})`, opacity: day}}
+ src="https://fast-cdn.dynamicwallpaper.club/wallpapers%2Feq8ggec3apr%2Fthumbs%2F800%2F4.jpg?generation=1614257969529252&alt=media"
+ />
+ </AbsoluteFill>
+ </AbsoluteFill>
+ <Blur />
+ <DropDots
+ opacity={night}
+ volume={interpolate(visualization[1], [0, 0.24], [0, 1], {
+ extrapolateLeft: 'clamp',
+ })}
+ />
+
+ <Audio src={music} />
+ <FullSize>
+ {circlesToUse.map((v, i) => {
+ const leftNeighbour =
+ i === circlesToUse.length - 1
+ ? circlesToUse[0]
+ : circlesToUse[i + 1];
+ const rightNeighbour =
+ i === 0
+ ? circlesToUse[circlesToUse.length - 1]
+ : circlesToUse[i - 1];
+ const a = (i / circlesToUse.length) * Math.PI * 2;
+ const offset =
+ (300 +
+ Math.log(
+ interpolate(
+ (v + leftNeighbour + rightNeighbour) / 3,
+ [0.0, 1],
+ [0, 1],
+ {
+ extrapolateLeft: 'clamp',
+ }
+ ) * 600
+ ) *
+ 6) *
+ day;
+ const x = Math.sin(a) * offset;
+ const y = Math.cos(a) * offset;
+ return (
+ <div
+ style={{
+ backgroundColor: 'white',
+ height: 20,
+ width: 20,
+ borderRadius: 10,
+ position: 'absolute',
+ left: x + width / 2,
+ top: y + height / 2,
+ }}
+ />
+ );
+ })}
+ <Orb
+ style={{
+ transform: `scale(${scale})`,
+ backgroundColor: orbBackground,
+ boxShadow: `0 0 50px ${transparentize(0.5, textColor)}`,
+ }}
+ >
+ <Text
+ blur={2}
+ color="rgba(255, 0, 0, 0.3)"
+ transform={`translateY(${-rgbEffect}px) translateX(${
+ rgbEffect * 2
+ }px)`}
+ />
+ <Text
+ blur={2}
+ color="rgba(0, 255, 0, 0.3)"
+ transform={`translateX(${rgbEffect * 3}px) translateY(${
+ rgbEffect * 3
+ }px)`}
+ />
+ <Text
+ blur={2}
+ color="rgba(0, 0, 255, 0.3)"
+ transform={`translateX(${-rgbEffect * 3}px)`}
+ />
+ <Text
+ blur={0}
+ color={textColor}
+ transform={`translateY(${rgbEffect}px)`}
+ />
+ </Orb>
+ </FullSize>
+ </div>
+ );
+};
+
+export default AudioVisualization;
diff --git a/packages/example/src/DropDots/DropDots.tsx b/packages/example/src/DropDots/DropDots.tsx
new file mode 100644
index 0000000000..e940d682e8
--- /dev/null
+++ b/packages/example/src/DropDots/DropDots.tsx
@@ -0,0 +1,159 @@
+import {linearGradient} from 'polished';
+import {
+ Img,
+ interpolate,
+ random,
+ useCurrentFrame,
+ useVideoConfig,
+} from 'remotion';
+
+const gradients = [
+ ['#845ec2', '#ff5e78'],
+ ['rgb(40, 150, 114)', '#e6dd3b'],
+ ['#e48900', '#be0000'],
+ ['#fff600', '#f48b2a'],
+ ['#23689b', '#487e95'],
+ ['#9d0391', '#120078'],
+];
+
+export const DropDots: React.FC<{
+ opacity: number;
+ volume: number;
+}> = ({opacity, volume}) => {
+ const frame = useCurrentFrame();
+
+ const cycle = 15;
+ const iteration = Math.floor(frame / cycle);
+ const {height, width} = useVideoConfig();
+ const dots = new Array(process.env.NODE_ENV === 'development' ? 45 : 45)
+ .fill(true)
+ .map((x, i) => {
+ const startX = random(`x-${i}-${iteration}`) * width;
+ const startY = random(`y-${i}-${iteration}`) * height;
+ const startRotation = random(`rotation-${i}-${iteration}`) * 360;
+ return {
+ startX,
+ endX:
+ startX +
+ interpolate(random(`x-end-${i}-${iteration}`), [0, 1], [-600, 600]),
+ startY,
+ endY:
+ startY +
+ interpolate(random(`y-end-${i}-${iteration}`), [0, 1], [-600, 600]),
+ startRotation,
+ endRotation:
+ startRotation +
+ interpolate(random(`rotatad-${i}`), [0, 1], [-180, 180]),
+ size: interpolate(
+ random(`size-${i}-${iteration}`),
+ [0, 0.9, 0.901, 1],
+ [40, 40, 160, 160]
+ ),
+
+ background:
+ gradients[
+ Math.floor(random(`color-${i}-${iteration}`) * gradients.length)
+ ],
+ opacity: interpolate(
+ random(`opacity-${i}-${iteration}`),
+ [0, 1],
+ [0.83, 0.95]
+ ),
+ gradId: random(`gradient-${i}-${iteration}`),
+ hasShine: random(`shine-${i}`) > 0.6,
+ shineOpacity: random(`shine-opacity-${i}-${iteration}`) * 0.7,
+ };
+ });
+ const progress = interpolate(frame % cycle, [0, cycle], [0, 1]);
+ return (
+ <div style={{width, height, opacity}}>
+ {dots.map((d) => {
+ const left = interpolate(progress, [0, 1], [d.startX, d.endX]);
+ const top = interpolate(progress, [0, 1], [d.startY, d.endY]);
+ const rotate = interpolate(
+ progress,
+ [0, 1],
+ [d.startRotation, d.endRotation]
+ );
+ return (
+ <div
+ style={{
+ position: 'absolute',
+ left,
+ top,
+ transform: `rotate(${rotate}deg)`,
+ }}
+ >
+ <svg
+ style={{
+ width: d.size * 2,
+ height: d.size * 40,
+ position: 'absolute',
+ top: 0,
+ }}
+ preserveAspectRatio="none"
+ viewBox="0 0 200 4000"
+ >
+ <defs>
+ <filter id="f1" x="0" y="0">
+ <feGaussianBlur in="SourceGraphic" stdDeviation="10" />
+ </filter>
+ <linearGradient id={`${d.gradId}`}>
+ <stop
+ stopColor={d.background[0]}
+ stopOpacity={d.shineOpacity * volume}
+ offset={0}
+ />
+ <stop
+ stopColor={d.background[1]}
+ stopOpacity={0.11 * volume}
+ offset={1}
+ />
+ </linearGradient>
+ </defs>
+ {d.hasShine ? (
+ <path
+ d="M 50 50 L 0 4000 L 200 4000 z"
+ fill={`url(#${d.gradId})`}
+ filter="url(#f1)"
+ />
+ ) : null}
+ </svg>
+ <div
+ style={{
+ height: d.size,
+ width: d.size,
+ borderRadius: d.size / 2,
+ opacity: d.opacity,
+ zIndex: d.size,
+ ...linearGradient({
+ colorStops: d.background,
+ }),
+ boxShadow: `0 0 60px ${
+ linearGradient({
+ colorStops: d.background,
+ }).backgroundColor
+ }`,
+ display: 'flex',
+ justifyContent: 'center',
+ alignItems: 'center',
+ }}
+ >
+ <Img
+ style={{
+ height: (d.size / 3) * 2,
+ width: (d.size / 3) * 2,
+ marginLeft: d.size * 0.05,
+ opacity: 0.55,
+ }}
+ src="https://github.com/JonnyBurger/remotion-logo/blob/main/monochromatic/element-0.png?raw=true"
+ />
+ </div>
+ </div>
+ );
+ })}
+ </div>
+ );
+};
+
+export default DropDots;
diff --git a/packages/example/src/Video.tsx b/packages/example/src/Video.tsx
index 0c3944324a..e06ff074d7 100644
--- a/packages/example/src/Video.tsx
+++ b/packages/example/src/Video.tsx
@@ -170,6 +170,14 @@ export const Index: React.FC = () => {
fps={30}
durationInFrames={180 * 30}
/>
+ <Composition
+ id="drop-dots"
+ lazyComponent={() => import('./DropDots/DropDots')}
+ width={1080}
+ height={1080}
+ fps={30}
+ durationInFrames={180 * 30}
+ />
<Composition
id="audio-testing-mute"
lazyComponent={() => import('./AudioTestingMute')}