Skip to content

🎥 Make videos programmatically with React

looking good

Jonny Burgerd9ddded89549
diff --git a/packages/example/src/Video.tsx b/packages/example/src/Video.tsx
index 9272e1ff87..da79b1aae8 100644
--- a/packages/example/src/Video.tsx
+++ b/packages/example/src/Video.tsx
@@ -360,17 +360,6 @@ export const Index: React.FC = () => {
durationInFrames={180 * 30}
defaultProps={{}}
/>
- <Composition
- id="audio-visualization-wave"
- lazyComponent={() => import('./AudioVisualization')}
- width={1080}
- height={1080}
- fps={30}
- defaultProps={{
- isWaveform: true,
- }}
- durationInFrames={30 * 30}
- />
</Folder>
<Folder name="three">
<Still id="Orb" component={OrbScene} width={2000} height={2000} />
diff --git a/packages/example/src/voice-visualization/index.tsx b/packages/example/src/voice-visualization/index.tsx
index b16d31c5ee..a61a044f72 100644
--- a/packages/example/src/voice-visualization/index.tsx
+++ b/packages/example/src/voice-visualization/index.tsx
@@ -31,25 +31,25 @@ const VoiceVisualization: React.FC = () => {
return null;
}
+ const posterized = Math.round(frame / 3) * 3;
+
const waveform = visualizeAudioWaveform({
fps,
- frame,
+ frame: posterized,
audioData: audioDataVoice,
- numberOfSamples: 16,
- windowInSeconds: 1 / fps,
+ numberOfSamples: 64,
+ windowInSeconds: 0.5,
});
const p = createSmoothSvgPath(
- waveform.map((p, i) => {
+ waveform.map((y, i) => {
return [
- (i / (waveform.length - 1)) * width * 2 - width / 2,
- p * height + height / 2,
+ (i / (waveform.length - 1)) * width,
+ height / 2 + ((y * height) / 2) * 5,
];
})
);
- const filledPath = `${p} L ${width} ${height} L 0 ${height} z`;
-
return (
<div style={{flex: 1}}>
<Audio src={voice} />
@@ -61,7 +61,7 @@ const VoiceVisualization: React.FC = () => {
width={width}
height={height}
>
- <path fill="none" stroke="black" strokeWidth={10} d={filledPath} />
+ <path fill="none" stroke="#0b84f3" strokeWidth={10} d={p} />
</svg>
</Orb>
</FullSize>
diff --git a/packages/media-utils/src/create-smooth-svg-path.ts b/packages/media-utils/src/create-smooth-svg-path.ts
index 1414fdd243..b0b1f79429 100644
--- a/packages/media-utils/src/create-smooth-svg-path.ts
+++ b/packages/media-utils/src/create-smooth-svg-path.ts
@@ -1,29 +1,51 @@
-export const createSmoothSvgPath = (points: [number, number][]) => {
+const line = (pointA: [number, number], pointB: [number, number]) => {
+ const lengthX = pointB[0] - pointA[0];
+ const lengthY = pointB[1] - pointA[1];
+ 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 p = previous || current;
+ const n = next || current;
+ // The smoothing ratio
+ const smoothing = 0.2;
+ // Properties of the opposed-line
+ const o = line(p, n);
+
+ 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;
+
+ return [x, y];
+};
+
+export const smoothenSvgPath = (points: [number, number][]) => {
return points.reduce(
- (acc: string | null, point: [number, number], i: number, a) => {
+ (acc: string, current: [number, number], i: number, a) => {
if (i === 0) {
- return `M ${point[0]},${point[1]}`;
+ return `M ${current[0]},${current[1]}`;
}
- const p0 = a[i - 2] || a[i - 1];
- const x0 = p0[0];
- const y0 = p0[1];
- const p1 = a[i - 1];
- const x1 = p1[0];
- const y1 = p1[1];
- const x = point[0];
- const y = point[1];
- const cp1x = (2 * x0 + x1) / 3;
- const cp1y = (2 * y0 + y1) / 3;
- const cp2x = (x0 + 2 * x1) / 3;
- const cp2y = (y0 + 2 * y1) / 3;
- const cp3x = (x0 + 4 * x1 + x) / 6;
- const cp3y = (y0 + 4 * y1 + y) / 6;
- if (i === a.length - 1) {
- return `${acc} C ${cp1x},${cp1y} ${cp2x},${cp2y} ${cp3x},${cp3y} C${x},${y} ${x},${y} ${x},${y}`;
- }
+ const [x, y] = current;
+
+ 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);
- return `${acc} C ${cp1x},${cp1y} ${cp2x},${cp2y} ${cp3x},${cp3y}`;
+ return `${acc} C ${cp1x},${cp1y} ${cp2x},${cp2y} ${x},${y}`;
},
''
);
diff --git a/packages/media-utils/src/get-wave-form-samples.ts b/packages/media-utils/src/get-wave-form-samples.ts
index ef89163714..57339ebaa5 100644
--- a/packages/media-utils/src/get-wave-form-samples.ts
+++ b/packages/media-utils/src/get-wave-form-samples.ts
@@ -1,17 +1,21 @@
export type SampleOutputRange = 'minus-one-to-one' | 'zero-to-one';
-const filterData = (
- audioBuffer: Float32Array,
- samples: number,
- outputRange: SampleOutputRange
-) => {
- const blockSize = Math.floor(audioBuffer.length / samples); // the number of samples in each subdivision
+export const getWaveformSamples = ({
+ audioBuffer,
+ numberOfSamples,
+ outputRange,
+}: {
+ audioBuffer: Float32Array;
+ numberOfSamples: number;
+ outputRange: SampleOutputRange;
+}) => {
+ const blockSize = Math.floor(audioBuffer.length / numberOfSamples); // the number of samples in each subdivision
if (blockSize === 0) {
return [];
}
const filteredData = [];
- for (let i = 0; i < samples; i++) {
+ for (let i = 0; i < numberOfSamples; i++) {
const blockStart = blockSize * i; // the location of the first sample in the block
let sum = 0;
for (let j = 0; j < blockSize; j++) {
@@ -26,11 +30,3 @@ const filterData = (
return filteredData;
};
-
-export const getWaveformSamples = (
- waveform: Float32Array,
- sampleAmount: number,
- outputRange: SampleOutputRange
-) => {
- return filterData(waveform, sampleAmount, outputRange);
-};
diff --git a/packages/media-utils/src/get-waveform-portion.ts b/packages/media-utils/src/get-waveform-portion.ts
index e8c150fc63..6fb957e16f 100644
--- a/packages/media-utils/src/get-waveform-portion.ts
+++ b/packages/media-utils/src/get-waveform-portion.ts
@@ -6,6 +6,29 @@ type Bar = {
amplitude: number;
};
+const concatArrays = (arrays: Float32Array[]): Float32Array => {
+ // sum of individual array lengths
+ const totalLength = arrays.reduce((acc, value) => acc + value.length, 0);
+
+ const result = new Float32Array(totalLength);
+
+ // for each array - copy it over result
+ // next array is copied right after the previous one
+ let length = 0;
+ for (const array of arrays) {
+ result.set(array, length);
+ length += array.length;
+ }
+
+ return result;
+};
+
+type Truthy<T> = T extends false | '' | 0 | null | undefined ? never : T;
+
+function truthy<T>(value: T): value is Truthy<T> {
+ return Boolean(value);
+}
+
const getWaveformPortion = ({
audioData,
startTimeInSeconds,
@@ -19,20 +42,41 @@ const getWaveformPortion = ({
numberOfSamples: number;
outputRange?: SampleOutputRange;
}): Bar[] => {
+ const waveform = audioData.channelWaveforms[0];
+
const startSample = Math.floor(
- (startTimeInSeconds / audioData.durationInSeconds) *
- audioData.channelWaveforms[0].length
+ (startTimeInSeconds / audioData.durationInSeconds) * waveform.length
);
const endSample = Math.floor(
((startTimeInSeconds + durationInSeconds) / audioData.durationInSeconds) *
- audioData.channelWaveforms[0].length
+ waveform.length
);
- return getWaveformSamples(
- audioData.channelWaveforms[0].slice(startSample, endSample),
+ const samplesBeforeStart = 0 - startSample;
+ const samplesAfterEnd = endSample - waveform.length;
+
+ const clampedStart = Math.max(startSample, 0);
+ const clampedEnd = Math.min(waveform.length, endSample);
+
+ const padStart =
+ samplesBeforeStart > 0
+ ? new Float32Array(samplesBeforeStart).fill(0)
+ : null;
+ const padEnd =
+ samplesAfterEnd > 0 ? new Float32Array(samplesAfterEnd).fill(0) : null;
+ console.log({samplesBeforeStart, samplesAfterEnd});
+ const arrs = [
+ padStart,
+ waveform.slice(clampedStart, clampedEnd),
+ padEnd,
+ ].filter(truthy);
+ const audioBuffer = arrs.length === 1 ? arrs[0] : concatArrays(arrs);
+
+ return getWaveformSamples({
+ audioBuffer,
numberOfSamples,
- outputRange
- ).map((w, i) => {
+ outputRange,
+ }).map((w, i) => {
return {
index: i,
amplitude: w,
diff --git a/packages/media-utils/src/index.ts b/packages/media-utils/src/index.ts
index 61c7881f02..3bf662c4b7 100644
--- a/packages/media-utils/src/index.ts
+++ b/packages/media-utils/src/index.ts
@@ -1,5 +1,5 @@
export {audioBufferToDataUrl} from './audio-buffer/audio-url-helpers';
-export {createSmoothSvgPath} from './create-smooth-svg-path';
+export {smoothenSvgPath} from './create-smooth-svg-path';
export {getAudioData} from './get-audio-data';
export {
getAudioDuration,
diff --git a/packages/media-utils/src/visualize-audio-waveform.ts b/packages/media-utils/src/visualize-audio-waveform.ts
index 22828bc773..4cfd11c9a8 100644
--- a/packages/media-utils/src/visualize-audio-waveform.ts
+++ b/packages/media-utils/src/visualize-audio-waveform.ts
@@ -35,13 +35,7 @@ const visualizeAudioWaveformFrame = ({
const time = frame / fps;
- const max = audioData.durationInSeconds - windowInSeconds / 2;
- const min = windowInSeconds / 2;
-
- const startTimeInSeconds = Math.min(
- max,
- Math.max(min, time - windowInSeconds / 2)
- );
+ const startTimeInSeconds = time - windowInSeconds / 2;
return getWaveformPortion({
audioData,