🎥 Make videos programmatically with React
alright
JonnyBurger8b629f6e2821
diff --git a/packages/renderer/src/assets/change-tempo.ts b/packages/renderer/src/assets/change-tempo.tsindex 5a49f7e9a3..eba92c5c98 100644--- a/packages/renderer/src/assets/change-tempo.ts+++ b/packages/renderer/src/assets/change-tempo.ts@@ -1,3 +1,5 @@+import {applyToneFrequency} from './change-tonefrequency';+function clamp16(x: number): number {const y = Math.round(x);return y < -32768 ? -32768 : y > 32767 ? 32767 : y;@@ -235,3 +237,57 @@ export function wsolaInt16Interleaved(return result;}++/**+ * Reads a WAV file, applies WSOLA tempo modification, and writes it back.+ * Ignores the first 44 bytes (WAV header) and treats the rest as interleaved Int16 PCM.+ *+ * @param filePath Path to the WAV file to process+ * @param tempoFactor Tempo factor: >1 = faster/shorter, <1 = slower/longer+ */+export async function processWavFileWithWSOLA(+ filePath: string,+ tempoFactor: number,+): Promise<void> {+ const fs = await import('fs/promises');++ // Read the file+ const fileBuffer = await fs.readFile(filePath);++ // Skip first 44 bytes (WAV header) and create Int16Array+ const audioData = new Int16Array(fileBuffer.buffer, 44);++ // Apply WSOLA with 2 channels (stereo)+ const processedAudio = applyToneFrequency(+ audioData.length / 2,+ audioData,+ tempoFactor,+ );++ // Create new buffer with original header + processed audio+ const newBuffer = new Uint8Array(44 + processedAudio.length * 2);++ // Copy original header (first 44 bytes)+ newBuffer.set(fileBuffer.subarray(0, 44), 0);++ // Copy processed audio data+ const processedBytes = new Uint8Array(processedAudio.buffer);+ newBuffer.set(processedBytes, 44);++ // Update file size in WAV header (bytes 4-7)+ const newFileSize = newBuffer.length - 8;+ newBuffer[4] = newFileSize & 0xff;+ newBuffer[5] = (newFileSize >> 8) & 0xff;+ newBuffer[6] = (newFileSize >> 16) & 0xff;+ newBuffer[7] = (newFileSize >> 24) & 0xff;++ // Update data chunk size in WAV header (bytes 40-43)+ const newDataSize = processedAudio.length * 2;+ newBuffer[40] = newDataSize & 0xff;+ newBuffer[41] = (newDataSize >> 8) & 0xff;+ newBuffer[42] = (newDataSize >> 16) & 0xff;+ newBuffer[43] = (newDataSize >> 24) & 0xff;++ // Write the processed file back+ await fs.writeFile(filePath, newBuffer);+}diff --git a/packages/renderer/src/assets/inline-audio-mixing.ts b/packages/renderer/src/assets/inline-audio-mixing.tsindex 7d9ad7d346..29cd2bea12 100644--- a/packages/renderer/src/assets/inline-audio-mixing.ts+++ b/packages/renderer/src/assets/inline-audio-mixing.ts@@ -3,6 +3,7 @@ import path from 'node:path';import type {InlineAudioAsset} from 'remotion/no-react';import {deleteDirectory} from '../delete-directory';import {DEFAULT_SAMPLE_RATE} from '../sample-rate';+import {processWavFileWithWSOLA} from './change-tempo';import {makeAndReturn} from './download-map';const numberTo32BiIntLittleEndian = (num: number) => {@@ -133,9 +134,13 @@ export const makeInlineAudioMixing = (dir: string) => {); // Remaining size};- const finish = () => {+ const finish = async () => {for (const fd of Object.keys(openFiles)) {- console.log('finished', fd, toneFrequencies[fd]);+ const frequency = toneFrequencies[fd];+ if (frequency !== 1) {+ console.log('processing wsofa', fd, frequency);+ await processWavFileWithWSOLA(fd, toneFrequencies[fd]);+ }}};@@ -161,18 +166,20 @@ export const makeInlineAudioMixing = (dir: string) => {trimLeftOffset,trimRightOffset,});++ const filePath = getFilePath(asset);+if (- toneFrequencies[asset.id] !== undefined &&- toneFrequencies[asset.id] !== asset.toneFrequency+ toneFrequencies[filePath] !== undefined &&+ toneFrequencies[filePath] !== asset.toneFrequency) {throw new Error(`toneFrequency must be the same across the audio, got ${asset.toneFrequency}, but before it was ${toneFrequencies[asset.id]}`,);}- toneFrequencies[asset.id] = asset.toneFrequency;- const filePath = getFilePath(asset);const fileDescriptor = openFiles[filePath];+ toneFrequencies[filePath] = asset.toneFrequency;let arr = new Int16Array(asset.audio);const isFirst = asset.frame === firstFrame;diff --git a/packages/renderer/src/create-audio.ts b/packages/renderer/src/create-audio.tsindex 2c414f6b29..06dd9f21a8 100644--- a/packages/renderer/src/create-audio.ts+++ b/packages/renderer/src/create-audio.ts@@ -123,7 +123,7 @@ export const createAudio = async ({}),);- downloadMap.inlineAudioMixing.finish();+ await downloadMap.inlineAudioMixing.finish();const inlinedAudio = downloadMap.inlineAudioMixing.getListOfAssets();