Search code examples
audioffmpeg

Ffmpeg mixing audio only for n number of videos using offset with an altered duration


I want to mix audio stream of n amount of video files with ffmpeg, with certain parameters such as:

  • Duration: I want to specify how long each audio is going to play for each output. For example if I specify ffmpeg -i -d:5 first.mp4 -i second.mp4 it should play the audio from first.mp4 for 5 seconds (I don't know if -d:5 is a real tag it's just an example I've made ).
  • Starting point: I want to specify when a given file is going start on the output, I probably need to use -itsoffset 5 but I don't know if it's the right one in terms of interacting with other commands, in on itself it works fine. For example: ffmpeg -i -isoffset 5 first.mp -i second.mp4 causes the second.mp4 to start immediately, and first.mp4 to start after 5 seconds.
  • Segmentation: This is the tricky one, I want to specify at which point the input's audio should start. It's like the -ss flag but the problem is it's not working together with -itsoffset. For example when I say ffmpeg -i -ss 5 first.mp4 -i second.mp4 both files should start immediately on the output, but first.mp4 should start on it's 5 seconds. So the 5th second of first.mp4 is heard at the 1st second of the output.

This is what I'm trying to achieve, my problem is that I don't know how to implement 'duration' and -ss is not working together with -itsoffset.

At the end I should have something similar to this:

ffmpeg -y -d 5 -itsoffset 3.5 -i first.mp4 -d 10 -ss 10 -itsoffset 5.3 -i 3 -vn -copyts -async 1 -filter_complex amix=inputs=2 out.mp

Which should result in an audio that sounds like this: The first 3.5 seconds are empty, no audio is heard. Then first.mp4 is heard from it's beginning for 5 seconds. When the outputs timestamp reaches 5.3, the 10th seconds of second.mp4 is heard (while first.mp4 is still playing, it's supposed to play until 8.5, so I should hear both files at the same time.) for 10 seconds.

I can't find an example of this and some sources are out-of date.


Solution

  • Try something like this:

    ffmpeg -t  5 -ss  0 -i first.mp4 \
           -t 10 -ss 10 -i second.mp4 \
           -filter_complex \
             [0:a]adelay=3500:all=1[c1];\
             [1:a]adelay=5300:all=1[c2];\
             [c1][c2]amix=inputs=2[aout] \
           -map [aout] out.mp3