Software & AppsOperating SystemLinux

How To Check Video Length in MP4 Files Using Shell Commands

Ubuntu 19

In this article, we will explore various methods to check the length of a video in MP4 format using different shell commands. We’ll use tools such as FFmpeg, Mediainfo, Exiftool, Avconv, and ffprobe, providing detailed explanations and examples for each.

Quick Answer

To check the length of a video in MP4 format using shell commands, you can utilize various tools such as FFmpeg, Mediainfo, Exiftool, Avconv, and ffprobe. Each tool has its own command syntax, but they all provide the duration of the video as output. By running the appropriate command for the tool you have installed, you can quickly obtain the video length in MP4 files.

Using FFmpeg

FFmpeg is a powerful tool that can handle a variety of multimedia data, including video and audio files. To use it for checking the length of an MP4 file, you can use the following command:

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

Here’s what each part of this command does:

  • ffmpeg -i myvideo.mp4 calls FFmpeg to get information about myvideo.mp4.
  • 2>&1 redirects standard error to standard output. This is necessary because FFmpeg outputs the information we need to standard error.
  • grep Duration filters the output to lines containing ‘Duration’.
  • cut -d ' ' -f 4 splits each line into fields by spaces, and outputs the fourth field, which is where the duration is.
  • sed s/,// removes the trailing comma from the duration.

Using Mediainfo

Mediainfo is a convenient tool for extracting information about media files. To install Mediainfo, visit their official website. Once installed, you can run the following command to get the duration of the video:

mediainfo --Inform="Video;%Duration%" inputfile.mp4

This command provides the duration of the video in milliseconds. The --Inform option tells Mediainfo what information to output, in this case, the duration of the video.

Using Exiftool

Exiftool is a versatile tool that can read, write, and edit meta information in a wide variety of files. To install Exiftool on Debian-based systems, run apt install libimage-exiftool-perl. Then, use the following command to get the duration of the video:

exiftool inputfile.mp4 | grep Duration

This command extracts the duration information from the Exif metadata of the file. The grep Duration part filters the output to show only the line containing ‘Duration’.

Using Avconv

Avconv, part of the Libav project, is another tool for handling multimedia data. To install Avconv on Ubuntu, run apt install libav-tools. Then, use the following command to get the duration of the video:

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

This command works similarly to the FFmpeg command explained earlier, extracting the duration information from the Avconv output.

Using ffprobe

ffprobe is a simple multimedia stream analyzer, part of the FFmpeg project. To use it to get the duration of a video, run the following command:

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

This command extracts the duration information from the ffprobe output. The grep -E '^ +Duration' part filters the output to show only the line starting with ‘Duration’.

Conclusion

In this article, we’ve explored several methods to check the length of an MP4 video using shell commands. Each method uses a different tool and has its own advantages, so you can choose the one that suits your needs and the tools available on your system. Remember to replace ‘inputfile.mp4’ with the path to your actual video file when running these commands.

How do I install FFmpeg?

To install FFmpeg, you can use package managers like apt on Ubuntu or brew on macOS. For example, on Ubuntu, you can run sudo apt install ffmpeg to install it.

How do I install Mediainfo?

To install Mediainfo, you can visit their official website at https://mediaarea.net/ and download the appropriate version for your operating system. Follow the installation instructions provided on their website.

How do I install Exiftool?

To install Exiftool on Debian-based systems, you can run sudo apt install libimage-exiftool-perl. This will install the necessary package to use Exiftool.

How do I install Avconv?

Avconv is part of the Libav project. To install Avconv on Ubuntu, you can run sudo apt install libav-tools. This will install the necessary package to use Avconv.

How do I install ffprobe?

ffprobe is part of the FFmpeg project. To install ffprobe, you can follow the same steps as installing FFmpeg. For example, on Ubuntu, you can run sudo apt install ffmpeg to install both FFmpeg and ffprobe.

How can I check the length of a video in MP4 format using shell commands?

You can use tools like FFmpeg, Mediainfo, Exiftool, Avconv, or ffprobe to check the length of a video in MP4 format. Each tool has its own command, and you can find detailed explanations and examples in the article above.

Can I use these shell commands on Windows?

Yes, you can use these shell commands on Windows by using a terminal emulator like PowerShell, Git Bash, or Cygwin. Make sure you have the necessary tools installed and adjust the commands accordingly.

Leave a Comment

Your email address will not be published. Required fields are marked *