shiveshnavin commited on
Commit
a997fd4
·
1 Parent(s): ff7460a

Bubbles workign but sound issues:

Browse files
common-utils CHANGED
@@ -1 +1 @@
1
- Subproject commit f14b0fb9ba0096171cc049cf6606c114c6ae3951
 
1
+ Subproject commit d4042809c56e544a633fe81717b5f4b06ee1662d
tmp_packets.txt ADDED
Binary file (8.21 kB). View file
 
tmp_packets2.txt ADDED
Binary file (8.15 kB). View file
 
tmp_packets3.txt ADDED
Binary file (8.15 kB). View file
 
tmp_packets4.txt ADDED
Binary file (50.6 kB). View file
 
utils/bubble/Bubble.js CHANGED
@@ -94,6 +94,14 @@ class BubbleMaker {
94
  workingVideo = videoPath;
95
  }
96
  }
 
 
 
 
 
 
 
 
97
  // helper that computes input flag, video filter snippet and audio filter
98
  // snippet for a single bubble. It largely mirrors the logic used later
99
  // when executing ffmpeg, but stops before constructing the command.
@@ -228,10 +236,13 @@ class BubbleMaker {
228
  let current = workingVideo;
229
  for (let idx = 0; idx < bubble.length; idx++) {
230
  const tmp = tempPath('bubble-step', 'mp4');
 
231
  // attempt normal rendering
232
  try {
233
  await this.makeBubble(current, bubble[idx], tmp, onLog, false);
 
234
  } catch (err) {
 
235
  // if it failed and there is an audio effect we can retry without it
236
  if (bubble[idx] && bubble[idx].audioEffectFile) {
237
  onLog && onLog('bubble render failed, retrying without audio effect: ' + err);
@@ -259,6 +270,23 @@ class BubbleMaker {
259
  current = tmp;
260
  }
261
  fs.copyFileSync(current, outputPath);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
262
  return outputPath;
263
  }
264
 
@@ -300,14 +328,14 @@ class BubbleMaker {
300
 
301
  const videoFilter = filterParts.length ? filterParts.join(';') : '';
302
  // build audio filter: keep main audio (if present) and mix any effect streams.
303
- const workingHasAudio = !(detachAudio && originalHasAudio);
304
  let audioFilter = workingHasAudio ? '[0:a]aresample=async=1[a0]' : '';
305
  if (audioParts.length) {
306
  if (workingHasAudio) {
307
  audioFilter += ';' + audioParts.join(';');
308
  const mixInputs = 1 + effectLabels.length;
309
  audioFilter += ';[a0]' + effectLabels.map(l => `[${l}]`).join('') +
310
- `amix=inputs=${mixInputs}:duration=first:dropout_transition=0`;
311
  if (mixInputs > 1) {
312
  audioFilter += `,volume=${mixInputs}`;
313
  }
@@ -318,7 +346,7 @@ class BubbleMaker {
318
  const mixInputs = effectLabels.length;
319
  if (mixInputs > 1) {
320
  audioFilter += ';' + effectLabels.map(l => `[${l}]`).join('') +
321
- `amix=inputs=${mixInputs}:duration=first:dropout_transition=0,volume=${mixInputs}[aout]`;
322
  } else if (mixInputs === 1) {
323
  // single effect stream is already trimmed/delayed; alias to aout
324
  audioFilter += `;${effectLabels[0]}anull[aout]`;
@@ -333,7 +361,7 @@ class BubbleMaker {
333
  }
334
  const fc = [videoFilter, audioFilter].filter(Boolean).join(';');
335
 
336
- const cmd = `ffmpeg ${inputs.join(' ')} -filter_complex "${fc}" -map "${currentLabel === '[0:v]' ? '[0:v]' : currentLabel}" ${audioFilter ? '-map "[aout]" -c:a aac' : ''} -c:v libx264 -preset veryfast -crf 23 -shortest "${outputPath}" -y`;
337
  onLog && onLog('Running ffmpeg: ' + cmd);
338
  try {
339
  await FFMpegUtils.execute(cmd);
@@ -341,7 +369,7 @@ class BubbleMaker {
341
  // if audio mixing caused failure, try again without any audio effects
342
  if (audioParts.length) {
343
  onLog && onLog('ffmpeg optimized mix failed, retrying without audio effects: ' + err);
344
- const retryCmd = `ffmpeg ${inputs.join(' ')} -filter_complex "${videoFilter}" -map "${currentLabel === '[0:v]' ? '[0:v]' : currentLabel}" -c:v libx264 -preset veryfast -crf 23 -shortest "${outputPath}" -y`;
345
  onLog && onLog('Running ffmpeg (no audio): ' + retryCmd);
346
  await FFMpegUtils.execute(retryCmd);
347
  } else {
@@ -353,7 +381,16 @@ class BubbleMaker {
353
  const finalWithAudio = tempPath('withaudio', 'mp4');
354
  onLog && onLog(`Re‑attaching audio from ${videoPath} to ${outputPath}`);
355
  await FFMpegUtils.execute(`ffmpeg -i "${outputPath}" -i "${videoPath}" -c copy -map 0:v -map 1:a "${finalWithAudio}"`);
356
- fs.renameSync(finalWithAudio, outputPath);
 
 
 
 
 
 
 
 
 
357
  }
358
  return outputPath;
359
  }
@@ -567,6 +604,7 @@ class BubbleMaker {
567
  let audioFilter = '';
568
  let audioMap = '-map "[aout]" -c:a aac';
569
  let extraAudioInput = '';
 
570
  if (bubble.audioEffectFile) {
571
  let audioPath = resolveAudioPath(bubble.audioEffectFile);
572
  if (!audioPath) {
@@ -580,16 +618,21 @@ class BubbleMaker {
580
  // overlay is input 1, so effect audio will be input 2
581
  const audioInputIndex = 2;
582
  const clipSec = Math.min((typeof bubble.audioEffectDurationSec === 'number' ? bubble.audioEffectDurationSec : (to - from)), (to - from));
583
- audioFilter = `[0:a]aresample=async=1[a0];[${audioInputIndex}:a]atrim=0:${clipSec},asetpts=PTS-STARTPTS,volume=${aVol}[aeff];[aeff]adelay=${delayMs}|${delayMs}[aeffd];[a0][aeffd]amix=inputs=2:duration=first:dropout_transition=0`; // will boost below
584
- // compensate for division by inputs (2)
585
- audioFilter += ",volume=2[aout]";
 
 
 
 
 
586
  } else {
587
- // fallback to mapping main audio
588
- audioFilter = `[0:a]aresample=async=1[aout]`;
589
  }
590
  } else {
591
  // map main audio through as aout
592
- audioFilter = `[0:a]aresample=async=1[aout]`;
593
  }
594
 
595
  const fc = `${videoFilter};${audioFilter}`;
@@ -597,7 +640,7 @@ class BubbleMaker {
597
  // Build final command
598
  // place extraAudioInput after overlayInputFlag
599
  const inputs = `${overlayInputFlag} ${extraAudioInput}`.trim();
600
- cmd = `ffmpeg -i "${workingVideo}" ${inputs} -filter_complex "${fc}" -map "[vout]" ${audioMap} -c:v libx264 -preset veryfast -crf 23 -shortest "${outputPath}" -y`;
601
 
602
  } else if (bubble.bubbleText && bubble.bubbleText.text) {
603
  const t = bubble.bubbleText;
@@ -683,28 +726,54 @@ class BubbleMaker {
683
  const delayMs = Math.round(from * 1000);
684
  const audioInputIndex = 1;
685
  const clipSec = Math.min((typeof bubble.audioEffectDurationSec === 'number' ? bubble.audioEffectDurationSec : (to - from)), (to - from));
686
- const fc = `${videoFilter};[0:a]aresample=async=1[a0];[${audioInputIndex}:a]atrim=0:${clipSec},asetpts=PTS-STARTPTS,volume=${aVol}[aeff];[aeff]adelay=${delayMs}|${delayMs}[aeffd];[a0][aeffd]amix=inputs=2:duration=first:dropout_transition=0,volume=2[aout]`;
 
 
 
 
 
 
 
687
  if (bgPath) {
688
- cmd = `ffmpeg -i "${workingVideo}" -i "${bgPath}" -i "${audioPath}" -filter_complex "${fc}" -map "[vout]" -map "[aout]" -c:v libx264 -preset veryfast -crf 23 -c:a aac -shortest "${outputPath}" -y`;
689
  } else {
690
- cmd = `ffmpeg -i "${workingVideo}" -i "${audioPath}" -filter_complex "${fc}" -map "[vout]" -map "[aout]" -c:v libx264 -preset veryfast -crf 23 -c:a aac -shortest "${outputPath}" -y`;
691
  }
692
  } else {
693
- // fallback
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
694
  const fc = `${videoFilter};[0:a]aresample=async=1[aout]`;
695
  if (bgPath) {
696
  cmd = `ffmpeg -i "${videoPath}" -i "${bgPath}" -filter_complex "${fc}" -map "[vout]" -map "[aout]" -c:v libx264 -preset veryfast -crf 23 -c:a aac -b:a 128k -shortest "${outputPath}" -y`;
697
  } else {
698
  cmd = `ffmpeg -i "${videoPath}" -filter_complex "${fc}" -map "[vout]" -map "[aout]" -c:v libx264 -preset veryfast -crf 23 -c:a aac -b:a 128k -shortest "${outputPath}" -y`;
699
  }
700
- }
701
- } else {
702
- // no audio mixing; if bg present include it as input
703
- const fc = `${videoFilter};[0:a]aresample=async=1[aout]`;
704
- if (bgPath) {
705
- cmd = `ffmpeg -i "${videoPath}" -i "${bgPath}" -filter_complex "${fc}" -map "[vout]" -map "[aout]" -c:v libx264 -preset veryfast -crf 23 -c:a aac -b:a 128k -shortest "${outputPath}" -y`;
706
  } else {
707
- cmd = `ffmpeg -i "${videoPath}" -filter_complex "${fc}" -map "[vout]" -map "[aout]" -c:v libx264 -preset veryfast -crf 23 -c:a aac -b:a 128k -shortest "${outputPath}" -y`;
 
 
 
 
 
708
  }
709
  }
710
  }
 
94
  workingVideo = videoPath;
95
  }
96
  }
97
+ // figure out whether the current working video actually contains an audio stream
98
+ let workingHasAudio = false;
99
+ try {
100
+ const m2 = await FFMpegUtils.getMediaMetadata(workingVideo);
101
+ workingHasAudio = !!(m2 && m2.audio);
102
+ } catch (e) {
103
+ // if ffprobe fails, assume no audio
104
+ }
105
  // helper that computes input flag, video filter snippet and audio filter
106
  // snippet for a single bubble. It largely mirrors the logic used later
107
  // when executing ffmpeg, but stops before constructing the command.
 
236
  let current = workingVideo;
237
  for (let idx = 0; idx < bubble.length; idx++) {
238
  const tmp = tempPath('bubble-step', 'mp4');
239
+ onLog && onLog(`sequential loop idx=${idx}, source=${current}, tmp=${tmp}`);
240
  // attempt normal rendering
241
  try {
242
  await this.makeBubble(current, bubble[idx], tmp, onLog, false);
243
+ onLog && onLog(`render finished idx=${idx}, exists=${fs.existsSync(tmp)}`);
244
  } catch (err) {
245
+ onLog && onLog(`render error idx=${idx}: ${err}`);
246
  // if it failed and there is an audio effect we can retry without it
247
  if (bubble[idx] && bubble[idx].audioEffectFile) {
248
  onLog && onLog('bubble render failed, retrying without audio effect: ' + err);
 
270
  current = tmp;
271
  }
272
  fs.copyFileSync(current, outputPath);
273
+ // if we detached audio at the very top, put it back now
274
+ if (detachAudio && originalHasAudio && workingVideo !== videoPath) {
275
+ const finalWithAudio = tempPath('withaudio', 'mp4');
276
+ onLog && onLog(`Re‑attaching audio from ${videoPath} to ${outputPath}`);
277
+ await FFMpegUtils.execute(`ffmpeg -i "${outputPath}" -i "${videoPath}" -c copy -map 0:v -map 1:a "${finalWithAudio}"`);
278
+ // rename may fail across filesystems (EXDEV), so fall back to copy+unlink
279
+ try {
280
+ fs.renameSync(finalWithAudio, outputPath);
281
+ } catch (err) {
282
+ if (err && err.code === 'EXDEV') {
283
+ fs.copyFileSync(finalWithAudio, outputPath);
284
+ fs.unlinkSync(finalWithAudio);
285
+ } else {
286
+ throw err;
287
+ }
288
+ }
289
+ }
290
  return outputPath;
291
  }
292
 
 
328
 
329
  const videoFilter = filterParts.length ? filterParts.join(';') : '';
330
  // build audio filter: keep main audio (if present) and mix any effect streams.
331
+ // `workingHasAudio` was determined above after probing metadata.
332
  let audioFilter = workingHasAudio ? '[0:a]aresample=async=1[a0]' : '';
333
  if (audioParts.length) {
334
  if (workingHasAudio) {
335
  audioFilter += ';' + audioParts.join(';');
336
  const mixInputs = 1 + effectLabels.length;
337
  audioFilter += ';[a0]' + effectLabels.map(l => `[${l}]`).join('') +
338
+ `amix=inputs=${mixInputs}:duration=longest:dropout_transition=0`;
339
  if (mixInputs > 1) {
340
  audioFilter += `,volume=${mixInputs}`;
341
  }
 
346
  const mixInputs = effectLabels.length;
347
  if (mixInputs > 1) {
348
  audioFilter += ';' + effectLabels.map(l => `[${l}]`).join('') +
349
+ `amix=inputs=${mixInputs}:duration=longest:dropout_transition=0,volume=${mixInputs}[aout]`;
350
  } else if (mixInputs === 1) {
351
  // single effect stream is already trimmed/delayed; alias to aout
352
  audioFilter += `;${effectLabels[0]}anull[aout]`;
 
361
  }
362
  const fc = [videoFilter, audioFilter].filter(Boolean).join(';');
363
 
364
+ const cmd = `ffmpeg ${inputs.join(' ')} -filter_complex "${fc}" -map "${currentLabel === '[0:v]' ? '[0:v]' : currentLabel}" ${audioFilter ? '-map "[aout]" -c:a aac' : ''} -c:v libx264 -preset veryfast -crf 23 "${outputPath}" -y`;
365
  onLog && onLog('Running ffmpeg: ' + cmd);
366
  try {
367
  await FFMpegUtils.execute(cmd);
 
369
  // if audio mixing caused failure, try again without any audio effects
370
  if (audioParts.length) {
371
  onLog && onLog('ffmpeg optimized mix failed, retrying without audio effects: ' + err);
372
+ const retryCmd = `ffmpeg ${inputs.join(' ')} -filter_complex "${videoFilter}" -map "${currentLabel === '[0:v]' ? '[0:v]' : currentLabel}" -c:v libx264 -preset veryfast -crf 23 "${outputPath}" -y`;
373
  onLog && onLog('Running ffmpeg (no audio): ' + retryCmd);
374
  await FFMpegUtils.execute(retryCmd);
375
  } else {
 
381
  const finalWithAudio = tempPath('withaudio', 'mp4');
382
  onLog && onLog(`Re‑attaching audio from ${videoPath} to ${outputPath}`);
383
  await FFMpegUtils.execute(`ffmpeg -i "${outputPath}" -i "${videoPath}" -c copy -map 0:v -map 1:a "${finalWithAudio}"`);
384
+ try {
385
+ fs.renameSync(finalWithAudio, outputPath);
386
+ } catch (err) {
387
+ if (err && err.code === 'EXDEV') {
388
+ fs.copyFileSync(finalWithAudio, outputPath);
389
+ fs.unlinkSync(finalWithAudio);
390
+ } else {
391
+ throw err;
392
+ }
393
+ }
394
  }
395
  return outputPath;
396
  }
 
604
  let audioFilter = '';
605
  let audioMap = '-map "[aout]" -c:a aac';
606
  let extraAudioInput = '';
607
+ // workingHasAudio already computed above from metadata
608
  if (bubble.audioEffectFile) {
609
  let audioPath = resolveAudioPath(bubble.audioEffectFile);
610
  if (!audioPath) {
 
618
  // overlay is input 1, so effect audio will be input 2
619
  const audioInputIndex = 2;
620
  const clipSec = Math.min((typeof bubble.audioEffectDurationSec === 'number' ? bubble.audioEffectDurationSec : (to - from)), (to - from));
621
+ if (workingHasAudio) {
622
+ // mix the existing track with the effect stream without adjusting overall level
623
+ audioFilter = `[0:a]aresample=async=1[a0];[${audioInputIndex}:a]atrim=0:${clipSec},asetpts=PTS-STARTPTS,volume=${aVol}[aeff];[aeff]adelay=${delayMs}|${delayMs}[aeffd];[a0][aeffd]amix=inputs=2:duration=longest:dropout_transition=0[aout]`;
624
+ } else {
625
+ // only effect present
626
+ audioFilter = `[${audioInputIndex}:a]atrim=0:${clipSec},asetpts=PTS-STARTPTS,volume=${aVol}[aeff];[aeff]adelay=${delayMs}|${delayMs}[aeffd]`;
627
+ audioFilter += `;[aeffd]anull[aout]`;
628
+ }
629
  } else {
630
+ // fallback to mapping main audio (if any)
631
+ audioFilter = workingHasAudio ? `[0:a]aresample=async=1[aout]` : '';
632
  }
633
  } else {
634
  // map main audio through as aout
635
+ audioFilter = workingHasAudio ? `[0:a]aresample=async=1[aout]` : '';
636
  }
637
 
638
  const fc = `${videoFilter};${audioFilter}`;
 
640
  // Build final command
641
  // place extraAudioInput after overlayInputFlag
642
  const inputs = `${overlayInputFlag} ${extraAudioInput}`.trim();
643
+ cmd = `ffmpeg -i "${workingVideo}" ${inputs} -filter_complex "${fc}" -map "[vout]" ${audioMap} -c:v libx264 -preset veryfast -crf 23 "${outputPath}" -y`;
644
 
645
  } else if (bubble.bubbleText && bubble.bubbleText.text) {
646
  const t = bubble.bubbleText;
 
726
  const delayMs = Math.round(from * 1000);
727
  const audioInputIndex = 1;
728
  const clipSec = Math.min((typeof bubble.audioEffectDurationSec === 'number' ? bubble.audioEffectDurationSec : (to - from)), (to - from));
729
+ // build fc depending on whether the working video has audio
730
+ let fc;
731
+ if (workingHasAudio) {
732
+ fc = `${videoFilter};[0:a]aresample=async=1[a0];[${audioInputIndex}:a]atrim=0:${clipSec},asetpts=PTS-STARTPTS,volume=${aVol}[aeff];[aeff]adelay=${delayMs}|${delayMs}[aeffd];[a0][aeffd]amix=inputs=2:duration=longest:dropout_transition=0[aout]`;
733
+ } else {
734
+ // no main audio stream, just use effect and then pad with null to produce aout
735
+ fc = `${videoFilter};[${audioInputIndex}:a]atrim=0:${clipSec},asetpts=PTS-STARTPTS,volume=${aVol}[aeff];[aeff]adelay=${delayMs}|${delayMs}[aeffd];[aeffd]anull[aout]`;
736
+ }
737
  if (bgPath) {
738
+ cmd = `ffmpeg -i "${workingVideo}" -i "${bgPath}" -i "${audioPath}" -filter_complex "${fc}" -map "[vout]" -map "[aout]" -c:v libx264 -preset veryfast -crf 23 -c:a aac "${outputPath}" -y`;
739
  } else {
740
+ cmd = `ffmpeg -i "${workingVideo}" -i "${audioPath}" -filter_complex "${fc}" -map "[vout]" -map "[aout]" -c:v libx264 -preset veryfast -crf 23 -c:a aac "${outputPath}" -y`;
741
  }
742
  } else {
743
+ // fallback with only main audio (if it exists)
744
+ if (workingHasAudio) {
745
+ const fc = `${videoFilter};[0:a]aresample=async=1[aout]`;
746
+ if (bgPath) {
747
+ cmd = `ffmpeg -i "${videoPath}" -i "${bgPath}" -filter_complex "${fc}" -map "[vout]" -map "[aout]" -c:v libx264 -preset veryfast -crf 23 -c:a aac -b:a 128k "${outputPath}" -y`;
748
+ } else {
749
+ cmd = `ffmpeg -i "${videoPath}" -filter_complex "${fc}" -map "[vout]" -map "[aout]" -c:v libx264 -preset veryfast -crf 23 -c:a aac -b:a 128k "${outputPath}" -y`;
750
+ }
751
+ } else {
752
+ // no audio at all, just video
753
+ const fc = `${videoFilter}`;
754
+ if (bgPath) {
755
+ cmd = `ffmpeg -i "${videoPath}" -i "${bgPath}" -filter_complex "${fc}" -map "[vout]" -c:v libx264 -preset veryfast -crf 23 "${outputPath}" -y`;
756
+ } else {
757
+ cmd = `ffmpeg -i "${videoPath}" -filter_complex "${fc}" -map "[vout]" -c:v libx264 -preset veryfast -crf 23 "${outputPath}" -y`;
758
+ }
759
+ }
760
+ }
761
+ } else {
762
+ // no audio mixing; if bg present include it as input
763
+ if (workingHasAudio) {
764
  const fc = `${videoFilter};[0:a]aresample=async=1[aout]`;
765
  if (bgPath) {
766
  cmd = `ffmpeg -i "${videoPath}" -i "${bgPath}" -filter_complex "${fc}" -map "[vout]" -map "[aout]" -c:v libx264 -preset veryfast -crf 23 -c:a aac -b:a 128k -shortest "${outputPath}" -y`;
767
  } else {
768
  cmd = `ffmpeg -i "${videoPath}" -filter_complex "${fc}" -map "[vout]" -map "[aout]" -c:v libx264 -preset veryfast -crf 23 -c:a aac -b:a 128k -shortest "${outputPath}" -y`;
769
  }
 
 
 
 
 
 
770
  } else {
771
+ const fc = `${videoFilter}`;
772
+ if (bgPath) {
773
+ cmd = `ffmpeg -i "${videoPath}" -i "${bgPath}" -filter_complex "${fc}" -map "[vout]" -c:v libx264 -preset veryfast -crf 23 -shortest "${outputPath}" -y`;
774
+ } else {
775
+ cmd = `ffmpeg -i "${videoPath}" -filter_complex "${fc}" -map "[vout]" -c:v libx264 -preset veryfast -crf 23 -shortest "${outputPath}" -y`;
776
+ }
777
  }
778
  }
779
  }
utils/bubble/bubble-templates.js CHANGED
@@ -5,7 +5,7 @@ export const BaseBubbleTemplates = {
5
  // top-level bubble background controls whether a box is rendered
6
  backgroundColor: '#ffffff',
7
  borderRadius: 20,
8
- audioEffectFile: 'woosh',
9
  animExtra: {
10
  template: 'slide_down',
11
  durationSec: 0.1
 
5
  // top-level bubble background controls whether a box is rendered
6
  backgroundColor: '#ffffff',
7
  borderRadius: 20,
8
+ audioEffectFile: 'click',
9
  animExtra: {
10
  template: 'slide_down',
11
  durationSec: 0.1