How to set a video's duration in FFMPEG?

89,083

Solution 1

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

Solution 2

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.

Solution 3

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.

Share:
89,083
user872387
Author by

user872387

Updated on July 14, 2022

Comments

  • user872387
    user872387 almost 2 years

    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.

  • PeterCo
    PeterCo almost 8 years
    How can we set the output duration to the same as input duration, like the possibilty in scale with -1?
  • Aaron Franke
    Aaron Franke over 7 years
    Note: This needs to be placed somewhere in the middle of the command, like this: ffmpeg -i input.mp3 -t 00:05:00 output.mp3
  • manish1706
    manish1706 about 7 years
    how to calculate duration of time depending on drawtext text width scrolling ?
  • llogan
    llogan about 5 years
    -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.