π₯ Make videos programmatically with React
use ffmpeg π
JonnyBurger62c572bb5840
diff --git a/packages/renderer/src/assets/apply-tone-frequency.ts b/packages/renderer/src/assets/apply-tone-frequency.tsnew file mode 100644index 0000000000..c479e74bd5--- /dev/null+++ b/packages/renderer/src/assets/apply-tone-frequency.ts@@ -0,0 +1,64 @@+import {callFf} from '../call-ffmpeg';+import type {LogLevel} from '../log-level';+import {Log} from '../logger';+import type {CancelSignal} from '../make-cancel-signal';+import {DEFAULT_SAMPLE_RATE} from '../sample-rate';++export const applyToneFrequencyUsingFfmpeg = async ({+ input,+ output,+ toneFrequency,+ indent,+ logLevel,+ binariesDirectory,+ cancelSignal,+}: {+ input: string;+ output: string;+ toneFrequency: number;+ indent: boolean;+ logLevel: LogLevel;+ binariesDirectory: string | null;+ cancelSignal: CancelSignal | undefined;+}) => {+ const filter = `asetrate=${DEFAULT_SAMPLE_RATE}*${toneFrequency},aresample=${DEFAULT_SAMPLE_RATE},atempo=1/${toneFrequency}`;++ const args = [+ '-hide_banner',+ '-i',+ input,+ ['-ac', '2'],+ '-filter:a',+ filter,+ ['-c:a', 'pcm_s16le'],+ ['-ar', String(DEFAULT_SAMPLE_RATE)],+ '-y',+ output,+ ].flat(2);++ Log.verbose(+ {indent, logLevel},+ 'Changing tone frequency using FFmpeg:',+ JSON.stringify(args.join(' ')),+ 'Filter:',+ filter,+ );+ const startTimestamp = Date.now();++ const task = callFf({+ bin: 'ffmpeg',+ args,+ indent,+ logLevel,+ binariesDirectory,+ cancelSignal,+ });++ await task;++ Log.verbose(+ {indent, logLevel},+ 'Changed tone frequency using FFmpeg',+ `${Date.now() - startTimestamp}ms`,+ );+};diff --git a/packages/renderer/src/assets/change-tempo.ts b/packages/renderer/src/assets/change-tempo.tsdeleted file mode 100644index 8b6dcbed33..0000000000--- a/packages/renderer/src/assets/change-tempo.ts+++ /dev/null@@ -1,399 +0,0 @@-import fs from 'fs/promises';-import {DEFAULT_SAMPLE_RATE} from '../sample-rate';--function clamp16(x: number): number {- const y = Math.round(x);- return y < -32768 ? -32768 : y > 32767 ? 32767 : y;-}--/**- * WSOLA time-scale modification for interleaved Int16 PCM (multi-channel).- * - Preserves pitch approximately while changing tempo by factor f.- * - Works for N interleaved channels.- * - Mitigates head/tail fade-out via overlap-weight normalization and boundary reinforcement.- *- * @param input Interleaved Int16 PCM (e.g., LRLRLR... for stereo)- * @param channels Number of channels (>=1)- * @param f Tempo factor: >1 = faster/shorter, <1 = slower/longer- * @param opts Optional tuning parameters- * @returns Interleaved Int16Array with length β round(input.length * f)- */-export function wsolaInt16Interleaved(- input: Int16Array,- channels: number,- f: number,-): Int16Array {- if (!Number.isFinite(f) || f <= 0)- throw new Error('f must be a positive finite number');- if (!Number.isInteger(channels) || channels <= 0)- throw new Error('channels must be a positive integer');-- const n = input.length;- if (n === 0) return new Int16Array(0);- if (n % channels !== 0)- throw new Error('input length must be a multiple of channels');-- // Parameters and sensible defaults- const sampleRate = 48_000;- const frameMs = 30; // 20β40 ms typical- const overlapRatio = 0.5;- const searchMs = 8; // +/- 8 ms local search- const winKind = 'hann';- const headReinf = 3;- const tailReinf = 3;-- // Work per-channel- const samplesPerChannel = (n / channels) | 0;-- // Frame and hop sizing- const frameSize = Math.max(128, Math.floor((sampleRate * frameMs) / 1_000));- const overlap = Math.floor(frameSize * overlapRatio);- const anaHop = Math.max(1, frameSize - overlap);- const synHop = Math.max(1, Math.round(anaHop * f));-- // Search radius in samples- const searchRadius = Math.max(0, Math.floor((sampleRate * searchMs) / 1_000));-- // Window- const win = new Float32Array(frameSize);- for (let i = 0; i < frameSize; i++) {- const x = (Math.PI * 2 * i) / (frameSize - 1);- win[i] =- winKind === 'hann' ? 0.5 * (1 - Math.cos(x)) : 0.54 - 0.46 * Math.cos(x); // Hamming- }-- // Estimate output length per channel and allocate with extra headroom- const estFrames = Math.max(- 1,- Math.ceil(Math.max(0, samplesPerChannel - frameSize) / anaHop) + 1,- );- const estLen = Math.max(0, frameSize + synHop * (estFrames - 1));- const extraHead = frameSize * (headReinf + 1);- const extraTail = frameSize * (tailReinf + 2);- const outLenAlloc = estLen + searchRadius + extraHead + extraTail;-- const out = Array.from(- {length: channels},- () => new Float32Array(outLenAlloc),- );- const outWeight = new Float32Array(outLenAlloc);-- // Temporary buffers- const chanFrames = Array.from(- {length: channels},- () => new Float32Array(frameSize),- );- const guideFrame = new Float32Array(frameSize);-- // Helpers- function readChannelFrame(chan: number, start: number, dst: Float32Array) {- let srcIndex = start * channels + chan;- for (let i = 0; i < frameSize; i++) {- const pos = start + i;- dst[i] = pos >= 0 && pos < samplesPerChannel ? input[srcIndex] : 0;- srcIndex += channels;- }- }-- function readGuideFrame(start: number) {- for (let i = 0; i < frameSize; i++) {- const pos = start + i;- if (pos >= 0 && pos < samplesPerChannel) {- let sum = 0;- const base = pos * channels;- for (let c = 0; c < channels; c++) sum += input[base + c];- guideFrame[i] = sum / channels;- } else {- guideFrame[i] = 0;- }- }- }-- // Find best local alignment around outPos using normalized cross-correlation- function bestAlignment(outPoss: number): number {- let bestShift = 0;- let bestScore = -Infinity;- for (let shift = -searchRadius; shift <= searchRadius; shift++) {- const pos = outPoss + shift - overlap;- let score = 0;- let normA = 0;- let normB = 0;- for (let i = 0; i < overlap; i++) {- const idx = pos + i;- const outVal = idx >= 0 && idx < outLenAlloc ? out[0][idx] : 0; // channel 0 proxy- const frmVal = guideFrame[i];- score += outVal * frmVal;- normA += outVal * outVal;- normB += frmVal * frmVal;- }-- const denom = Math.sqrt((normA || 1e-9) * (normB || 1e-9));- const corr = score / denom;- if (corr > bestScore) {- bestScore = corr;- bestShift = shift;- }- }-- return bestShift;- }-- // Overlap-add a frame for all channels at writeStart with windowing- function olaAllChannels(writeStart: number) {- for (let c = 0; c < channels; c++) {- for (let i = 0; i < frameSize; i++) {- const idx = writeStart + i;- if (idx >= 0 && idx < outLenAlloc) {- const w = win[i];- out[c][idx] += chanFrames[c][i] * w;- if (c === 0) outWeight[idx] += w; // track weights once- }- }- }- }-- // 1) Seed: place the first frame at t=0- readGuideFrame(0);- for (let c = 0; c < channels; c++) readChannelFrame(c, 0, chanFrames[c]);- olaAllChannels(0);-- // 2) Head reinforcement: place extra frames whose writeStart <= 0- for (let h = 0; h < headReinf; h++) {- // Option 1: reuse the first analysis position to strengthen early region- const headIn = Math.min(- anaHop * h,- Math.max(0, samplesPerChannel - frameSize),- );- readGuideFrame(headIn);- for (let c = 0; c < channels; c++)- readChannelFrame(c, headIn, chanFrames[c]);- // Align around outPos=0 so we bias writeStart near/before 0- const shift = bestAlignment(0);- const writeStart = shift - overlap; // likely negative; ok, we clamp on write- olaAllChannels(writeStart);- }-- // 3) Main WSOLA loop- let inPos = anaHop; // next analysis position (we already seeded at 0)- let outPos = synHop; // next synthesis position-- while (inPos < samplesPerChannel - 1) {- readGuideFrame(inPos);- for (let c = 0; c < channels; c++)- readChannelFrame(c, inPos, chanFrames[c]);-- const shift = bestAlignment(outPos);- const writeStart = outPos + shift - overlap;- olaAllChannels(writeStart);-- inPos += anaHop;- outPos += synHop;-- // Safety: if we're very close to capacity, break to handle tail separately- if (outPos > outLenAlloc - (frameSize + searchRadius + 8)) break;- }-- // 4) Tail reinforcement: ensure the end gets full coverage- // Place a few extra frames around the last outPos using the last available input frames.- for (let t = 0; t < tailReinf; t++) {- const tailIn = Math.max(- 0,- Math.min(samplesPerChannel - frameSize, inPos - anaHop * t),- );- readGuideFrame(tailIn);- for (let c = 0; c < channels; c++)- readChannelFrame(c, tailIn, chanFrames[c]);-- const shift = bestAlignment(outPos);- const writeStart = outPos + shift - overlap;- olaAllChannels(writeStart);-- outPos += synHop;- }-- // 5) Normalize by accumulated weights BEFORE trimming- for (let i = 0; i < outLenAlloc; i++) {- const w = outWeight[i];- if (w > 1e-9) {- const inv = 1 / w;- for (let c = 0; c < channels; c++) out[c][i] *= inv;- } else {- for (let c = 0; c < channels; c++) out[c][i] = 0;- }- }-- // 6) Produce final interleaved Int16Array with length β round(n * f)- const targetPerChan = Math.max(1, Math.round(samplesPerChannel * f));- const targetTotal = targetPerChan * channels;- const result = new Int16Array(targetTotal);-- // Interleave and clamp/round- for (let i = 0; i < targetPerChan; i++) {- for (let c = 0; c < channels; c++) {- const v = i < out[c].length ? out[c][i] : 0;- const y = clamp16(v);- result[i * channels + c] = y;- }- }-- 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> {- // 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);-- // Write the processed file back- await fs.writeFile(filePath, newBuffer);-}--export const NUMBER_OF_CHANNELS = 2;--export const applyToneFrequency = (- numberOfFrames: number,- audioData: Int16Array,- toneFrequency: number,-): Int16Array => {- // In FFmpeg, we apply toneFrequency as follows:- // `asetrate=${DEFAULT_SAMPLE_RATE}*${toneFrequency},aresample=${DEFAULT_SAMPLE_RATE},atempo=1/${toneFrequency}`- // So there are 2 steps:- // 1. Change the assumed sample rate- // 2. Resample to 48Khz- // 3. Apply playback rate-- const step1SampleRate = DEFAULT_SAMPLE_RATE * toneFrequency;-- const newNumberOfFrames = Math.round(- numberOfFrames * (DEFAULT_SAMPLE_RATE / step1SampleRate),- );-- const step2Data = new Int16Array(newNumberOfFrames * NUMBER_OF_CHANNELS);-- const chunkSize = numberOfFrames / newNumberOfFrames;-- resampleAudioData({- sourceChannels: audioData,- destination: step2Data,- targetFrames: newNumberOfFrames,- chunkSize,- });-- const step3Data = wsolaInt16Interleaved(- step2Data,- NUMBER_OF_CHANNELS,- toneFrequency,- );-- return step3Data;-};--const fixFloatingPoint = (value: number) => {- if (value % 1 < 0.0000001) {- return Math.floor(value);- }-- if (value % 1 > 0.9999999) {- return Math.ceil(value);- }-- return value;-};--export const resampleAudioData = ({- sourceChannels,- destination,- targetFrames,- chunkSize,-}: {- sourceChannels: Int16Array;- destination: Int16Array;- targetFrames: number;- chunkSize: number;-}) => {- const getSourceValues = (- startUnfixed: number,- endUnfixed: number,- channelIndex: number,- ) => {- const start = fixFloatingPoint(startUnfixed);- const end = fixFloatingPoint(endUnfixed);- const startFloor = Math.floor(start);- const startCeil = Math.ceil(start);- const startFraction = start - startFloor;- const endFraction = end - Math.floor(end);- const endFloor = Math.floor(end);-- let weightedSum = 0;- let totalWeight = 0;-- // Handle first fractional sample- if (startFraction > 0) {- const firstSample =- sourceChannels[startFloor * NUMBER_OF_CHANNELS + channelIndex];- weightedSum += firstSample * (1 - startFraction);- totalWeight += 1 - startFraction;- }-- // Handle full samples- for (let k = startCeil; k < endFloor; k++) {- const num = sourceChannels[k * NUMBER_OF_CHANNELS + channelIndex];- weightedSum += num;- totalWeight += 1;- }-- // Handle last fractional sample- if (endFraction > 0) {- const lastSample =- sourceChannels[endFloor * NUMBER_OF_CHANNELS + channelIndex];- weightedSum += lastSample * endFraction;- totalWeight += endFraction;- }-- const average = weightedSum / totalWeight;-- return average;- };-- for (let newFrameIndex = 0; newFrameIndex < targetFrames; newFrameIndex++) {- const start = newFrameIndex * chunkSize;- const end = start + chunkSize;-- for (let i = 0; i < NUMBER_OF_CHANNELS; i++) {- destination[newFrameIndex * NUMBER_OF_CHANNELS + i] = getSourceValues(- start,- end,- i,- );- }- }-};diff --git a/packages/renderer/src/assets/inline-audio-mixing.ts b/packages/renderer/src/assets/inline-audio-mixing.tsindex 88e81e47b7..397660029c 100644--- a/packages/renderer/src/assets/inline-audio-mixing.ts+++ b/packages/renderer/src/assets/inline-audio-mixing.ts@@ -2,8 +2,10 @@ import fs, {writeSync} from 'node:fs';import path from 'node:path';import type {InlineAudioAsset} from 'remotion/no-react';import {deleteDirectory} from '../delete-directory';+import type {LogLevel} from '../log-level';+import type {CancelSignal} from '../make-cancel-signal';import {DEFAULT_SAMPLE_RATE} from '../sample-rate';-import {processWavFileWithWSOLA} from './change-tempo';+import {applyToneFrequencyUsingFfmpeg} from './apply-tone-frequency';import {makeAndReturn} from './download-map';const numberTo32BiIntLittleEndian = (num: number) => {@@ -134,11 +136,31 @@ export const makeInlineAudioMixing = (dir: string) => {); // Remaining size};- const finish = async () => {+ const finish = async ({+ binariesDirectory,+ indent,+ logLevel,+ cancelSignal,+ }: {+ indent: boolean;+ logLevel: LogLevel;+ binariesDirectory: string | null;+ cancelSignal: CancelSignal | undefined;+ }) => {for (const fd of Object.keys(openFiles)) {const frequency = toneFrequencies[fd];if (frequency !== 1) {- await processWavFileWithWSOLA(fd, toneFrequencies[fd]);+ const tmpFile = fd.replace(/.wav$/, '-tmp.wav');+ await applyToneFrequencyUsingFfmpeg({+ input: fd,+ output: tmpFile,+ toneFrequency: frequency,+ indent,+ logLevel,+ binariesDirectory,+ cancelSignal,+ });+ fs.renameSync(tmpFile, fd);}}};diff --git a/packages/renderer/src/create-audio.ts b/packages/renderer/src/create-audio.tsindex 06dd9f21a8..087eea11df 100644--- a/packages/renderer/src/create-audio.ts+++ b/packages/renderer/src/create-audio.ts@@ -123,7 +123,12 @@ export const createAudio = async ({}),);- await downloadMap.inlineAudioMixing.finish();+ await downloadMap.inlineAudioMixing.finish({+ indent,+ logLevel,+ binariesDirectory,+ cancelSignal,+ });const inlinedAudio = downloadMap.inlineAudioMixing.getListOfAssets();