[FFmpeg-trac] #9487(ffmpeg:open): FFmpeg -shortest not working when using -filter_complex with audio and video filters
FFmpeg
trac at avcodec.org
Sun Oct 31 23:08:43 EET 2021
#9487: FFmpeg -shortest not working when using -filter_complex with audio and
video filters
-------------------------------------+-------------------------------------
Reporter: | Owner: (none)
pigeonburger |
Type: defect | Status: open
Priority: important | Component: ffmpeg
Version: git-master | Resolution:
Keywords: shortest | Blocked By:
filter_complex audio video |
audio/video filters |
Blocking: | Reproduced by developer: 0
Analyzed by developer: 0 |
-------------------------------------+-------------------------------------
Description changed by pigeonburger:
Old description:
> Pretty much the exact same issue was described
> [[https://trac.ffmpeg.org/ticket/3789|here]] several years ago, but the
> 'solution' given, while it works, is more inconvenient and doesn't
> explain why the actual command in question does not work.
>
> I'm using the following command to mix the audio of `input.mp4` with
> `song.m4a` and ensure that the dimensions of my output file are even
> numbers:
>
> {{{
> ffmpeg -i input.mp4 -i song.m4a -filter_complex \
> [0:a][1:a]amix[s0];[0:v]pad=height=ceil(ih/2)*2:width=ceil(iw/2)*2[s1] \
> -map [s0] -map [s1] -shortest output.mp4
> }}}
>
> `input.mp4` is only about 20 seconds long, but `song.m4a` is about 8
> minutes long. I want to mix both the audio streams together, so that the
> first 20 seconds of `song.m4a` plays alongside the original audio of
> `input.m4a`. To ensure that the output file does not become 8 minutes
> long, I'm using the `-shortest` option, which should make FFmpeg stop
> encoding once it hits the shortest audio/video stream. However, in the
> command above, `-shortest` seems to be not working entirely.
>
> When I separate the audio and video filters into 2 separate
> `-filter_complex`s, it works as intended (doing this was also the
> solution given in the issue a linked at the top):
>
> {{{
> ffmpeg -i input.mp4 -i song.m4a -filter_complex \
> [0:a][1:a]amix[s0] -filter_complex
> [0:v]pad=height=ceil(ih/2)*2:width=ceil(iw/2)*2[s1] \
> -map [s0] -map [s1] -shortest output.mp4
> }}}
>
> Same with if I remove the video filters entirely:
>
> {{{
> ffmpeg -i input.mp4 -i song.m4a -filter_complex [0:a][1:a]amix[s0] -map
> [s0] -map 0:v -shortest output.mp4
> }}}
>
> Using the above 2 commands results in a 20-second long video as intended.
>
> I guess my question is why is `-shortest` made redundant when combining
> both audio and video filters in a single `-filter_complex` argument? Is
> this a bug, or the intended behaviour? How can I get it to work as
> intended with just one `-filter_complex`?
>
> I'm using a self-compiled FFmpeg build on Linux (using the most recent
> Git version as of writing this) but I can confirm I can also recreate the
> issue with the official Linux AND Windows builds as well, even when using
> different audio and video files too.
New description:
Pretty much the exact same issue was described
[[https://trac.ffmpeg.org/ticket/3789|here]] several years ago, but the
'solution' given, while it works, is more inconvenient and doesn't explain
why the actual command in question does not work.
I'm using the following command to mix the audio of `input.mp4` with
`song.m4a` and ensure that the dimensions of my output file are even
numbers:
{{{
ffmpeg -i input.mp4 -i song.m4a -filter_complex \
[0:a][1:a]amix[s0];[0:v]pad=height=ceil(ih/2)*2:width=ceil(iw/2)*2[s1] \
-map [s0] -map [s1] -shortest output.mp4
}}}
`input.mp4` is only about 20 seconds long, but `song.m4a` is about 8
minutes long. I want to mix both the audio streams together, so that the
first 20 seconds of `song.m4a` plays alongside the original audio of
`input.m4a`. To ensure that the output file does not become 8 minutes
long, I'm using the `-shortest` option, which should make FFmpeg stop
encoding once it hits the shortest audio/video stream. However, in the
command above, `-shortest` seems to be not working entirely.
When I separate the audio and video filters into 2 separate
`-filter_complex`s, it works as intended (doing this was also the solution
given in the issue a linked at the top):
{{{
ffmpeg -i input.mp4 -i song.m4a -filter_complex \
[0:a][1:a]amix[s0] -filter_complex
[0:v]pad=height=ceil(ih/2)*2:width=ceil(iw/2)*2[s1] \
-map [s0] -map [s1] -shortest output.mp4
}}}
Same with if I remove the video filters entirely:
{{{
ffmpeg -i input.mp4 -i song.m4a -filter_complex [0:a][1:a]amix[s0] -map
[s0] -map 0:v -shortest output.mp4
}}}
Using the above 2 commands results in a 20-second long video as intended.
I guess my question is why is `-shortest` made redundant when combining
both audio and video filters in a single `-filter_complex` argument? Is
this a bug, or the intended behaviour? How can I get it to work as
intended with just one `-filter_complex`?
I'm using a self-compiled FFmpeg build on Linux (using the most recent Git
version as of writing this) but I can confirm I can also recreate the
issue with the official Linux AND Windows builds as well, even when using
different audio and video files too.
-----
The issue can be easily recreated by anyone using these commands (courtesy
of Michael Koch):
{{{
// Create video file
ffmpeg -f lavfi -i testsrc2 -f lavfi -i sine=2000 -t 5 -y input.mp4
// Create song file
ffmpeg -f lavfi -i sine=500 -t 10 -y song.m4a
// Combine both files (-shortest should work here but it does not)
ffmpeg -i input.mp4 -i song.m4a -lavfi [0:a][1:a]amix[s0];[0:v]null[s1]
-map [s0] -map [s1] -shortest -y output.mp4
}}}
--
--
Ticket URL: <https://trac.ffmpeg.org/ticket/9487#comment:7>
FFmpeg <https://ffmpeg.org>
FFmpeg issue tracker
More information about the FFmpeg-trac
mailing list