How to check how long a video (mp4) is using the shell?

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!


Solution 1:

This will give you the length of a video.

ffmpeg -i myvideo 2>&1 | grep Duration | cut -d ' ' -f 4 | sed s/,//

Solution 2:

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.

Solution 3:

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. :)

Solution 4:

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

Type:

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

In my case the result is:

00:58:28.05

58 Minutes and 28.05 seconds.

Hope this will helpful!