Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ffmpeg selects shortest movie but leaves full length audio

Tags:

ffmpeg

I've created a tool to create a grid of movies, like in the Brady Bunch opening. It's a cool, funny effect.

Here's a sample movie showing the grid.

Grid of movies in ffmpeg output

The problem is that ffmpeg is choosing the shortest movie to determine the maximum length of overlaid videos, but still using the full length of the first audio track to determine the overall output movie length.

So all my videos stop moving when the shortest one ends, but the audio plays on for full length of the first movie.

How can I either (1) set the length of the output movie to the longest input movie or (2) match the audio length to the shortest movie length as well?

Gist of my script.

Based on this ffmpeg example usage which exhibits the same audio problem:

ffmpeg
    -i 1.avi -i 2.avi -i 3.avi -i 4.avi
    -filter_complex "
        nullsrc=size=640x480 [base];
        [0:v] setpts=PTS-STARTPTS, scale=320x240 [upperleft];
        [1:v] setpts=PTS-STARTPTS, scale=320x240 [upperright];
        [2:v] setpts=PTS-STARTPTS, scale=320x240 [lowerleft];
        [3:v] setpts=PTS-STARTPTS, scale=320x240 [lowerright];
        [base][upperleft] overlay=shortest=1 [tmp1];
        [tmp1][upperright] overlay=shortest=1:x=320 [tmp2];
        [tmp2][lowerleft] overlay=shortest=1:y=240 [tmp3];
        [tmp3][lowerright] overlay=shortest=1:x=320:y=240
    "
    -c:v libx264 output.mkv
like image 237
Stan James Avatar asked Sep 27 '22 09:09

Stan James


1 Answers

How can I set the length of the output movie to the longest input movie?

Remove the shortest=1 from each overlay. By default the shortest input for the overlay will simply stop and the last frame will be repeated while the longer input continues. This behavior can be changed with the eof_action overlay option. See overlay filter docs.

How can I match the audio length to the shortest movie length as well?

Add the -shortest output option. This is a different option that the overlay shortest option; place it outside the filtergraph as an output option (before the output file). Now the final output file will be the same duration as the shortest stream: either the video from the filtergraph, or the audio from one of your inputs (which one exactly depends on the default stream selection behavior because I don't see you doing anything with the audio in the command).

IIRC, there may be an existing bug involving filtering and -shortest not doing what is expected, but I can't recall the details at the moment and am too lazy to look. Just something to be aware of.

like image 181
llogan Avatar answered Oct 18 '22 19:10

llogan