40

How can I limit the video duration for a given video? For example, if we are uploading one video that should not exceed more than 5 minutes, I need a command in FFMPEG.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
user872387
  • 511
  • 1
  • 4
  • 6

3 Answers3

63

Use the -t option to specify a time limit:

`-t duration'
    Restrict the transcoded/captured video sequence to the duration specified in seconds. hh:mm:ss[.xxx] syntax is also supported. 

http://www.ffmpeg.org/ffmpeg.html

marto
  • 4,402
  • 25
  • 15
9

Just to elaborate a bit further for more detailed use and examples.

As Specified in the FFMpeg Docs


  • -t duration (input/output)

    • When used as an input option (before -i),
      • limit the duration of data read from the input file.
      • e.g. ffmpeg -t 5 -i input.mp3 testAsInput.mp3
        • Will stop writing automatically after 5 seconds
    • When used as an output option (before an output url),
      • stop writing the output after its duration reaches duration.
      • e.g. ffmpeg -i input.mp3 -t 5 testAsOutput.mp3
        • Will stop writing automatically after 5 seconds
    • Effectively, in this use case the result is the same. See below for a more extended use case.
  • -to position (input/output)

    • Stop writing the output or reading the input at position.
    • e.g. same as above but with to instead of t
  • duration or positionmust be a time duration specification, as specified in the ffmpeg-utils(1) manual.

    • [-][HH:]MM:SS[.m...] or [-]S+[.m...][s|ms|us]
  • -to and -t are mutually exclusive and -t has priority.


Example use as input option with multiple inputs

Note: -f pulse -i 1 is my system audio , -f pulse -i 2 is my micrphone input

Lets imagine I want to record both my microphone and speakers at the same time indefinetly.(until I force a stop with Ctrl+C)

ffmpeg \
-f pulse -i 1 \
-f pulse -i 2 \
-filter_complex "amix=inputs=2" \
testmix.mp3
  • Now lets imagine I only want to record the first 5 seconds of my system audio and always my microphone, again, until I kill the process with Ctrl+C).
ffmpeg \
-t 5 -f pulse -i 1 \
-f pulse -i 2 \
-filter_complex "amix=inputs=2:duration=longest" \
testmix.mp3

Note: :duration=longest amix option is the default anyway, so not really needed to specify explicitly

  • Now lets assume I want the same as above but limit the recording to 10 seconds. The following examples would satisfy that requirement:
ffmpeg \
-t 5 -f pulse -i 1 \
-t 10 -f pulse -i 2 \
-filter_complex "amix=inputs=2:duration=longest" \
testmix.mp3
ffmpeg \
-t 5 -f pulse -i 1 \
-f pulse -i 2 \
-filter_complex "amix=inputs=2:duration=longest" \
-t 10 testmix.mp3

Note: With regards to start position searching/seeking this answer with a bit of investigation I did, may also be of interest.

Pau Coma Ramirez
  • 4,261
  • 1
  • 20
  • 19
5

An example;

ffmpeg -f lavfi -i color=s=1920x1080 -loop 1 -i "input.png" -filter_complex "[1:v]scale=1920:-2[fg]; [0:v][fg]overlay=y=-'t*h*0.02'[v]" -map "[v]" -t 00:00:03 output.mp4

This sets the max time to 3 seconds. Note that the -t has to be just before the output file, if you set it at the start of this command, i.e. ffmpeg -t .... it will NOT work.

Roel Van de Paar
  • 2,111
  • 1
  • 24
  • 38
  • 4
    **`-t` can be both an input or output option** depending on placement location. If you place `-t` as an input option (before `-i`), then it will only apply to the particular input it precedes. So in your case would apply only to `-i color=s=1920x1080` unless you add another `-t` before `-i "input.png"`. Meanwhile, you are looping `input.png` indefinitely and overlaying it. overlay filter will not end with the shortest input unless you tell it to do so, such as `overlay=y=-'t*h*0.02':shortest=1`. – llogan May 24 '19 at 17:38