Count ffmpeg frames

Does anyone know how to get the total number of frames from a video file using ffmpeg? The ffmpeg rendering result shows the current frame, and I need the number of frames to calculate the percentage progress.

+79
ffmpeg video
Jan 07 '10 at 2:31
source share
14 answers

Note. Having an edit list in MP4 / M4V / M4A / MOV may affect your frame number. See Edit Lists below.




ffprobe: container request

ffprobe -v error -select_streams v:0 -show_entries stream=nb_frames -of default=nokey=1:noprint_wrappers=1 input.mp4 
  • This is a quick method.
  • Not all formats (such as Matroska) report the number of frames, which leads to N/A output N/A See other methods listed below.

ffprobe: count frames

 ffprobe -v error -count_frames -select_streams v:0 -show_entries stream=nb_read_frames -of default=nokey=1:noprint_wrappers=1 input.mkv 
  • This is a slow method.
  • Add -skip_frame nokey to count only keyframes.

ffmpeg: count the number of frames

If you don't have ffprobe you can use ffmpeg instead:

 ffmpeg -i input.mkv -map 0:v:0 -c copy -f null - 
  • This is a pretty quick method.
  • Refer to frame= near the end of the console output.
  • Add -discard nokey input -discard nokey (before -i ) only for keyframe counting.

Edit lists

Ignore the edit list MP4 / M4V / M4A / MOV with -ignore_editlist 1 input -ignore_editlist 1 . By default, do not ignore the edit list.

What do the ffprobe options mean

  • -v error This hides the output of information (version information, etc.), -v error makes analysis easier.

  • -count_frames Count the number of frames per stream and report this in the corresponding section of the stream.

  • -select_streams v:0 Select only the video stream.

  • -show_entries stream=nb_frames or -show_entries stream=nb_read_frames Show only the entry for nb_frames or nb_read_frames .

  • -of default=nokey=1:noprint_wrappers=1 Set the output format (aka β€œwriter”) to default , do not print the key of each field ( nokey=1 ) and do not print the header and footer noprint_wrappers=1 ( noprint_wrappers=1 ) Yes shorter alternatives such as -of csv=p=0 .

Also see




media information

The well-known mediainfo tool can output the number of frames:

 mediainfo --Output="Video;%FrameCount%" input.avi 



MP4Box

For MP4 / M4V / M4A files.

Gpac MP4Box can show the number of frames:

 MP4Box -info input.mp4 

Refer to the Media Info line in the output for the video stream in question:

 Media Info: Language "Undetermined (und)" - Type "vide:avc1" - 2525 samples 

In this example, the video stream has 2525 frames.




boxdumper

For MP4 / M4V / M4A / MOV files.

boxdumper is a simple tool from l-smash. This will output a large amount of information. Under stsz sample stsz refer to sample_count for the number of frames. In this example, the input has 1900 video frames:

 boxdumper input.mp4 ... [stsz: Sample Size Box] position = 342641 size = 7620 version = 0 flags = 0x000000 sample_size = 0 (variable) sample_count = 1900 
  • Remember that a file can have more than one stsz atom.
+108
Feb 06 '15 at 23:56
source share

On Unix, this works like a charm:

 ffmpeg -i 00000.avi -vcodec copy -acodec copy -f null /dev/null 2>&1 | grep 'frame=' | cut -f 2 -d ' ' 
+23
Jul 31 '12 at 10:13
source share

Instead, calculate it based on time.

This is what I am doing and it works great for me and many others.) First find the length of the video in the following snippet:

 Seems stream 0 codec frame rate differs from container frame rate: 5994.00 (5994/1) -> 29.97 (30000/1001) Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/Users/stu/Movies/District9.mov': Duration: 00:02:32.20, start: 0.000000, bitrate: 9808 kb/s Stream #0.0(eng): Video: h264, yuv420p, 1920x1056, 29.97tbr, 2997tbn, 5994tbc Stream #0.1(eng): Audio: aac, 44100 Hz, 2 channels, s16 Stream #0.2(eng): Data: tmcd / 0x64636D74 

