1

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 600 to 1200 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. 600 to 1200 in timeline)

Thank you.

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

1 Answer 1

0

You can use amix for this.

ffmpeg \
-i video.mkv \
-i 0-new.wav \
-i 1200-new.wav \
-i 1800-new.wav \
-filter_complex "[1:a]adelay=0:all=1[a1];
                 [2:a]adelay=1200s:all=1[a2];
                 [3:a]adelay=1800s:all=1[a3];
                 [0:a]volume=0:enable='between(t,0,600)+gte(t,1200)'[a0];
                 [a0][a1][a2][a3]amix=inputs=4:duration=first:dropout_transition=0:normalize=0[a]" \
-map 0:v -map "[a]" -c:v copy -c:a aac test.mkv

The volume filter mutes the original audio from 0 to 600s and after 1200s. To mute throughout, change enable value to simply 1.

1
  • +1. Cannot upvote yet. Thank you.
    – kal
    Commented Jul 26 at 16:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.