Answers.Body: <p>You can use amix for this.</p>
  <pre><code>ffmpeg \
  -i video.mkv \
  -i 0-new.wav \
  -i 1200-new.wav \
  -i 1800-new.wav \
  -filter_complex &quot;[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]&quot; \
  -map 0:v -map &quot;[a]&quot; -c:v copy -c:a aac test.mkv
  </code></pre>
  <p>The volume filter mutes the original audio from 0 to 600s and after 1200s. To mute throughout, change enable value to simply <code>1</code>.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-07-26T16:02:49.770Z
Answers.Text: +1. Cannot upvote yet. Thank you.
Answers.UserDisplayName: kal
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-07-26T04:26:53.950Z
Answers.LastActivityDate: 2024-07-26T04:26:53.950Z
Answers.UserDisplayName: Gyan
Body: <p>I have video.mkv with 1 video and 1 audio stream. 34 minutes long.</p>
  <p>I have 3 audio files: 0-new.wav (10 mins), 1200-new.wav (10 mins), 1800-new.wav (4 mins)</p>
  <p>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.</p>
  <ol>
  <li>0-new.wav: 0 to 600 seconds</li>
  <li>1200-new.wav: 1200 to 1800 seconds</li>
  <li>1800-new.wav: 1800 to end of timeline</li>
  </ol>
  <p>I tried:</p>
  <pre><code>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 &quot;[1:a:0][2:a:0][3:a:0] amerge=inputs=3 [a]&quot; \
      -map &quot;[a]&quot; -c:a libmp3lame -c:v copy test.mkv
  </code></pre>
  <p>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)</p>
  <p>The fourth audio track is the 3 combined and occupies the first ~ 5 minutes of the timeline (the rest is silent)</p>
  <p>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)</p>
  <p>How do I tell ffmpeg to collapse all the audio inputs into one stream output?</p>
  <p>How do I tell it to drop the fourth stream?</p>
  <p>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)</p>
  <p>Thank you.</p>
  <p>(ffmpeg version 6.1-1build2~22.04 Copyright (c) 2000-2023 the FFmpeg developers)</p>
  
Tags: <ffmpeg>
Title: ffmpeg amerge/amix with itsoffset into single audio stream
UserDisplayName: kal
ThreadText: # ffmpeg amerge/amix with itsoffset into single audio stream
  
  (Asked by: username_0 on 2024-07-25)
  
  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)
  
  **Tags:** ffmpeg
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2024-07-26. Score: 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`.
  
  > **Comments:**
  > **username_0**: +1. Cannot upvote yet. Thank you.  
  
  ---
  
  