question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to use the 'shortest' argument

See original GitHub issue

I’m using ffmpeg-python and ffmpeg for my first time. I’m trying to use the shortest=1 argument in ffmpeg.output(), but my program keeps giving me errors when I use it.

I want to replace the audio from foo.mp4 with the audio from bar.webm, while keeping the visuals of foo.mp4. I got this working, but the problem is that ffmpeg.concat by default keeps playing the audio from bar.webm while the visuals from foo.mp4 have stopped playing. According to this FFmpeg docs page, this is the intended behaviour: “The concat filter will use the duration of the longest stream in each segment (except the last one), and if necessary pad shorter audio streams with silence.”

I want the output video to stop when the visuals stop playing, so I want it to cut off the audio, but I can’t get it to work. That page also states that I should be able to add shortest as an argument to get my desired result: “If set to 1, force the output to terminate when the shortest input terminates. Default value is 0.” I can’t get it to work, though. The second from the bottom line from the block of code below contains shortest=1, which I’d expect to work, but I get this error instead:

[NULL @ 000001d50dfdea80] Unable to find a suitable output format for '1' and 1: Invalid argument

Using shortestfoo instead gives me this error, as expected:

Unrecognized option 'shortestfoo'. and Error splitting the argument list: Option not found

This tells me that the shortest argument was a possible argument, but that the 1 value that I assign to it is wrong. Setting it to None my program runs, but the audio still won’t be cut off with this set to None, of course.

I also tried using True **{'shortest': 1} and **{'shortest': True} instead of 1. These also didn’t work.

What value for shortest should I use here? Am I correct in assessing that I’m just using the wrong value for shortest? Many thanks!

import ffmpeg

input_video_name = 'foo'
input_video_extension = '.mp4'

input_audio_name = 'bar'
input_audio_extension = '.webm'

input_video = ffmpeg.input('input videos/' + input_video_name + input_video_extension)
input_audio = ffmpeg.input('input audio/'+ input_audio_name + input_audio_extension)

stream = ffmpeg.concat(input_video, input_audio, v=1, a=1)

stream = ffmpeg.output(stream, 'output videos/' + input_video_name + '.mp4', shortest=1)
ffmpeg.run(stream)

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:5

github_iconTop GitHub Comments

4reactions
alexmagnatocommented, May 19, 2020

I had a similar problem, and this is the solution I found:

video_part = ffmpeg.input('video.mp4')
audio_part = ffmpeg.input('audio.mp3')
(
    ffmpeg
    .output(audio_part.audio, video_part.video, 'output-video.mp4', shortest=None, vcodec='copy')
    .run()
    
)
2reactions
rlevinecommented, Nov 11, 2020

This one was even more fun. Usage shortest=1 is documented in the api spec and here:

_filters.py, line 130:

        shortest: If set to 1, force the output to terminate when the shortest input terminates. Default value is 0.

My code:

output = (
        ffmpeg.concat(video_component, audio_component, v=1, a=1)
        .output(
            combined_file,
            pix_fmt='yuv420p',
            movflags='faststart',
            hls_time=10,
            hls_list_size=0,
            crf=18,
            format='mov',
            shortest=1,
        )
        .global_args('-loglevel', log_level)
        .overwrite_output() 
    )

But -shortest is a flag for ffmpeg, not a keyword arg. When ffmpeg-python processes its kwargs, it sorts them, shortest lands at the end of the list, the “1” appears right before the output file path, and ffmpeg drops an extra output file, named “1”, in the current working directory of the script.

Here’s the generated command line:

ffmpeg -i /Users/CS255/Desktop/git/python/fmva/sample_files/output/Fleece_04_10_frame_test_annotated_10NOV2020_1537_16.mp4 -i /Users/CS255/Desktop/git/python/fmva/sample_files/Fleece_04_10_frame_test.mp4 -filter_complex [0:v [1:a]concat=a=1:n=1:v=1[s0] -map [s0] -f mov -crf 18 -hls_list_size 0 -hls_time 10 -movflags faststart -pix_fmt yuv420p -shortest 1 /Users/CS255/Desktop/git/python/fmva/sample_files/output/Fleece_04_10_frame_test_annotated_w_audio_10NOV2020_1537_16.mp4 -loglevel error -y

Setting shortest=None will get it to only output -shortest in the command line, as expected.

Easiest fix might just be to change the docs?

Thanks!

Python 3.7.9 ffmpeg 4.0.2 ffmpeg-python 0.2.0

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use the 'shortest' argument with ffmpeg-python
I'm trying to use the shortest=1 argument in ffmpeg.output() , but my program keeps giving me errors when I use it. I want...
Read more >
The shortest argument you've ever seen in favor of Late Insertion
Preminger 2014 shows that checking-based theories of agreement are untenable, and only valuation-based theories are adequate. In valuation-based ...
Read more >
Stop Arguing Over the Smallest Things - Lia Huynh
My advice is to slow down a little and think about what outcome you want. Don't settle for short term gains–yelling and getting...
Read more >
Use a shortest-paths argument to prove a combinatorics identity
This graphical representation of a typical staircase pattern in a n×m rectangle: enter image description here.
Read more >
How to Use Key Function in Max() and Min() in Python - Medium
Return the smallest item in an iterable or the smallest of two or more arguments. If one positional argument is provided, it should...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found