shiveshnavin commited on
Commit
4f3aa5c
·
1 Parent(s): 4edf38e

Both iamge and text with sfx working

Browse files
Files changed (2) hide show
  1. common-utils +1 -1
  2. utils/bubble/Bubble.js +45 -61
common-utils CHANGED
@@ -1 +1 @@
1
- Subproject commit d4042809c56e544a633fe81717b5f4b06ee1662d
 
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 os from 'os';
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
- * Build a mixed audio file: takes the original audio from videoPath and
70
- * overlays all SFX from bubbles at their respective timestamps.
71
- * Uses a single ffmpeg filter_complex so there is no sequential re-mixing
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(b => b.audioEffectFile && resolveAudioPath(b.audioEffectFile));
 
 
 
85
  if (!sfxBubbles.length) {
86
- // no SFX — just extract original audio using appropriate codec
87
- const ac = outputAudioPath.toLowerCase().endsWith('.mp3') ? 'libmp3lame' : 'aac';
88
- await FFMpegUtils.execute(`ffmpeg -i "${videoPath}" -vn -c:a ${ac} "${outputAudioPath}" -y`);
 
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 filterParts = [];
100
- const mixLabels = ['[0:a]'];
101
 
102
  sfxBubbles.forEach((b, i) => {
103
- const audioPath = resolveAudioPath(b.audioEffectFile);
104
- inputs.push(`-i "${audioPath}"`);
105
- const sfxIdx = i + 1;
106
- const from = typeof b.fromSec === 'number' ? b.fromSec : 0;
107
- const to = typeof b.toSec === 'number' ? b.toSec : (from + (b.durationSec || 3));
108
- const clipSec = typeof b.audioEffectDurationSec === 'number' ? b.audioEffectDurationSec : Math.min(1, to - from);
109
- const aVol = typeof b.audioEffectVolume === 'number' ? b.audioEffectVolume : 1.5;
110
- const delayMs = Math.round(from * 1000);
111
- const label = `sfx${sfxIdx}`;
112
- filterParts.push(`[${sfxIdx}:a]atrim=0:${clipSec},asetpts=PTS-STARTPTS,volume=${aVol},adelay=${delayMs}|${delayMs}[${label}]`);
113
- mixLabels.push(`[${label}]`);
114
  });
115
 
116
- // Instead of feeding all streams into a single `amix` (which averages the
117
- // input samples when more than two streams are present), build a sequential
118
- // 2-input mixing chain. this keeps the original audio level constant no
119
- // matter how many effects have not yet started or have already finished.
120
- // each step uses `normalize=0`, which is safe because amix only has two
121
- // inputs in each invocation. this also sidesteps the old-
122
- // ffmpeg-throws-errors bug.
123
- let prevLabel = mixLabels[0];
124
- for (let idx = 1; idx < mixLabels.length; idx++) {
125
- const curLabel = mixLabels[idx];
126
- const outLabel = idx === mixLabels.length - 1 ? '[aout]' : `[mix${idx}]`;
127
- // inputs=2 because we're only mixing a running mix with a single new sfx
128
- filterParts.push(`${prevLabel}${curLabel}amix=inputs=2:duration=first:dropout_transition=0:normalize=0${outLabel}`);
129
- prevLabel = outLabel;
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()