Skip to content

🎥 Make videos programmatically with React

let's go

Jonny Burger837c9cd60153
diff --git a/packages/convert/app/components/AudioWaveform.tsx b/packages/convert/app/components/AudioWaveform.tsx
index 7a338c4ca7..07057adce9 100644
--- a/packages/convert/app/components/AudioWaveform.tsx
+++ b/packages/convert/app/components/AudioWaveform.tsx
@@ -1,27 +1,34 @@
import React, {useMemo} from 'react';
import {AMOUNT_OF_BARS} from '~/lib/waveform-visualizer';
+import {THUMBNAIL_HEIGHT} from './VideoThumbnail';
export const AudioWaveForm: React.FC<{readonly bars: number[]}> = ({bars}) => {
const padded = useMemo(() => {
- const p = [...bars];
+ const p: (number | null)[] = [...bars];
while (p.length < AMOUNT_OF_BARS) {
- p.push(0);
+ p.push(null);
}
return p;
}, [bars]);
return (
- <div className="h-[70px] bg-slate-100 border-b-2 border-black justify-center items-center flex flex-row gap-[1px]">
+ <div
+ style={{
+ height: THUMBNAIL_HEIGHT,
+ }}
+ className=" bg-slate-100 border-b-2 border-black justify-center items-center flex flex-row gap-[1px]"
+ >
{padded.map((bar, i) => {
return (
<div
// eslint-disable-next-line react/no-array-index-key
key={i}
- className="rounded bg-slate-400 inline-block w-[4px]"
+ data-notnull={bar !== null}
+ className="rounded bg-slate-200 inline-block w-[4px] data-[notnull=true]:bg-slate-600"
style={{
- height: Math.max(6, (bar / 255) * 40),
- transition: 'height 0.1s ease',
+ height: Math.max(6, ((bar ?? 0) / 255) * 40),
+ transition: 'height 0.1s ease, color 0.1s ease',
}}
/>
);
diff --git a/packages/convert/app/components/ConvertUi.tsx b/packages/convert/app/components/ConvertUi.tsx
index 0f40c6ae93..f2f52ae9bd 100644
--- a/packages/convert/app/components/ConvertUi.tsx
+++ b/packages/convert/app/components/ConvertUi.tsx
@@ -166,6 +166,9 @@ export default function ConvertUI({
const waveform = makeWaveformVisualizer({
onWaveformBars,
});
+ if (duration) {
+ waveform.setDuration(duration);
+ }
convertMedia({
src: src.type === 'url' ? src.url : src.file,
@@ -183,9 +186,6 @@ export default function ConvertUI({
videoFrames++;
return flipped;
},
- onDurationInSeconds: (d) => {
- waveform.setDuration(d);
- },
onAudioData: ({audioData}) => {
waveform.add(audioData);
return audioData;
@@ -280,6 +280,7 @@ export default function ConvertUI({
};
}, [
onWaveformBars,
+ duration,
src,
userRotation,
logLevel,
diff --git a/packages/convert/app/components/VideoThumbnail.tsx b/packages/convert/app/components/VideoThumbnail.tsx
index a577b38498..c4087a4436 100644
--- a/packages/convert/app/components/VideoThumbnail.tsx
+++ b/packages/convert/app/components/VideoThumbnail.tsx
@@ -2,7 +2,7 @@ import {FastAverageColor} from 'fast-average-color';
import React, {useCallback, useImperativeHandle, useRef, useState} from 'react';
import {useIsNarrow} from '~/lib/is-narrow';
-const THUMBNAIL_HEIGHT = Math.round((350 / 16) * 9);
+export const THUMBNAIL_HEIGHT = Math.round((350 / 16) * 9);
type Props = {
readonly smallThumbOnMobile: boolean;
diff --git a/packages/convert/app/lib/waveform-visualizer.ts b/packages/convert/app/lib/waveform-visualizer.ts
index 9533361a29..5ba3d25dc8 100644
--- a/packages/convert/app/lib/waveform-visualizer.ts
+++ b/packages/convert/app/lib/waveform-visualizer.ts
@@ -15,7 +15,7 @@ export const makeWaveformVisualizer = (options: {
bars.durationInMicroseconds += frame.duration ?? 0;
if (duration !== null) {
const progress = bars.durationInMicroseconds / 1_000_000 / duration;
- const bar = Math.floor(progress * AMOUNT_OF_BARS);
+ const bar = Math.round(progress * AMOUNT_OF_BARS);
if (combinedBars.length < bar) {
const arr = new Uint8Array(
frame.allocationSize({planeIndex: 0, format: 's16'}),