You should be able to consistently and safely find Duration: hh:mm:ss.nn to determine the size of the original video. Then for each update line (CR, no LF) you can analyze the text for the current timestamp, on which:

 frame= 84 fps= 18 q=10.0 size= 5kB time=1.68 bitrate= 26.1kbits/s frame= 90 fps= 17 q=10.0 size= 6kB time=1.92 bitrate= 23.8kbits/s frame= 94 fps= 16 q=10.0 size= 232kB time=2.08 bitrate= 913.0kbits/s 

Just don't always expect the perfect output from these status bars. These may include error messages, as here:

 frame= 24 fps= 24 q=-1.0 size= 0kB time=1.42 bitrate= 0.3kbits/s frame= 41 fps= 26 q=-1.0 size= 0kB time=2.41 bitrate= 0.2kbits/s [h264 @ 0x1013000]Cannot parallelize deblocking type 1, decoding such frames in sequential order frame= 49 fps= 24 q=26.0 size= 4kB time=0.28 bitrate= 118.1kbits/s frame= 56 fps= 22 q=23.0 size= 4kB time=0.56 bitrate= 62.9kbits/s 

Once you have time, it's just the math: time / durration * 100 = % done .

+13
Jan 07
source share

Not all formats retain their frame count or overall duration β€” and even if they do, the file may not be complete β€” so ffmpeg does not detect any of them by default.

Instead, try to find the end of the file and read the time, and then count the current time as you go.

Alternatively, you can try AVFormatContext->nb_index_entries or a detected duration that should work on a thin, at least intact AVI / MOV, or the FFMS2 library, which is probably too slow to worry about the progress bar.

+8
Jan 07 '10 at 3:15
source share

Try something like:

 ffmpeg -i "path to file" -f null /dev/null 

It writes the frame number to stderr, so you can extract the last frame from it.

+8
Jan 07 '10 at 3:16
source share

You can use ffprobe to get the frame number with the following commands

  • first method

ffprobe.exe -i video_name -print_format json -loglevel fatal -show_streams -count_frames -select_streams v

which report to print data in json format

select_streams v will tell ffprobe just give us the video data of the stream, and if you delete it, it will also provide you with audio information

and the output will look like

 { "streams": [ { "index": 0, "codec_name": "mpeg4", "codec_long_name": "MPEG-4 part 2", "profile": "Simple Profile", "codec_type": "video", "codec_time_base": "1/25", "codec_tag_string": "mp4v", "codec_tag": "0x7634706d", "width": 640, "height": 480, "coded_width": 640, "coded_height": 480, "has_b_frames": 1, "sample_aspect_ratio": "1:1", "display_aspect_ratio": "4:3", "pix_fmt": "yuv420p", "level": 1, "chroma_location": "left", "refs": 1, "quarter_sample": "0", "divx_packed": "0", "r_frame_rate": "10/1", "avg_frame_rate": "10/1", "time_base": "1/3000", "start_pts": 0, "start_time": "0:00:00.000000", "duration_ts": 256500, "duration": "0:01:25.500000", "bit_rate": "261.816000 Kbit/s", "nb_frames": "855", "nb_read_frames": "855", "disposition": { "default": 1, "dub": 0, "original": 0, "comment": 0, "lyrics": 0, "karaoke": 0, "forced": 0, "hearing_impaired": 0, "visual_impaired": 0, "clean_effects": 0, "attached_pic": 0 }, "tags": { "creation_time": "2005-10-17 22:54:33", "language": "eng", "handler_name": "Apple Video Media Handler", "encoder": "3ivx D4 4.5.1" } } ] } 

2. you can use

 ffprobe -v error -show_format -show_streams video_name 

which will give you stream data if you want selected information, such as frame rate, to use the following command

 ffprobe -v error -select_streams v:0 -show_entries stream=avg_frame_rate -of default=noprint_wrappers=1:nokey=1 video_name 

