user872387
user872387

Reputation: 571

How to set a video's duration in FFMPEG?

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.

Upvotes: 46

Views: 126716

Answers (3)

Pau Coma Ramirez
Pau Coma Ramirez

Reputation: 4711

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.

Upvotes: 15

Roel Van de Paar
Roel Van de Paar

Reputation: 2228

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.

Upvotes: 5

marto
marto

Reputation: 4493

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

Upvotes: 70

Related Questions