2

I'm trying to count the number of frames in a video but ffmpeg and ffprobe are giving me two different answers.

$ time ffprobe -v error -count_frames -select_streams v:0 -show_entries stream=nb_read_frames -of default=nokey=1:noprint_wrappers=1 myvideo.mp4
2858

real    0m2.987s
user    0m2.740s
sys     0m0.172s

When I check the same file with ffmpeg I get 2 more frames...

$ time ffmpeg -y -i myvideo.mp4 -vcodec copy -acodec copy -f null /dev/null 2>&1 | grep 'frame=' | awk '{print $2}'
2860

real    0m0.127s
user    0m0.080s
sys     0m0.032s

I used ffprobe to output all of the frames and count the number of "[FRAME]"s in the resultant output...

ffprobe -i myvideo.mp4 -show_frames -v error | grep -o '\[FRAME\]' | wc -l
2858

Which as you can see shows the number ffprobe thought there were.

Obviously I would prefer to use ffmpeg here because it is significantly faster than ffprobe and I am dealing with thousands of videos that need parsing and indexing. However the failure isn't consistent across multiple videos; sometimes it's 1 out, other times it's 2 or more...

Unfortunately, I have been counting frames for the past two years using the ffmpeg method, so I have a significant library of videos to reprocess now ... he gulps... I guess its a good way to verify the readability of the files on the cluster... even so, its going to take probably a few weeks to recalculate all of the existing video frame sizes.

Greg
  • 1,803
  • 3
  • 18
  • 26
  • Can you provide a small input file so we can give it a try? – llogan Feb 26 '18 at 23:46
  • Sure thing... I have uploaded an utterly uninteresting video to https://securitgroup.com/mp4/trouble.mp4... (someone turned out the lights). – Greg Feb 27 '18 at 03:48
  • 1
    I get `2858` with `ffmpeg -i trouble.mp4 -f null -`. Sounds like you have an older version of ffmpeg. In any case, the number your ffmpeg is producing is the same as that stored in the `stsz` box of the file, and includes frames dropped by the edit list stored in the file. You can get the same number in ffprobe by looking at `nb_frames` with `-show_streams`. – Gyan Feb 27 '18 at 06:29
  • Pretty sure I have the current stable release of ffmpeg which I installed last week, I will verify in the morning. Thanks – Greg Feb 27 '18 at 07:55
  • ffprobe version 3.4.1-1~16.04.york0 and ffmpeg version 3.4.1-1~16.04.york0 – Greg Feb 27 '18 at 19:32

1 Answers1

0

Yes Mulvya was correct. I was able to get a consistent number by using FFprobe to query the nb_frames. By default ffprobe uses -count_frames which literally starts at the top of the file and counts the number of frames all the way to the bottom of the file. This is, of course, quite a slow operation, especially if the video file is quite large (which mine tend to be).

time ./ffprobe -v error -count_frames -select_streams v:0 -show_entries stream=nb_read_frames -of default=nokey=1:noprint_wrappers=1 big.mp4
real    1m4.531s
user    0m7.172s
sys     0m0.104s

FFmpeg writes to the stsz box, the number of frames it thinks is in the file when (in my case) it is converted from another format. So, I chose to use the value in stsz because although it is not correct, it is consistent and very quick to query

time ffprobe -v quiet -pretty  -select_streams v:0 -show_entries "stream=nb_frames" big.mp4
real    0m0.023s
user    0m0.040s
sys     0m0.008s

Thanks Mulvya!

Greg
  • 1,803
  • 3
  • 18
  • 26