hyperframes icon indicating copy to clipboard operation
hyperframes copied to clipboard

Audio mix pads every clip to the full composition length: ~30–50 s for 190 short clips

Open bkdev98 opened this issue 1 month ago • 2 comments

Describe the bug

mixAudioTracks builds every branch as atrim,volume,afade,adelay,apad,asetpts=N/SR/TB,atrim=0:TOTAL and sums them with one amix. Every clip, including a 0.3 s sound effect, becomes a stream as long as the whole composition, so the mix costs (number of clips × composition length) in one single-threaded filter graph.

On a real composition (5 min 11 s, 190 <audio> elements: narration, two music beds, ~180 short sound effects) audio_process takes 53 s on every render. With 12 workers and --gpu the whole render is 4 min, so the audio step is about a fifth of it; on a 2 fps review render it is 40%.

Dropping the per-branch padding and summing with amix ... :normalize=0 gives the same samples 2.7–3.3× faster on FFmpeg 6.1, 7.0.2, 8.1 and master.

Minimal reproduction

Synthetic tones only. The script writes the graph audioMixer.ts writes today (current) and the same mix without per-branch padding (nopad), times both, and measures their difference.

#!/usr/bin/env bash
# Times the per-track graph audioMixer.ts writes (every branch padded to the full duration) against
# the same mix without per-branch padding (amix normalize=0, one pad on the mixed output), and
# compares the two outputs sample by sample. Synthetic tones only.
set -euo pipefail
FF="${FF:-ffmpeg}"; N="${N:-190}"; TOTAL="${TOTAL:-300}"
W=$(mktemp -d); trap 'rm -rf "$W"' EXIT; cd "$W"
$FF -v error -y -f lavfi -i "sine=frequency=110:duration=$TOTAL" -ar 48000 -ac 2 bed.wav
$FF -v error -y -f lavfi -i "sine=frequency=880:duration=0.3" -ar 48000 -ac 2 sfx.wav
# Track 0: a bed for the whole film. Tracks 1..N-1: 0.3 s effects spread over the timeline.
ins="-i bed.wav"; starts=(0); durs=($TOTAL); vols=(0.2)
for ((i=1;i<N;i++)); do ins+=" -i sfx.wav"; starts+=("$(awk -v i=$i -v n=$N -v t=$TOTAL 'BEGIN{printf "%.3f",(i*(t-1))/n}')"); durs+=(0.3); vols+=(0.5); done
graph(){ local mode=$1 g="" mix="" i ms tail
  for ((i=0;i<N;i++)); do
    ms=$(awk -v s=${starts[$i]} 'BEGIN{printf "%d",s*1000+0.5}')
    [ $mode = current ] && tail=",apad,asetpts=N/SR/TB,atrim=0:$TOTAL" || tail=""
    g+="[$i:a]atrim=0:${durs[$i]},volume=${vols[$i]},adelay=$ms|$ms$tail[a$i];"; mix+="[a$i]"
  done
  if [ $mode = current ]; then g+="${mix}amix=inputs=$N:duration=longest:dropout_transition=0[mixed];[mixed]volume=$N[out]"
  else g+="${mix}amix=inputs=$N:duration=longest:dropout_transition=0:normalize=0[mixed];[mixed]volume=1,apad,asetpts=N/SR/TB,atrim=0:$TOTAL[out]"; fi
  echo "$g" > $mode.txt; }
run(){ graph $1; local s=$(date +%s.%N)
  $FF -v error -y $ins -filter_complex_script $1.txt -map "[out]" -c:a pcm_f32le -t $TOTAL $1.wav 2>/dev/null ||
    $FF -v error -y $ins -/filter_complex $1.txt -map "[out]" -c:a pcm_f32le -t $TOTAL $1.wav
  printf "%-8s %6.1fs\n" $1 "$(echo "$(date +%s.%N) - $s" | bc)"; }
echo "$($FF -version | head -1 | cut -d' ' -f1-3), $N tracks, ${TOTAL}s"
run current; run nopad
$FF -v info -i current.wav -i nopad.wav -filter_complex "[1:a]volume=-1[n];[0:a][n]amix=inputs=2:normalize=0,astats" -f null - 2>&1 |
  awk '/Overall/{o=1} o&&/Peak level dB/{printf "difference current - nopad: peak %s dB", $NF} o&&/RMS level dB/{print ", RMS " $NF " dB"; exit}'

Steps to reproduce

  1. Save the script, chmod +x mixcost.sh
  2. ./mixcost.sh (or FF=/path/to/ffmpeg ./mixcost.sh; N and TOTAL set the clip count and length)

Expected behavior

The mix costs roughly the audio that is actually playing, not (clips × composition length).

Actual behavior

190 tracks, 300 s, Ubuntu 24.04, Xeon E5-2686 v4:

FFmpeg current nopad difference current − nopad
6.1.1 (Ubuntu) 30.5 s 11.3 s peak −162.6 dB
7.0.2 static 25.6 s 7.8 s peak −162.6 dB
8.1.3 27.0 s 8.3 s peak −162.6 dB
master N-127507 27.1 s 8.4 s peak −162.6 dB

On the real 190-clip composition: 52.0 s → 19.1 s on 6.1, 28.2 s → 10.9 s on 7.0.2, identical output (max sample difference 1.8e-7).

Proposal

  • No per-branch apad,asetpts,atrim: [i:a]atrim=0:D,volume=…,afade…,adelay=…[ai].
  • amix=inputs=N:duration=longest:dropout_transition=0:normalize=0 sums without re-weighting as inputs end, which is what the padding plus volume=N compensation achieves today; the master gain becomes masterOutputGain alone.
  • Pad once, on the mixed output, with the same portable shape: [mixed]volume=G,apad,asetpts=N/SR/TB,atrim=0:TOTAL[out] (#2769, #3344).
  • FFmpeg builds without amix normalize (before 4.4) keep today's graph, the way -filter_complex_script already falls back.

I extended the #3344 reproduction with this variant: on 6.1, 7.0.2, 8.1 and master it places every clip correctly, keeps t=0 silent and keeps full length, where the old apad,atrim shape fails on 7.0.2 and master.

One question before a PR: the test "compensates amix normalization so multi-track master gain equals track count" asserts the graph does not contain normalize=. Is that only about builds before 4.4, or is there another reason to avoid it? Happy to open the PR either way.

A further step, for later: summing the decoded tracks directly (Float32Array, each clip only where it plays) took 0.9 s for the same 190-clip mix and matched FFmpeg master to 1.8e-7, but it would have to reimplement fades, volume automation and the master FX chain outside FFmpeg.

Environment

hyperframes 0.8.145 (the mix graph is unchanged on main at 6f76b30), Linux (Ubuntu 24.04), Node 24.

bkdev98 avatar Oct 13 '26 17:10 bkdev98

Thanks for the benchmark. The grouped-audio sub-mix already uses amix ... normalize=0 with a fallback for builds that reject it (groupNormalizeOptionUnsupported in audioMixer.ts). The test assertions just pin today's main-mix shape. A PR that reuses that fallback and keeps the #3344 cases passing is welcome.

jrusso1020 avatar Oct 13 '26 20:10 jrusso1020

I can take this. I’ll reuse the existing normalize=0 fallback, remove per-clip padding only on the supported path, and check timing and sample output against the current graph before opening a PR.

CreedTech avatar Oct 13 '26 21:10 CreedTech