Authors Top

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

1. Overview

In this tutorial, we’ll see how we can extract the duration information from video files. There are different tools available in Linux to work with multimedia files.

These tools help us process audio, video, or image files by extracting their metadata or converting them from one format to another. We’ll see how to use a few of them.

2. Using FFmpeg

The FFmpeg suite is one of the widely used packages in the multimedia arena. It is a set of libraries and tools for working with multimedia files such as audio, video, subtitles, and metadata. It is most commonly used for editing, encoding, and decoding multimedia files.

This application doesn’t come installed by default in Linux distributions. Let’s see how to install this in Ubuntu 18 and CentOS 7.

In Ubuntu 18:

$ sudo apt install ffmpeg

For CentOS 7:

$ sudo yum install epel-release
$ sudo yum install --nogpgcheck https://download1.rpmfusion.org/free/el/rpmfusion-free-release-7.noarch.rpm
$ sudo yum install ffmpeg

We can run these above commands to install the application in their respective distributions. Once installed, we’re ready to use it.

Below is the general usage of the command:

ffmpeg [global_options] {[input_file_options] -i input_url} ... {[output_file_options] output_url} ...

Let’s see how we can get the duration of a video file:

$ ffmpeg -hide_banner -i test.avi 2>&1 | grep Duration | cut -d ',' -f1
Duration: 00:00:17.20

From the above results, we can see the duration is 17.20 seconds.

Here, we’ve redirected the stderr to the stdout because the ffmpeg command sends the metadata to the stderr. Then we grep for the text Duration and cut only the duration information from the output.

The different options used are:

  • -hide_banner: Is used to suppress the version and other information printed by default by the ffmpeg command to the console.
  • -i: Sets the input file for processing

3. Using ffprobe

The ffprobe tool is part of the FFmpeg suite. If we’ve installed the ffmpeg package, we should be ready to use this tool. It is used to fetch the metadata from multimedia files.

The general usage is:

ffprobe [OPTIONS] [INPUT_FILE]

Let’s check how we can use this tool to fetch the duration:

$ ffprobe -i test.avi -hide_banner -v error -show_format -show_entries format=duration -of flat
format.duration="17.200000"

From the above result, we can see it has printed only the duration. The ffprobe tool comes with a few options to extract only the required information without using any grep or cut commands in Linux.

Let’s check the options we’ve used here one by one:

  • -i: Input file
  • -hide_banner: To suppress the version information
  • -v error: Sets the log level to error to suppress the default information about the input file
  • -show_format: This shows the file format information
  • -show_entries format=duration: Shows only the entry for the duration from the format information
  • -of flat: Use a flat output format to suppress the format tags

4. Using mediainfo

The mediainfo tool is an open-source program to extract metadata from audio, video, and image files.

Like the ffprobe tool above, this also supports options to extract just the information we need.

Again, this application also doesn’t come installed by default in Linux distributions. Let’s check the commands to install it:

On Ubuntu 18:

$ sudo apt install mediainfo

On CentOS 7:

$ sudo yum install mediainfo

Once installed, we’re ready to use the application.

The usage is:

mediainfo [-Options...] FileName1 [Filename2...]

Let’s check how we can get the duration information:

$ mediainfo --Output="General;%Duration%" test.avi 
17200
$

Here, we’ve used the –Output option to extract the duration of the supplied video file.

By default, the command prints the output in three sections for video files. A General section, a Video section, and an Audio section. Using the –Output option, we requested to print the Duration from the General section. And the duration is printed in milliseconds.

5. Using exiftool

The exiftool is an open-source program that allows us to edit metadata in audio, video, image, and PDF files. Since this can be used to edit the metadata, we can use this tool to fix corrupted multimedia files. Unlike others, this is a Perl application which makes it platform-independent too.

Let’s start by looking at how we can install the application:

On Ubuntu 18:

$ sudo apt install libimage-exiftool-perl

On CentOS 7:

$ sudo yum install epel-release
$ sudo yum install perl-Image-ExifTool

Once installed, we’re ready to run the application.

The usage is:

exiftool [OPTIONS] FILE

Let’s see how we can use this to extract the duration:

$ exiftool test.avi  | grep Duration 
Duration                        : 17.20 s

As we can see, the usage is pretty straightforward. The exiftool prints the metadata of the given video file. We’ve used the grep command to print just the duration information from the result.

6. Using mplayer

The mplayer is a robust media player which is used to play media files in various formats from the command line. It can print the metadata of the media file sent for playing. We can get the duration information from that metadata.

Let’s start by looking at different ways to install this application.

On Ubuntu 18:

$ sudo apt install mplayer

On CentOS 7:

$ sudo yum install mplayer

Post-installation, we’re ready with the application. Let’s check the general usage:

mplayer [options] [url|path/]filename

With that, let’s try to get the duration information:

$ mplayer -vo null -ao null -frames 0 -identify -really-quiet test.avi 2>/dev/null | grep ID_LENGTH
ID_LENGTH=17.20
$

From the above result, we can see that it printed the duration correctly.

Let’s check the different options used in the command:

  • -vo: Sets the video driver to null to suppress video output
  • -ao: Sets the audio driver to null to suppress audio output
  • -frames: Sets the frames to play to zero so that it doesn’t play the content
  • -identify: Sets the verbosity to information level, which prints the metadata of the video file
  • -really-quiet: Suppress other unwanted output to the console

Lastly, we’ve redirected all the stderr to /dev/null to prevent printing the unwanted errors in the console. Then we piped the result to the grep command and filtered the duration.

7. Using tcprobe

The tcprobe tool is part of the transcode package. The transcode utility is used to convert video files from one format to another. Even though it is old and no longer maintained, it can still be used to decode objects from a DVD. The tcprobe tool is used internally by the transcode application to gather information about the input stream.

Let’s start by looking at how we can install this tool.

On Ubuntu 18:

$ sudo apt install transcode

On CentOS 7:

$ sudo yum install transcode

After installing the application, we can start using it.

The general usage is:

tcprobe [options] [-]

Let’s check how we can fetch the duration from a video file:

$ tcprobe -i test.avi 2>/dev/null | grep duration | cut -d ',' -f3 
 duration=0:00:17.200

As we can see, we’ve supplied the video file as input using the -i flag. Then we took the result through the grep and the cut commands. Finally, it printed the duration correctly. We’ve redirected the stderr to /dev/null to ignore the error messages.

8. Conclusion

In this tutorial, we’ve seen how to extract the duration from video files using different tools. These tools offer a lot of features, but here we utilized them to collect only the duration information.

Authors Bottom

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

Comments are closed on this article!