which give the numerical base of your video information, the problem is when you use this method, you may get an N/A result.

For more information check out this page. FFProbe Tips

+7
Apr 28 '15 at 6:54
source share

try the following:

 ffmpeg -i "path to file" -f null /dev/null 2>&1 | grep 'frame=' | cut -f 2 -d ' ' 
+3
Jul 15 '10 at 15:27
source share

to build on stu answer. this is how i found the frame rate for the video from my mobile phone. For a while I ran the following command. I let the number of frames reach approximately ~ 10000, before I get impatient and press ^ C:

 $ ffmpeg -i 2013-07-07\ 12.00.59.mp4 -f null /dev/null 2>&1 ... Press [q] to stop, [?] for help [null @ 0x7fcc80836000] Encoder did not produce proper pts, making some up. frame= 7989 fps= 92 q=0.0 Lsize=N/A time=00:04:26.30 bitrate=N/A dup=10 drop=0 video:749kB audio:49828kB subtitle:0 global headers:0kB muxing overhead -100.000042% Received signal 2: terminating. $ 

then I grabbed two pieces of information from this line, which starts with "frame =", the number of frames, 7989 and the time, 00: 04: 26.30. First you need to convert the time to seconds, and then divide the number of frames per second to get β€œframes per second”. "frames per second" is the frame rate.

 $ bc -l 0*60*60 + 4*60 + 26.3 266.3 7989/(4*60+26.3) 30.00000000000000000000 $ 

the frame rate for my video is 30 frames per second.

+2
Jul 11 '13 at
source share

The only thing I could do was the following:

 ffprobe -i my_video.mp4 -show_frames 2>&1|grep -c '^\[FRAME' 

To make sure this works with the video:

 ffprobe -i my_video.mp4 -show_frames 2>&1 | grep -c media_type=video 
+2
Aug 04 '14 at 2:31
source share

I use php_ffmpeg, then I can get all the time and all frames of the movie. Like fame

 $input_file='/home/strone/workspace/play/CI/abc.rmvb'; $ffmpegObj = new ffmpeg_movie($input_file); echo $ffmpegObj->getDuration(); echo $ffmpegObj->getFrameCount(); 

And then the item is on the page.

http://ffmpeg-php.sourceforge.net/doc/api/ffmpeg_movie.php

+1
Sep 15 '11 at 10:55
source share

