Search code examples
audioffmpeg

Can't use -shortest parameter when using multiple audio streams ffmpeg


I want to add a second audio stream to an mp4 video file already containing sound.

The second audio stream is a little longer than the video, but I want the final product to be the same length.

I tried using the -shortest feature but the second audio stream I wanted to add want not audible at all.

I think -shortest only allows for one stream, so what can I do to keep the video the same length and keep both audio streams?

Here is the full command I used before asking this question:

ffmpeg -i input.mp4 -i input.m4a -map 0:v -map 0:a -shortest output.mp4

Output of ffmpeg -i output.mp4:

Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'output.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.45.100
  Duration: 00:00:32.08, start: 0.000000, bitrate: 1248 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 480x600 [SAR 1:1 DAR 4:5], 1113 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s (default)
    Metadata:
      handler_name    : SoundHandler

Solution

  • You have to map the audio from the 2nd input as well.

    ffmpeg -i input.mp4 -i input.m4a -map 0:v -map 0:a -map 1:a -shortest -fflags +shortest -max_interleave_delta 100M output.mp4
    

    See https://stackoverflow.com/a/55804507/ for explanation of how to make shortest effective.