I need to ftp upload all the mp4 files in a directory with length > 4 minutes using the shell. I can't find any script to check how long a video is. Does anybody have any idea how to do that?

Thank you very much!


11 Answers 11


This will give you the length of a video.

ffmpeg -i myvideo 2>&1 | grep Duration | cut -d ' ' -f 4 | sed s/,//
  • Also works for remote video urls e.g. ffmpeg -i "instagram.fewr1-1.fna.fbcdn.net/t50.2886-16/…" 2>&1 | grep Duration | cut -d ' ' -f 4 | sed s/,//
    – skidadon
    Oct 16, 2017 at 19:35
  • Is the FFmpeg log messages format guaranteed? If not, I don't like this much - it could stop working with any release. Mar 24, 2019 at 14:10
  • ffmpeg -i myvid does output media info, but returns an error status (-1). The command ffprobe however does exactly the same but without the error (if worried about future behavior changing); e.g., ffprobe -hide_banner myvideo 2>&1 | grep "Duration" then cut, sed, etc; however there are options for customizing the output format as well (xml, json, txt, etc).
    – michael
    Nov 28, 2022 at 3:36

Mediainfo is a fast tool for this purpose:

$ mediainfo --Inform="Video;%Duration%"  [inputfile]

You can find more options in a more thorough answer.

In my tests, ffprobe takes 0.3 seconds and mediainfo takes 0.09 seconds.

  • 2
    This gives weird results... like 2679600... better to use mediainfo --Inform="Video;%Duration/String%" [inputfile] ... which would get you something ike 43 min 50s Oct 28, 2019 at 8:11
  • This worked great for me on RPi in command-line-only mode Apr 20, 2022 at 20:34

exiftool (originally intended for reading camera metadata from image files, but later expanded to read and write metadata from almost any kind of media file) is very convenient to use for this. Run it with:

exiftool FILE.mp4 | grep Duration

You'll probably need to install exiftool first, but this is is easily done with the following command (on Debian and derivatives like Ubuntu etc.):

apt install libimage-exiftool-perl

Of course, this answer is just another alternative. Many of the other answers are good too. :)

  • 2
    I believe you can use exiftool -Duration FILE.mp4 to just get the duration. No grep needed ...
    – jochen
    Nov 2, 2020 at 18:09

Adding to pers solution, this can be used on an entire directory:

for f in *; do ffmpeg -i "$f" 2>&1 | grep Duration | cut -d " " -f 4 | sed s/,//  | tr -d "\n" && echo " $f"; done

it can even be extended by | sort to have the files sorted by their length.

you can add this to .bashrc or .bash_aliases in order to be able to do lsvlength | sort on a directory

alias lsvlength='for f in *; do ffmpeg -i "$f" 2>&1 | grep Duration | cut -d " " -f 4 | sed s/,//  | tr -d "\n" && echo " $f"; done'

You can try to use avconv command..

First you should to install:

Install via the software center

if you type the command with the flag -i, you will get information about the video:

avconv -i test.mp4

In the output there is a field called Duration

avconv version 0.8.4-4:0.8.4-0ubuntu0.12.04.1, Copyright (c) 2000-2012 the Libav developers
  built on Nov  6 2012 16:51:33 with gcc 4.6.3
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'test.mp4':

  Duration: 00:58:28.05, start: 0.000000, bitrate: 888 kb/s
    Stream #0.0(eng): Video: h264 (High), yuv420p, 720x404, 748 kb/s, 25 fps, 25 tbr, 20k tbn, 50 tbc
    Stream #0.1(und): Audio: aac, 48000 Hz, stereo, s16, 127 kb/s

Now you can use the command to only get the value of the field Duration


avconv -i file.mp4 2>&1 | grep 'Duration' | awk '{print $2}' | sed s/,//

In my case the result is:


58 Minutes and 28.05 seconds.

Hope this will helpful!

  • 1
    This can also be used with avprobe - no need for the -i, but it otherwise works exactly the same way. Or ffprobe for @per's answer.
    – evilsoup
    Dec 18, 2012 at 7:54

Even simpler:

avprobe file.mp4 -show_format_entry duration
  • I currently get the message "Option 'show_format_entry' is deprecated, use '-show_entries format=duration' instead" Nov 5, 2017 at 13:31

ffprobe is more elegant solution than ffmpeg which throws an error if not given the output path.

ffprobe video.mp4 2>&1 | grep -E '^ +Duration' | cut -d':' -f2- | cut -d, -f1

Check all the videos in current folder with their names and lengths:

exiftool -T -Duration -FileName *


0:01:38 vid1.mp4
0:09:54 vid2.mp4

As you can see there are numerous ways to accomplish this: ffprobe mediafile -show_entries format=format=duration 2>&1 | grep DURATION where mediafile is the name of the file, will provide output similar to the below. The first being the duration of the first stream (typically video) and the second providing the duration of the second stream (typically audio)

  DURATION        : 00:21:40.132000000
  DURATION        : 00:21:40.062000000

A more succinct but slightly less accurate and resource intensive approach would be exiftool -T -Duration mediafile where mediafile is the name of the file, will provide output similar to the below.


Another good option is mediainfo --Inform="Video;%Duration/String3%" mediafile where mediafile is the name of the file, will provide output similar to the below.


If you want to see duration of some videos in a directory , you can use following command

exiftool * | grep ^Duration | cut -d' ' -f 26

  • 2
    No need to use grep and such. Simply exiftool -T -Duration *.mkv (or whatever file pattern)
    – PerlDuck
    Mar 24, 2019 at 14:37

The above answers are all great as long as you combine it with: https://stackoverflow.com/questions/2181712/simple-way-to-convert-hhmmss-hoursminutesseconds-split-seconds-to-seconds To get something like:

for movie in *                                         
  duration=$(ffmpeg -i $movie 2>&1 | grep Duration | cut -d ' ' -f 4 | sed -e 's/,//' -e 's/\.[0-9]*$//')
  duration_seconds=$(echo "$duration" | awk -F: '{ print ($1 * 3600) + ($2 * 60) + $3 }')
  if [ $duration_seconds -gt 240 ]; then
    echo "the movie $movie is longer than 4 minutes: $duration"
    echo "the movie $movie is shorter than 4 minutes: $duration" 
the movie a_short.mpg is shorter than 4 minutes: 00:03:06
the movie mlong_147309001.mov is longer than 4 minutes: 00:10:44

I wanted to post this as I came across this question, but none of the above provided the link/answer to H:M:S->testable shell threshold. Hope you have a great day!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .