ffmpeg amerge/amix with itsoffset into single audio stream

I have video.mkv with 1 video and 1 audio stream. 34 minutes long.

I have 3 audio files: 0-new.wav (10 mins), 1200-new.wav (10 mins), 1800-new.wav (4 mins)

I want to discard the video’s audio and replace it with 1 audio stream from combining the new audio files such that each audio file falls in the right segment on the timeline.

  1. 0-new.wav: 0 to 600 seconds
  2. 1200-new.wav: 1200 to 1800 seconds
  3. 1800-new.wav: 1800 to end of timeline

I tried:

ffmpeg -i video.mkv -itsoffset 0 -i 0-new.wav -itsoffset 1200 -i 1200-new.wav -itsoffset 1800 -i 1800-new.wav -map 0:0 -map 1:a -map 2:a -map 3:a -filter_complex "[1:a:0][2:a:0][3:a:0] amerge=inputs=3 [a]" -map "[a]" -c:a libmp3lame -c:v copy test.mkv

I end up with 1 video stream and 4 audio streams. The first 3 audio streams occupy the correct segment on the timeline, alas in separate streams (audio tracks)

The fourth audio track is the 3 combined and occupies the first ~ 5 minutes of the timeline (the rest is silent)

I used ‘amix’ with the same result. I am fine with gaps in audio in the result (above, there’d be no audio from 1200 to 1800 seconds)

How do I tell ffmpeg to collapse all the audio inputs into one stream output?

How do I tell it to drop the fourth stream?

As a bonus how do I tell it to keep the original video’s audio for segments that would not be overwritten (e.g. 1200 to 1800 in timeline)

Thank you.

(ffmpeg version 6.1-1build2~22.04 Copyright (c) 2000-2023 the FFmpeg developers)

Read more here: Source link