Skip to content

🎥 Make videos programmatically with React

awesome

Jonny Burgercd1d73b8a2aa
diff --git a/packages/media-utils/src/get-audio-data.ts b/packages/media-utils/src/get-audio-data.ts
index a06deeeb7f..d5b2688d79 100644
--- a/packages/media-utils/src/get-audio-data.ts
+++ b/packages/media-utils/src/get-audio-data.ts
@@ -21,7 +21,7 @@ const fn = async (src: string, options?: Options): Promise<AudioData> => {
}
const audioContext = new AudioContext({
- sampleRate: options?.sampleRate ?? 16000,
+ sampleRate: options?.sampleRate ?? 48000,
});
const response = await fetchWithCorsCatch(src);
diff --git a/packages/media-utils/src/get-partial-wave-data.ts b/packages/media-utils/src/get-partial-wave-data.ts
index 49bc13217a..cdded96850 100644
--- a/packages/media-utils/src/get-partial-wave-data.ts
+++ b/packages/media-utils/src/get-partial-wave-data.ts
@@ -11,6 +11,7 @@ export const getPartialWaveData = async ({
toSeconds,
blockAlign,
fileSize,
+ signal,
}: {
dataOffset: number;
src: string;
@@ -21,22 +22,20 @@ export const getPartialWaveData = async ({
toSeconds: number;
blockAlign: number;
fileSize: number;
+ signal: AbortSignal;
}) => {
const startByte =
dataOffset + Math.floor(fromSeconds * sampleRate) * blockAlign;
- const endByte =
- dataOffset + Math.floor(toSeconds * sampleRate - 1) * blockAlign;
-
- if (endByte > fileSize - 1) {
- throw new Error(
- `End byte ${endByte} is greater than file size ${fileSize}`,
- );
- }
+ const endByte = Math.min(
+ fileSize - 1,
+ dataOffset + Math.floor(toSeconds * sampleRate - 1) * blockAlign,
+ );
const response = await fetchWithCorsCatch(src, {
headers: {
range: `bytes=${startByte}-${endByte}`,
},
+ signal,
});
if (response.status !== 206) {
diff --git a/packages/media-utils/src/use-windowed-audio-data.ts b/packages/media-utils/src/use-windowed-audio-data.ts
index a1d304f567..7dd6e01543 100644
--- a/packages/media-utils/src/use-windowed-audio-data.ts
+++ b/packages/media-utils/src/use-windowed-audio-data.ts
@@ -38,6 +38,7 @@ export const useWindowedAudioData = ({
const isMounted = useRef(true);
const [waveProbe, setWaveProbe] = useState<WaveProbe | null>(null);
const [waveFormMap, setWaveformMap] = useState({} as WaveformMap);
+ const requests = useRef<Record<string, AbortController | null>>({});
const [initialWindowInSeconds] = useState(windowInSeconds);
if (windowInSeconds !== initialWindowInSeconds) {
throw new Error('windowInSeconds cannot be changed dynamically');
@@ -76,12 +77,22 @@ export const useWindowedAudioData = ({
const currentWindowIndex = Math.floor(currentTime / windowInSeconds);
const windowsToFetch = useMemo(() => {
+ if (!waveProbe) {
+ return [];
+ }
+
+ const maxWindowIndex = Math.floor(
+ waveProbe.durationInSeconds / windowInSeconds,
+ );
+
return [
currentWindowIndex,
- currentWindowIndex - 1,
- currentWindowIndex + 1,
- ].filter((i) => i >= 0);
- }, [currentWindowIndex]);
+ currentWindowIndex === 0 ? null : currentWindowIndex - 1,
+ currentWindowIndex + 1 > maxWindowIndex ? null : currentWindowIndex + 1,
+ ]
+ .filter((i) => i !== null)
+ .filter((i) => i >= 0);
+ }, [currentWindowIndex, waveProbe, windowInSeconds]);
const fetchAndSetWaveformData = useCallback(
async (windowIndex: number) => {
@@ -89,6 +100,8 @@ export const useWindowedAudioData = ({
throw new Error('Wave probe is not loaded yet');
}
+ const controller = new AbortController();
+ requests.current[windowIndex] = controller;
const partialWaveData = await getPartialWaveData({
bitsPerSample: waveProbe.bitsPerSample,
blockAlign: waveProbe.blockAlign,
@@ -99,7 +112,9 @@ export const useWindowedAudioData = ({
sampleRate: waveProbe.sampleRate,
src,
toSeconds: (windowIndex + 1) * windowInSeconds,
+ signal: controller.signal,
});
+ requests.current[windowIndex] = null;
setWaveformMap((prev) => {
const entries = Object.keys(prev);
@@ -136,11 +151,35 @@ export const useWindowedAudioData = ({
return;
}
+ const windowsToClear = Object.keys(requests.current).filter(
+ (entry) => !windowsToFetch.includes(Number(entry)),
+ );
+ for (const windowIndex of windowsToClear) {
+ const controller = requests.current[windowIndex];
+ if (controller) {
+ controller.abort();
+ requests.current[windowIndex] = null;
+ }
+ }
+
Promise.all(
windowsToFetch.map((windowIndex) => {
return fetchAndSetWaveformData(windowIndex);
}),
).catch((err) => {
+ if ((err as Error).stack?.includes('Cancelled')) {
+ return;
+ }
+
+ if ((err as Error).stack?.toLowerCase()?.includes('aborted')) {
+ return;
+ }
+
+ // firefox
+ if ((err as Error).message?.toLowerCase()?.includes('aborted')) {
+ return;
+ }
+
cancelRender(err);
});
}, [fetchAndSetWaveformData, waveProbe, windowsToFetch]);
@@ -149,6 +188,19 @@ export const useWindowedAudioData = ({
return waveFormMap[currentWindowIndex] ?? null;
}, [currentWindowIndex, waveFormMap]);
+ useLayoutEffect(() => {
+ if (currentAudioData) {
+ return;
+ }
+
+ const handle = delayRender(
+ `Waiting for audio data with src="${src}" to be loaded`,
+ );
+ return () => {
+ continueRender(handle);
+ };
+ }, [currentAudioData, src]);
+
return {
audioData: currentAudioData,
dataOffsetInSeconds: currentWindowIndex * windowInSeconds,
diff --git a/packages/template-audiogram/src/Root.tsx b/packages/template-audiogram/src/Root.tsx
index bcb8b674c2..0911090cc1 100644
--- a/packages/template-audiogram/src/Root.tsx
+++ b/packages/template-audiogram/src/Root.tsx
@@ -49,7 +49,7 @@ export const RemotionRoot: React.FC = () => {
);
return {
- durationInFrames: Math.round(durationInSeconds * FPS),
+ durationInFrames: Math.floor(durationInSeconds * FPS),
props: {
...props,
captions,