Skip to content

🎥 Make videos programmatically with React

waveform progress

Jonny Burger2f0cca9f59c8
diff --git a/packages/cli/src/editor/components/AudioWaveform.tsx b/packages/cli/src/editor/components/AudioWaveform.tsx
index bff5482385..3b18ae2869 100644
--- a/packages/cli/src/editor/components/AudioWaveform.tsx
+++ b/packages/cli/src/editor/components/AudioWaveform.tsx
@@ -1,5 +1,6 @@
import React, {useEffect, useMemo, useState} from 'react';
import {getWaveform} from '../helpers/get-waveform';
+import {getWaveformSamples} from '../helpers/reduce-waveform';
import {TIMELINE_LAYER_HEIGHT} from '../helpers/timeline-layout';
import {AudioWaveformBar} from './AudioWaveformBar';
@@ -11,30 +12,14 @@ const container: React.CSSProperties = {
height: TIMELINE_LAYER_HEIGHT,
};
-const filterData = (audioBuffer: Float32Array) => {
- const samples = 200; // Number of samples we want to have in our final data set
- const blockSize = Math.floor(audioBuffer.length / samples); // the number of samples in each subdivision
- const filteredData = [];
- for (let i = 0; i < samples; i++) {
- const blockStart = blockSize * i; // the location of the first sample in the block
- let sum = 0;
- for (let j = 0; j < blockSize; j++) {
- sum = sum + Math.abs(audioBuffer[blockStart + j]); // find the sum of all the samples in the block
- }
- filteredData.push(sum / blockSize); // divide the sum by the block size to get the average
- }
- return filteredData;
-};
-
-const normalizeData = (filteredData: number[]) => {
- const multiplier = Math.pow(Math.max(...filteredData), -1);
- return filteredData.map((n) => n * multiplier);
-};
-
// TODO: Show metadata such as stereo, duration, bitrate
export const AudioWaveform: React.FC<{
src: string;
-}> = ({src}) => {
+ visualizationWidth: number;
+ fps: number;
+ startFrom: number;
+ duration: number;
+}> = ({src, fps, startFrom, duration}) => {
// TODO: Resize to timeline length so it aligns with cursor
const [waveform, setWaveform] = useState<Float32Array | null>(null);
@@ -50,7 +35,7 @@ export const AudioWaveform: React.FC<{
if (!waveform) {
return [];
}
- return normalizeData(filterData(waveform)).map((w, i) => {
+ return getWaveformSamples(waveform, 200).map((w, i) => {
return {
index: i,
amplitude: w,
diff --git a/packages/cli/src/editor/components/Timeline/TimelineElements.tsx b/packages/cli/src/editor/components/Timeline/TimelineElements.tsx
index 72a8571c85..f7a2e107db 100644
--- a/packages/cli/src/editor/components/Timeline/TimelineElements.tsx
+++ b/packages/cli/src/editor/components/Timeline/TimelineElements.tsx
@@ -41,7 +41,7 @@ export const TimelineElements: React.FC = () => {
{timeline.map((track) => (
<div key={track.trackId} style={inner}>
{track.sequences.map((s) => {
- return <TimelineSequence key={s.id} s={s} />;
+ return <TimelineSequence key={s.id} fps={videoConfig.fps} s={s} />;
})}
</div>
))}
diff --git a/packages/cli/src/editor/components/Timeline/TimelineSequence.tsx b/packages/cli/src/editor/components/Timeline/TimelineSequence.tsx
index cac9fa92f8..12591b3779 100644
--- a/packages/cli/src/editor/components/Timeline/TimelineSequence.tsx
+++ b/packages/cli/src/editor/components/Timeline/TimelineSequence.tsx
@@ -24,8 +24,9 @@ const AUDIO_GRADIENT = 'linear-gradient(rgb(16 171 58), rgb(43 165 63) 60%)';
export const TimelineSequence: React.FC<{
s: TSequence;
-}> = ({s}) => {
- const {width} = useWindowSize();
+ fps: number;
+}> = ({s, fps}) => {
+ const {width: windowWidth} = useWindowSize();
// If a duration is 1, it is essentially a still and it should have width 0
const spatialDuration = Internals.FEATURE_FLAG_V2_BREAKING_CHANGES
@@ -39,6 +40,11 @@ export const TimelineSequence: React.FC<{
const lastFrame = (video.durationInFrames ?? 1) - 1;
+ const width =
+ s.duration === Infinity
+ ? windowWidth - TIMELINE_PADDING * 2
+ : (spatialDuration / lastFrame) * windowWidth - TIMELINE_PADDING * 2;
+
const style: React.CSSProperties = useMemo(() => {
return {
background: s.type === 'audio' ? AUDIO_GRADIENT : SEQUENCE_GRADIENT,
@@ -48,14 +54,11 @@ export const TimelineSequence: React.FC<{
height: TIMELINE_LAYER_HEIGHT,
marginTop: 1,
marginLeft: `calc(${(s.from / lastFrame) * 100}%)`,
- width:
- s.duration === Infinity
- ? width - TIMELINE_PADDING * 2
- : (spatialDuration / lastFrame) * width - TIMELINE_PADDING * 2,
+ width,
color: 'white',
overflow: 'hidden',
};
- }, [lastFrame, s.duration, s.from, s.type, spatialDuration, width]);
+ }, [lastFrame, s.from, s.type, width]);
const row: React.CSSProperties = useMemo(() => {
return {
@@ -90,7 +93,15 @@ export const TimelineSequence: React.FC<{
})
: null}
</div>
- {s.type === 'audio' ? <AudioWaveform src={s.src} /> : null}
+ {s.type === 'audio' ? (
+ <AudioWaveform
+ src={s.src}
+ visualizationWidth={width}
+ startFrom={s.from}
+ duration={s.duration}
+ fps={fps}
+ />
+ ) : null}
<Pre>{s.displayName}</Pre>
</div>
);
diff --git a/packages/cli/src/editor/helpers/reduce-waveform.ts b/packages/cli/src/editor/helpers/reduce-waveform.ts
new file mode 100644
index 0000000000..064ea3dd7d
--- /dev/null
+++ b/packages/cli/src/editor/helpers/reduce-waveform.ts
@@ -0,0 +1,25 @@
+const filterData = (audioBuffer: Float32Array, samples: number) => {
+ const blockSize = Math.floor(audioBuffer.length / samples); // the number of samples in each subdivision
+ const filteredData = [];
+ for (let i = 0; i < samples; i++) {
+ const blockStart = blockSize * i; // the location of the first sample in the block
+ let sum = 0;
+ for (let j = 0; j < blockSize; j++) {
+ sum = sum + Math.abs(audioBuffer[blockStart + j]); // find the sum of all the samples in the block
+ }
+ filteredData.push(sum / blockSize); // divide the sum by the block size to get the average
+ }
+ return filteredData;
+};
+
+const normalizeData = (filteredData: number[]) => {
+ const multiplier = Math.pow(Math.max(...filteredData), -1);
+ return filteredData.map((n) => n * multiplier);
+};
+
+export const getWaveformSamples = (
+ waveform: Float32Array,
+ sampleAmount: number
+) => {
+ return normalizeData(filterData(waveform, sampleAmount));
+};