Search code examples
linuxvideoffmpegvideo-processingffprobe

Per second frame count using ffmpeg


I need to count the number of frames in a video captured by a camera on a per-second basis. I haven't found a solution using ffmpeg or ffprobe (or something else) to output the number of frames per second (maintaining a constant frame rate is not guaranteed because of the capture mechanism and needs to be verified).

So far, I've needed to run ffmpeg and ffprobe separately. First, I run ffmpeg to trim the video:

ffmpeg -ss 00:00:00 -to <desired time in seconds> -i <in_video> -c copy <out_video>

Then, I run ffprobe to count the number of frames in the snippet:

ffprobe -v error -select_streams v:0 -count_frames -show_entries stream=nb_read_frames -print_format csv <out_video>

Is there one command to output the number of frames for each second in the video?


Solution

  • Run

    ffmpeg -report -i <in_video> -an -vf select='if(eq(n,0),1,floor(t)-floor(prev_selected_t))' -f null -

    In the generated report, search for select:1.000000

    that will get you lines of the form

    [Parsed_select_0 @ 000001f413152540] n:270.000000 pts:138240.000000 t:9.000000 key:0 interlace_type:P pict_type:P scene:nan -> select:1.000000 select_out:0

    The t is the timestamp and the n is the frame index. Check the frame index for each successive t. The difference is the frame count for that 1 second interval.