Count frames in H.264 bitstream

user500 picture user500 · Oct 28, 2013 · Viewed 10.6k times · Source

How to count/detect frames (pictures) in raw H.264 bitstream? I know there are 5 VCL NALU types but I don't know how to rec(k)ognize sequence of them as access unit. I suppose detect a frame means detect an access unit as access unit is

A set of NAL units that are consecutive in decoding order and contain exactly one primary coded picture. In addition to the primary coded picture, an access unit may also contain one or more redundant coded pictures, one auxiliary coded picture, or other NAL units not containing slices or slice data partitions of a coded picture. The decoding of an access unit always results in a decoded picture.

I want it to know what is the FPS of live stream out to server.

Answer

alexbuisson picture alexbuisson · Nov 3, 2013

You are right on the interpretation, and if you want to parse the stream by yourself, take a look here

But to quickly extract stream info in a format easy to read and parse (with any text parser) you can use ffprobe

ffprobe -show_streams -count_frames -pretty filename

You will find in the output:

  • nb_read_frames=....

And for the fps, as I heard that ffprobe may report some error for the fps, try a simple ffmpeg -i command.

ffmpeg -i filename 2>&1 | sed -n "s/.*, \(.*\) fps.*/\1/p"