Spaces:
Running
Running
Commit ·
4f3aa5c
1
Parent(s): 4edf38e
Both iamge and text with sfx working
Browse files- common-utils +1 -1
- utils/bubble/Bubble.js +45 -61
common-utils
CHANGED
|
@@ -1 +1 @@
|
|
| 1 |
-
Subproject commit
|
|
|
|
| 1 |
+
Subproject commit f3193e64265d3e62a078f03e22b136a049c0b1d6
|
utils/bubble/Bubble.js
CHANGED
|
@@ -1,12 +1,8 @@
|
|
| 1 |
import fs from 'fs';
|
| 2 |
import path from 'path';
|
| 3 |
import _ from 'lodash';
|
| 4 |
-
import { fileURLToPath } from 'url';
|
| 5 |
import { FFMpegUtils } from 'common-utils';
|
| 6 |
-
import
|
| 7 |
-
import sharp from 'sharp';
|
| 8 |
-
import crypto from 'crypto';
|
| 9 |
-
import { tempPath, escapeXml, escapeText, resolveAudioPath, ensureFontFile } from './helpers.js';
|
| 10 |
import { processImageWithBg } from './bg-utils.js';
|
| 11 |
import { createAssSubtitle } from './ass-utils.js';
|
| 12 |
import { computeXY } from './layout.js';
|
|
@@ -65,78 +61,66 @@ import { BaseBubbleTemplates } from './bubble-templates.js';
|
|
| 65 |
class BubbleMaker {
|
| 66 |
constructor() { }
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
* that would cause volume dips.
|
| 73 |
-
*
|
| 74 |
-
* @param {string} videoPath - source video (must have audio)
|
| 75 |
-
* @param {Object[]} bubbles - array of bubble configs (may include audioEffectFile)
|
| 76 |
-
* @param {string} outputAudioPath - destination .aac path
|
| 77 |
-
* @param {Function} onLog
|
| 78 |
-
* @returns {Promise<boolean>} true if audio was produced, false if no original audio
|
| 79 |
-
*/
|
| 80 |
async buildMixedAudio(videoPath, bubbles, outputAudioPath, onLog) {
|
| 81 |
const meta = await FFMpegUtils.getMediaMetadata(videoPath);
|
| 82 |
if (!meta || !meta.audio) return false;
|
| 83 |
|
| 84 |
-
const sfxBubbles = bubbles.filter(
|
|
|
|
|
|
|
|
|
|
| 85 |
if (!sfxBubbles.length) {
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
|
|
|
| 89 |
return true;
|
| 90 |
}
|
| 91 |
|
| 92 |
-
// Build a single filter_complex:
|
| 93 |
-
// [0:a] = original audio (input 0 is the source video)
|
| 94 |
-
// [1:a],[2:a],... = SFX files (inputs 1..N)
|
| 95 |
-
// Each SFX is trimmed to its clip duration, delayed to its start time,
|
| 96 |
-
// then everything is mixed with amix normalize=0 so the original level
|
| 97 |
-
// is never touched.
|
| 98 |
const inputs = [`-i "${videoPath}"`];
|
| 99 |
-
const
|
| 100 |
-
const
|
| 101 |
|
| 102 |
sfxBubbles.forEach((b, i) => {
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
const
|
| 106 |
-
const
|
| 107 |
-
const
|
| 108 |
-
const
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
mixLabels.push(`[${label}]`);
|
| 114 |
});
|
| 115 |
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
|
|
|
|
|
|
|
|
|
| 131 |
|
| 132 |
-
const ac = outputAudioPath.toLowerCase().endsWith('.mp3') ? 'libmp3lame' : 'aac';
|
| 133 |
-
const fc = filterParts.join(';');
|
| 134 |
-
const cmd = `ffmpeg ${inputs.join(' ')} -filter_complex "${fc}" -map "[aout]" -c:a ${ac} "${outputAudioPath}" -y`;
|
| 135 |
-
onLog && onLog('buildMixedAudio: ' + cmd);
|
| 136 |
-
await FFMpegUtils.execute(cmd);
|
| 137 |
return true;
|
| 138 |
}
|
| 139 |
|
|
|
|
| 140 |
/**
|
| 141 |
* Create an animated bubble (image or text) over a video.
|
| 142 |
* The second argument may also be an array of bubble objects; in that case
|
|
@@ -610,4 +594,4 @@ export async function test() {
|
|
| 610 |
}
|
| 611 |
}
|
| 612 |
|
| 613 |
-
test()
|
|
|
|
| 1 |
import fs from 'fs';
|
| 2 |
import path from 'path';
|
| 3 |
import _ from 'lodash';
|
|
|
|
| 4 |
import { FFMpegUtils } from 'common-utils';
|
| 5 |
+
import { tempPath, resolveAudioPath, ensureFontFile } from './helpers.js';
|
|
|
|
|
|
|
|
|
|
| 6 |
import { processImageWithBg } from './bg-utils.js';
|
| 7 |
import { createAssSubtitle } from './ass-utils.js';
|
| 8 |
import { computeXY } from './layout.js';
|
|
|
|
| 61 |
class BubbleMaker {
|
| 62 |
constructor() { }
|
| 63 |
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
async buildMixedAudio(videoPath, bubbles, outputAudioPath, onLog) {
|
| 69 |
const meta = await FFMpegUtils.getMediaMetadata(videoPath);
|
| 70 |
if (!meta || !meta.audio) return false;
|
| 71 |
|
| 72 |
+
const sfxBubbles = bubbles.filter(
|
| 73 |
+
b => b.audioEffectFile && resolveAudioPath(b.audioEffectFile)
|
| 74 |
+
);
|
| 75 |
+
|
| 76 |
if (!sfxBubbles.length) {
|
| 77 |
+
const ac = outputAudioPath.toLowerCase().endsWith(".mp3") ? "libmp3lame" : "aac";
|
| 78 |
+
await FFMpegUtils.execute(
|
| 79 |
+
`ffmpeg -i "${videoPath}" -vn -c:a ${ac} "${outputAudioPath}" -y`
|
| 80 |
+
);
|
| 81 |
return true;
|
| 82 |
}
|
| 83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
const inputs = [`-i "${videoPath}"`];
|
| 85 |
+
const filters = [];
|
| 86 |
+
const mixInputs = ["[0:a]"];
|
| 87 |
|
| 88 |
sfxBubbles.forEach((b, i) => {
|
| 89 |
+
console.log("bubble", i, JSON.stringify(b));
|
| 90 |
+
const sfxPath = resolveAudioPath(b.audioEffectFile);
|
| 91 |
+
const inputIdx = i + 1;
|
| 92 |
+
const delayMs = Math.round((b.fromSec ?? 0) * 1000);
|
| 93 |
+
const volume = b.audioEffectVolume ?? 1;
|
| 94 |
+
const label = `[sfx${i}]`;
|
| 95 |
+
|
| 96 |
+
inputs.push(`-i "${sfxPath}"`);
|
| 97 |
+
filters.push(`[${inputIdx}:a]adelay=${delayMs}|${delayMs},volume=${volume}${label}`);
|
| 98 |
+
mixInputs.push(label);
|
|
|
|
| 99 |
});
|
| 100 |
|
| 101 |
+
filters.push(
|
| 102 |
+
`${mixInputs.join("")}amix=inputs=${mixInputs.length}:duration=first:dropout_transition=0:normalize=0[aout]`
|
| 103 |
+
);
|
| 104 |
+
|
| 105 |
+
const fc = filters.join(";");
|
| 106 |
+
|
| 107 |
+
const ac = outputAudioPath.toLowerCase().endsWith(".mp3")
|
| 108 |
+
? "libmp3lame"
|
| 109 |
+
: "aac";
|
| 110 |
+
|
| 111 |
+
const cmd =
|
| 112 |
+
`ffmpeg ${inputs.join(" ")} ` +
|
| 113 |
+
`-filter_complex "${fc}" ` +
|
| 114 |
+
`-map "[aout]" -c:a ${ac} "${outputAudioPath}" -y`;
|
| 115 |
+
|
| 116 |
+
if (onLog) onLog("buildMixedAudio: " + cmd);
|
| 117 |
+
|
| 118 |
+
await FFMpegUtils.execute(cmd, onLog, undefined, null, 'ffmpeg');
|
| 119 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
return true;
|
| 121 |
}
|
| 122 |
|
| 123 |
+
|
| 124 |
/**
|
| 125 |
* Create an animated bubble (image or text) over a video.
|
| 126 |
* The second argument may also be an array of bubble objects; in that case
|
|
|
|
| 594 |
}
|
| 595 |
}
|
| 596 |
|
| 597 |
+
// test()
|