[FFmpeg-trac] #9512(undetermined:new): shortest works differently on transcode vs copy

FFmpeg trac at avcodec.org
Tue Nov 16 21:45:35 EET 2021

#9512: shortest works differently on transcode vs copy
             Reporter:  Cosmin       |                     Type:  defect
  Stejerean                          |
               Status:  new          |                 Priority:  normal
            Component:               |                  Version:
  undetermined                       |  unspecified
             Keywords:               |               Blocked By:
             Blocking:               |  Reproduced by developer:  0
Analyzed by developer:  0            |
 This may be related to the bug in #8489, or the bugs described in #9487
 and #3789, but the scenario is slightly differently so I'll explain the
 steps below.

 Recently we ran into a problem where taking an input video with a short
 audio track, and trying to mix in a longer audio, and then write out the
 mixed audio while copying video is not working with -shortest, whereas it
 does work correctly when transcoding.

 First let's create the test sequences.

 The steps below create a 10s video track with a 2s audio. There might be
 easier ways to pull this off in one command but it's irrelevant to the
 issue at hand.

 % ffmpeg -f lavfi -i testsrc2=size=vga -t 10 -y video.mp4
 % ffmpeg -f lavfi -i sine=1000 -t 2 -y audio.m4a
 ffmpeg -i video.mp4 -i audio.m4a -c copy -y muxed.mp4
 rm video.mp4 audio.m4a

 This creates a longer, let's say 30s track, pretend it represents some
 background music

 % ffmpeg -f lavfi -i sine=3000 -t 30 -y music.m4a

 We want to mix the background music over the original audio, and trim the
 overall audio to the length of the video. This should be possible to do
 with the -shortest option

 % ffmpeg -i music.m4a -i muxed.mp4 -filter_complex '[0:a:0] volume=1
 [user_audio]; [1:a:0] volume=1 [music]; [user_audio][music] amix=inputs=2
 [combined]' -map 1:v:0 -c:v libx264 -map '[combined]' -shortest -y

 And in fact this works. The mixed audio plays for 10s.

 However transcoding the video is inefficient (and hurts quality). Since
 we're only mixing audio this shouldn't be necessary. Attempting the same
 thing with -c:v copy

 % ffmpeg -i music.m4a -i muxed.mp4 -filter_complex '[0:a:0] volume=1
 [user_audio]; [1:a:0] volume=1 [music]; [user_audio][music] amix=inputs=2
 [combined]' -map 1:v:0 -map '[combined]' -c:v copy -shortest -y combined-

 At this point all the audio stops at 2s (the shortest of the audio inputs)
 rather than stopping at 10s (the shortest of mixed audio + video).

 The workaround of course is to demux the input audio and video, mix the
 audio separately, and then it's possible to combine the mixed audio and
 video track with -shortest. But this shouldn't be necessary if -shortest
 works as expected on video copy as it does on video transcode.
Ticket URL: <https://trac.ffmpeg.org/ticket/9512>
FFmpeg <https://ffmpeg.org>
FFmpeg issue tracker

More information about the FFmpeg-trac mailing list