Baeldung Pro – Linux – NPI EA (cat = Baeldung on Linux)
announcement - icon

Learn through the super-clean Baeldung Pro experience:

>> Membership and Baeldung Pro.

No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.

Partner – Orkes – NPI EA (tag=Kubernetes)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

1. Introduction

FFmpeg is one of the most popular tools to work with audio and video files. It is an open-source, cross-platform tool, and also offers a command line interface on Linux. It is very handy for performing basic tasks with audio and video (AV) such as viewing the stats and metadata about AV files, and converting, cropping, or joining them.

In this tutorial, let’s look at how we can count the total number of frames in a video file on the Linux command line, using FFmpeg.

2. Installing FFmpeg

The FFmpeg downloads page lists instructions for downloading and installing FFmpeg on various Linux distributions. On Ubuntu systems, we can install it using the following command:

$ sudo apt install ffmpeg

Once the installation is complete, we’ll have three commands at our disposal: ffmpegffprobe, and ffplay. However, for this tutorial, we’ll be using only ffmpeg and ffprobe.

3. Determining Frame Count Using ffprobe

ffprobe is a command line tool bundled with the FFmpeg package that gives us utilities to gather information from multimedia streams. This information includes duration, bitrate, and also the frame count.

3.1. Directly Obtaining the Frame Count

Using ffprobe, we can get the frame count of a video, say test.mp4, as follows:

$ ffprobe -v error
  -select_streams v:0
  -count_frames
  -show_entries stream=nb_read_frames
  -of csv=p=0
  test.mp4
962

The above command outputs 962 as the frame count without any additional text. Let’s look at the parameters we passed to the ffprobe command in detail:

  • -v error: Sets the verbosity level; error skips info outputs and prints only errors, if any
  • -select_streams v:0: Selects the first video stream in the file
  • -count_frames: Counts and reports the number of frames for this stream
  • -show_entries stream=nb_read_frames: Shows the frame count in the stream output
  • -of csv=p=0: Specifies the output format to show only the number of frames without any descriptor

3.2. Inferring Frame Count From Packet Count

We can also count the number of packets instead of counting the number of frames. In any case, the result will be the same, but counting the packets is usually faster than counting the frames. We can do this as follows:

$ ffprobe -v error
  -select_streams v:0 
  -count_packets
  -show_entries stream=nb_read_packets
  -of csv=p=0
  test.mp4 
962

In the above command, we substituted -count_frames with -count_packets, and nb_read_frames with nb_read_packets. We see that the output remains the same.

4. Determining Frame Count Using ffmpeg

While the ffprobe command is meant for gathering information and the ffmpeg command is meant for processing the file, the ffmpeg command also prints the frame numbers when it runs on a file. If, for some reason, we do not have ffprobe installed, we can use the ffmpeg to count the frames, by copying our video file, test.mp4, to the virtual null device /dev/null. We are not interested in the output of the command, but only in the logs, which contain the frame number. Let’s see how to do this:

$ ffmpeg -i test.mp4 -f null /dev/null
ffmpeg version 6.1.1-3ubuntu5 Copyright (c) 2000-2023 the FFmpeg developers
  built with gcc 13 (Ubuntu 13.2.0-23ubuntu3)
...
...
...
[out#0/null @ 0x5c24f2c3a980] video:451kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown
frame=  962 fps=0.0 q=-0.0 Lsize=N/A time=00:00:40.04 bitrate=N/A speed=81.8x

We see that we have the frame count of 962 shown in the last line of the above output. Further, we can filter the output using grep and cut to show just the number:

$ ffmpeg -i test.mp4 -f null /dev/null 2>&1 | grep 'frame=' | cut -d ' ' -
f 3
962

In the above command, we used grep to select only the output line that has frame= and the cut command to extract the number from the line. The result is just the frame count, 962.

5. Conclusion

In this article, we looked at how to count the total number of frames in a video using FFmpeg’s command line tools. Using the ffprobe command is the simple and straightforward option and the ffmpeg command can be used as a quick backup if the ffprobe command is unavailable.