Sorry for the necrop answer, but you may need this (since I have not found a solution for the latest ffmpeg releases.

With ffmpeg 3.3.4, I found what can be found with the following:

 ffprobe -i video.mp4 -show_streams -hide_banner | grep "nb_frames" 

At the end, the number of frames will be displayed. He worked on my video with sound. However, it gives the string "nb_frames" twice, but the first line was the actual number of frames in the tested videos.

+1
Nov 09 '17 at 1:08 on
source share

One answer mentions how to count the number of frames using "mediainfo":

mediainfo --Output = "Video;% FrameCount%" input.avi

If the input file is h264, the elementary stream 'mediainfo' does not report the number of frames. Only if the input file is a container (mp4, ts or avi), the number of frames is counted and reported.

0
Sep 28 '18 at 18:18
source share
 Cmd -> ffprobe.exe -v error -select_streams v:0 -show_entries stream=r_frame_rate,duration -of default=nw=1 "d:\movies\The.Matrix.1999.1080p.BrRip.x264.YIFY.dut.mp4" Result -> r_frame_rate=24000/1001 duration=8177.794625 Calculation -> Frames=24000/1001*8177.794625=196071 Proof -> ffmpeg -i "d:\movies\The.Matrix.1999.1080p.BrRip.x264.YIFY.dut.mp4" -f null /dev/null ffmpeg version N-92938-g0aaaca25e0-ffmpeg-windows-pacman Copyright (c) 2000-2019 the FFmpeg developers built with gcc 8.2.0 (GCC) configuration: --pkg-config=pkg-config --pkg-config-flags=--static --extra-version=ffmpeg-windows-pacman --enable-version3 --disable-debug --disable-w32threads --arch=x86_64 --target-os=mingw32 --cross-prefix=/opt/sandbox/cross_compilers/mingw-w64-x86_64/bin/x86_64-w64-mingw32- --enable-libcaca --enable-gray --enable-libtesseract --enable-fontconfig --enable-gmp --enable-gnutls --enable-libass --enable-libbluray --enable-libbs2b --enable-libflite --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libilbc --enable-libmodplug --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopus --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libtwolame --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libzimg --enable-libzvbi --enable-libmysofa --enable-libaom --enable-libopenjpeg --enable-libopenh264 --enable-liblensfun --enable-nvenc --enable-nvdec --extra-libs=-lm --extra-libs=-lpthread --extra-cflags=-DLIBTWOLAME_STATIC --extra-cflags=-DMODPLUG_STATIC --extra-cflags=-DCACA_STATIC --enable-amf --enable-libmfx --enable-gpl --enable-avisynth --enable-frei0r --enable-filter=frei0r --enable-librubberband --enable-libvidstab --enable-libx264 --enable-libx265 --enable-libxvid --enable-libxavs --enable-avresample --extra-cflags='-march=core2' --extra-cflags=-O2 --enable-static --disable-shared --prefix=/opt/sandbox/cross_compilers/mingw-w64-x86_64/x86_64-w64-mingw32 --enable-nonfree --enable-decklink --enable-libfdk-aac libavutil 56. 25.100 / 56. 25.100 libavcodec 58. 43.100 / 58. 43.100 libavformat 58. 25.100 / 58. 25.100 libavdevice 58. 6.101 / 58. 6.101 libavfilter 7. 47.100 / 7. 47.100 libavresample 4. 0. 0 / 4. 0. 0 libswscale 5. 4.100 / 5. 4.100 libswresample 3. 4.100 / 3. 4.100 libpostproc 55. 4.100 / 55. 4.100 Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'd:\movies\The.Matrix.1999.1080p.BrRip.x264.YIFY.dut.mp4': Metadata: major_brand : isom minor_version : 512 compatible_brands: isomiso2avc1mp41 encoder : Lavf58.25.100 Duration: 02:16:17.91, start: 0.000000, bitrate: 2497 kb/s Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x800 [SAR 1:1 DAR 12:5], 2397 kb/s, 23.98 fps, 23.98 tbr, 24k tbn, 47.95 tbc (default) Metadata: handler_name : VideoHandler Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 93 kb/s (default) Metadata: handler_name : GPAC ISO Audio Handler Stream mapping: Stream #0:0 -> #0:0 (h264 (native) -> wrapped_avframe (native)) Stream #0:1 -> #0:1 (aac (native) -> pcm_s16le (native)) Press [q] to stop, [?] for help Output #0, null, to '/dev/null': Metadata: major_brand : isom minor_version : 512 compatible_brands: isomiso2avc1mp41 encoder : Lavf58.25.100 Stream #0:0(und): Video: wrapped_avframe, yuv420p, 1920x800 [SAR 1:1 DAR 12:5], q=2-31, 200 kb/s, 23.98 fps, 23.98 tbn, 23.98 tbc (default) Metadata: handler_name : VideoHandler encoder : Lavc58.43.100 wrapped_avframe Stream #0:1(und): Audio: pcm_s16le, 44100 Hz, stereo, s16, 1411 kb/s (default) Metadata: handler_name : GPAC ISO Audio Handler encoder : Lavc58.43.100 pcm_s16le frame=196071 fps=331 q=-0.0 Lsize=N/A time=02:16:17.90 bitrate=N/A speed=13.8x video:102631kB audio:1408772kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown 
0
May 12 '19 at 9:54
source share

Linux

 ffmpeg -i "/home/iorigins//123.mov" -f null /dev/null 

Ruby

 result = 'ffmpeg -i #{path} -f null - 2>&1' r = result.match("frame=([0-9]+)") pr[1] 
-one
Jun 19 '18 at 17:04 on
source share



All Articles