跳到正文
DEV Community· StemSplitter·· 35 分鐘前AI 評分54

Demucs 和 FFmpeg 如何將 60 分鐘錄音拆分成四條音軌

How I Split a 60-Minute Recording into Four Stems with Demucs and FFmpeg

AI 導讀

作者示範以 Demucs 和 FFmpeg 將 60 分鐘錄音分成四段處理,再合併為人聲、鼓、低音及其他四條音軌。相鄰片段重疊 4 秒,合併時以交叉淡化處理;staging 測試中,四條輸出均為 3600.000 秒,並以 ffprobe 核對時長。

正文

Split an hour of audio into four chunks and give each pair of neighboring chunks a four-second overlap. Add up the lengths of those chunks, and you get 60 minutes and 12 seconds. Those extra 12 seconds are intentional. They give FFmpeg something to work with when it joins the pieces back together.

I ran into this while building StemSplitter, a browser-based tool that separates mixed audio into vocals, drums, bass, and everything else. Here’s how I planned the chunks, joined the stems, and checked that all four exports lined up with the source duration.

Route by duration

The pipeline reads the audio duration with ffprobe. Files up to 15 minutes go through one Demucs prediction. Longer files go through FFmpeg first, so they can be split into smaller inputs for the same model.

Duration gives the job planner a consistent basis for routing. A WAV and an MP3 of the same recording can have very different file sizes while spanning the same amount of audio. The 15-minute cutoff is an application setting. In the long-file path, the overlap makes a full chunk up to 15 minutes and 4 seconds.

Before slicing, the media worker converts the first audio stream to stereo, 44.1 kHz FLAC. All chunks then come from the same normalized source.

Give each join four seconds of shared audio

For a 60-minute source, the chunk plan looks like this:

Chunk Start End Length
1 00:00 15:04 15:04
2 15:00 30:04 15:04
3 30:00 45:04 15:04
4 45:00 60:00 15:00

Each chunk goes through Demucs independently. To rebuild the drums track, I take the drums output from each chunk, sort the pieces by their original position, and crossfade them in that order. Vocals, bass, and the other stem follow the same process. Demucs model documentation

Here’s the FFmpeg operation for two adjacent pieces of the same stem:

ffmpeg -i drums-0.flac -i drums-1.flac \
  -filter_complex "[0:a][1:a]acrossfade=d=4:c1=tri:c2=tri[out]" \
  -map "[out]" drums-joined.flac

During the four seconds shared by both inputs, one fades down while the other fades up. For four chunks, the worker chains three of these joins. Each join consumes one overlap, so the timeline comes back to 3612 − 3 × 4 = 3600 seconds. FFmpeg acrossfade documentation

Check every exported stem

For the staging test, I checked the duration of each delivered file with ffprobe. The pass condition was that all four outputs had to be within one second of the source.

for file in vocals.mp3 drums.mp3 bass.mp3 other.mp3; do
  printf '%s ' "$file"
  ffprobe -v error -show_entries format=duration \
    -of default=noprint_wrappers=1:nokey=1 "$file"
done
Stem Duration in the staging run
Vocals 3600.000 s
Drums 3600.000 s
Bass 3600.000 s
Other 3600.000 s

This check catches timeline mistakes, such as accidentally keeping duplicated overlap or dropping a section. Listening answers a different question. A file can have the right duration and still have an audible change at a join. If I were comparing overlap lengths, I’d start with an exposed vocal or a sharp drum hit near a boundary, where a change is easier to hear. ffprobe documentation

The most useful part of this design was working out the output timeline on paper before wiring up inference. Once the chunk starts and overlap lengths were explicit, I knew what each exported file should measure and could check the final result with one command.

This article was drafted with AI assistance using the project’s implementation and staging notes.

來源:DEV Community · dev.to