Skip to content

fix(bgm): carry a short BGM on from past its intro with a crossfade instead of restarting it - #34

Merged
umiyosh merged 3 commits into
masterfrom
fix/bgm-loop
Oct 25, 2026
Merged

umiyosh merged 3 commits into
masterfrom
fix/bgm-loop

Conversation

@umiyosh

@umiyosh umiyosh commented Oct 25, 2026

Copy link
Copy Markdown
Owner

Summary

When the film is longer than the BGM track, the music now carries on as the same piece instead of restarting from its intro with a hard cut.

Closes #32. Part of #16.

Changes

  • scripts/audio.ts
    • mediaSeconds(file) reads a file's length from ffmpeg's Duration line (no decoding)
    • bgmLoop() builds the BGM inputs and filters. If the track is long enough, nothing changes. If it is short, the first pass stops before the last 6 s (the closing bars fade out), then the track returns to 30 s (a quarter in for short tracks), past the intro, with a 3 s crossfade, as many times as the film needs. A track under 18 s, too short to crossfade, still loops from its start
    • mix uses it; sound effect input numbers follow the BGM inputs
  • Each return reads the file again as its own input (-ss 30 -t … -i bgm). The first version split one input with asplit and crossfaded the branches: it worked with ffmpeg 8.1 but with the bundled ffmpeg 6.0 the music stopped at the first crossfade (silent BGM from 2:50 in a 194 s film). Leaving out the closing bars came from measuring the next version: crossfading the track's fade-out made the music thin for about 4 s

Test plan

  • make check: lint, format, typecheck, 107 tests with the coverage gate, quality gates
  • Unit: bgmLoop for a long-enough track, one return (178 s track, 194 s film), many returns (40 s, 150 s), a too-short track; mediaSeconds reads Duration and fails without it
  • With the bundled ffmpeg: a 20 s tone in a 40 s film plays to the end (the asplit version stops at 14 s, so this test catches it)
  • On the real MP4 of the 194 s film with a 178 s BGM: the BGM alone (final audio minus the same mix with BGM volume 0) stays at -27 to -29 dB to the end, against -41 to -48 dB (the quiet intro) after the restart in the old version; the crossfade is 3-5 dB softer for about 2 s
  • Listened: the join sounds natural
  • CI

@umiyosh
umiyosh merged commit 26e9541 into master Oct 25, 2026
1 check passed
@umiyosh
umiyosh deleted the fix/bgm-loop branch October 25, 2026 17:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bgm: a film longer than the Lyria track restarts the music with a hard cut

1 participant