Spaces:
Running
Running
Commit ·
4f3aa5c
1
Parent(s): 4edf38e
Both iamge and text with sfx working
Browse filesFiles changed (2)
- common-utils+1-1
- utils/bubble/Bubble.js+45-61
1Subproject commit d4042809c56e544a633fe81717b5f4b06ee1662d1Subproject commit f3193e64265d3e62a078f03e22b136a049c0b1d6
12345678910111252 unmodified lines6566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142467 unmodified lines610611612613import fs from 'fs';import path from 'path';import _ from 'lodash';import { fileURLToPath } from 'url';import { FFMpegUtils } from 'common-utils';import os from 'os';import sharp from 'sharp';import crypto from 'crypto';import { tempPath, escapeXml, escapeText, resolveAudioPath, ensureFontFile } from './helpers.js';import { processImageWithBg } from './bg-utils.js';import { createAssSubtitle } from './ass-utils.js';import { computeXY } from './layout.js';52 unmodified linesclass BubbleMaker {constructor() { }/*** Build a mixed audio file: takes the original audio from videoPath and* overlays all SFX from bubbles at their respective timestamps.* Uses a single ffmpeg filter_complex so there is no sequential re-mixing* that would cause volume dips.** @param {string} videoPath - source video (must have audio)* @param {Object[]} bubbles - array of bubble configs (may include audioEffectFile)* @param {string} outputAudioPath - destination .aac path* @param {Function} onLog* @returns {Promise<boolean>} true if audio was produced, false if no original audio*/async buildMixedAudio(videoPath, bubbles, outputAudioPath, onLog) {const meta = await FFMpegUtils.getMediaMetadata(videoPath);if (!meta || !meta.audio) return false;const sfxBubbles = bubbles.filter(b => b.audioEffectFile && resolveAudioPath(b.audioEffectFile));if (!sfxBubbles.length) {// no SFX — just extract original audio using appropriate codecconst ac = outputAudioPath.toLowerCase().endsWith('.mp3') ? 'libmp3lame' : 'aac';await FFMpegUtils.execute(`ffmpeg -i "${videoPath}" -vn -c:a ${ac} "${outputAudioPath}" -y`);return true;}// Build a single filter_complex:// [0:a] = original audio (input 0 is the source video)// [1:a],[2:a],... = SFX files (inputs 1..N)// Each SFX is trimmed to its clip duration, delayed to its start time,// then everything is mixed with amix normalize=0 so the original level// is never touched.const inputs = [`-i "${videoPath}"`];const filterParts = [];const mixLabels = ['[0:a]'];sfxBubbles.forEach((b, i) => {const audioPath = resolveAudioPath(b.audioEffectFile);inputs.push(`-i "${audioPath}"`);const sfxIdx = i + 1;const from = typeof b.fromSec === 'number' ? b.fromSec : 0;const to = typeof b.toSec === 'number' ? b.toSec : (from + (b.durationSec || 3));const clipSec = typeof b.audioEffectDurationSec === 'number' ? b.audioEffectDurationSec : Math.min(1, to - from);const aVol = typeof b.audioEffectVolume === 'number' ? b.audioEffectVolume : 1.5;const delayMs = Math.round(from * 1000);const label = `sfx${sfxIdx}`;filterParts.push(`[${sfxIdx}:a]atrim=0:${clipSec},asetpts=PTS-STARTPTS,volume=${aVol},adelay=${delayMs}|${delayMs}[${label}]`);mixLabels.push(`[${label}]`);});// Instead of feeding all streams into a single `amix` (which averages the// input samples when more than two streams are present), build a sequential// 2-input mixing chain. this keeps the original audio level constant no// matter how many effects have not yet started or have already finished.// each step uses `normalize=0`, which is safe because amix only has two// inputs in each invocation. this also sidesteps the old-// ffmpeg-throws-errors bug.let prevLabel = mixLabels[0];for (let idx = 1; idx < mixLabels.length; idx++) {const curLabel = mixLabels[idx];const outLabel = idx === mixLabels.length - 1 ? '[aout]' : `[mix${idx}]`;// inputs=2 because we're only mixing a running mix with a single new sfxfilterParts.push(`${prevLabel}${curLabel}amix=inputs=2:duration=first:dropout_transition=0:normalize=0${outLabel}`);prevLabel = outLabel;}const ac = outputAudioPath.toLowerCase().endsWith('.mp3') ? 'libmp3lame' : 'aac';const fc = filterParts.join(';');const cmd = `ffmpeg ${inputs.join(' ')} -filter_complex "${fc}" -map "[aout]" -c:a ${ac} "${outputAudioPath}" -y`;onLog && onLog('buildMixedAudio: ' + cmd);await FFMpegUtils.execute(cmd);return true;}/*** Create an animated bubble (image or text) over a video.* The second argument may also be an array of bubble objects; in that case467 unmodified lines}}test()1234567852 unmodified lines616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126467 unmodified lines594595596597import fs from 'fs';import path from 'path';import _ from 'lodash';import { FFMpegUtils } from 'common-utils';import { tempPath, resolveAudioPath, ensureFontFile } from './helpers.js';import { processImageWithBg } from './bg-utils.js';import { createAssSubtitle } from './ass-utils.js';import { computeXY } from './layout.js';52 unmodified linesclass BubbleMaker {constructor() { }async buildMixedAudio(videoPath, bubbles, outputAudioPath, onLog) {const meta = await FFMpegUtils.getMediaMetadata(videoPath);if (!meta || !meta.audio) return false;const sfxBubbles = bubbles.filter(b => b.audioEffectFile && resolveAudioPath(b.audioEffectFile));if (!sfxBubbles.length) {const ac = outputAudioPath.toLowerCase().endsWith(".mp3") ? "libmp3lame" : "aac";await FFMpegUtils.execute(`ffmpeg -i "${videoPath}" -vn -c:a ${ac} "${outputAudioPath}" -y`);return true;}const inputs = [`-i "${videoPath}"`];const filters = [];const mixInputs = ["[0:a]"];sfxBubbles.forEach((b, i) => {console.log("bubble", i, JSON.stringify(b));const sfxPath = resolveAudioPath(b.audioEffectFile);const inputIdx = i + 1;const delayMs = Math.round((b.fromSec ?? 0) * 1000);const volume = b.audioEffectVolume ?? 1;const label = `[sfx${i}]`;inputs.push(`-i "${sfxPath}"`);filters.push(`[${inputIdx}:a]adelay=${delayMs}|${delayMs},volume=${volume}${label}`);mixInputs.push(label);});filters.push(`${mixInputs.join("")}amix=inputs=${mixInputs.length}:duration=first:dropout_transition=0:normalize=0[aout]`);const fc = filters.join(";");const ac = outputAudioPath.toLowerCase().endsWith(".mp3")? "libmp3lame": "aac";const cmd =`ffmpeg ${inputs.join(" ")} ` +`-filter_complex "${fc}" ` +`-map "[aout]" -c:a ${ac} "${outputAudioPath}" -y`;if (onLog) onLog("buildMixedAudio: " + cmd);await FFMpegUtils.execute(cmd, onLog, undefined, null, 'ffmpeg');return true;}/*** Create an animated bubble (image or text) over a video.* The second argument may also be an array of bubble objects; in that case467 unmodified lines}}// test()
common-utils+1-1
utils/bubble/Bubble.js+45-61