linux – Concatenate audio parts of different videos with ffmpeg

So I have two videos:

  • Vid_eng.mkv is a high resolution video that has the audio in english.
  • Vid_esp.mkv is a low resolution video that has the audio in spanish.

Both videos are showing more or less the same stuff but the difference is that the spanish one has the intro and outro cutted out (the first 66 seconds are missing).

I want to create a new video file, Vid_out.mkv that has the Vid_eng.mkv video and both audios (so the final file has multistream audio). The issue is that the spanish audio will not be in sync with the high resolution video, so what I did was this:

ffmpeg -i Vid_eng.mkv
       -f lavfi -t 66 -i anullsrc -i Vid_esp.mkv
       -filter_complex "[1:a][2:a]concat=n=2:v=0:a=1[outa]"
       -map 0 -map "[outa]"
       -metadata:s:a:0 language=eng
       -metadata:s:a:1 language=spa
       -disposition:a:1 default
       -c:v copy Vid_out.mkv

What I’m doing here is essentially creating the new Vid_out.mkv using the video from Vid_eng.mkv, the audio multistream from both Vid_eng.mkv and Vid_esp.mkv, but with the twist that the audio from Vid_esp.mkv is actually begining 66 seconds later (so that it is synced with the english video). I did this by adding 66 seconds of silence (with -f lavfi -t 66 -i anullsrc) and concatenating that silence with the spanish audio (with -filter_complex "[1:a][2:a]concat=n=2:v=0:a=1[outa]") before adding this concatenated audio stream to the final file as the secondary audio. Also I included some metadata to know the which audio is which.

Everything good for the moment. But now I wanted to try something more sophisticated: instead of having 66 seconds of silence concatenated to the spanish audio I want 66 seconds of the english audio (the audio from Vid_eng.mkv), then the audio from Vid_esp.mkv, and finally when the spanish audio has finished and the video is still going, I want the english audio again. So, instead of silence at the begining and end of the video when played with the secondary audio stream, what I want is the english audio to play. How can I do that with ffmpeg?

Read more here: Source link