mrgloom
mrgloom

Reputation: 21612

ffmpeg : how to determine frame rate automatically?

I use this simple script to convert video to images using ffmpeg, but frame rate is fixed, how can I determine it automatically?

FRAME_RATE="30"
SEPARATOR='/'


VIDEO_PATH=$1

VIDEO_BASE_DIR=`dirname $1`
FRAMES_DIR=$VIDEO_BASE_DIR$SEPARATOR"Frames"
rm -rf $FRAMES_DIR
mkdir $FRAMES_DIR

#Convert video to images
./ffmpeg -r $FRAME_RATE -i $VIDEO_PATH $FRAMES_DIR$SEPARATOR"image%d.png"

UPDATE:

By ffprobe I checked that my 1st video frame rate is 30. Also results are the same (339 frames are produced) even I reduce frame rate, so -r option doesn't work or work in some other way?

These command give the same result:

./ffmpeg -r 10 -i $VIDEO_PATH $FRAMES_DIR$SEPARATOR"image%d"$EXTENSION
./ffmpeg -r 30 -i $VIDEO_PATH $FRAMES_DIR$SEPARATOR"image%d"$EXTENSION
./ffmpeg -i $VIDEO_PATH $FRAMES_DIR$SEPARATOR"image%d"$EXTENSION

Output:

ffmpeg version N-63893-gc69defd Copyright (c) 2000-2014 the FFmpeg developers
  built on Jul 16 2014 05:38:01 with gcc 4.6 (Debian 4.6.3-1)
  configuration: --prefix=/root/ffmpeg-static/64bit --extra-cflags='-I/root/ffmpeg-static/64bit/include -static' --extra-ldflags='-L/root/ffmpeg-static/64bit/lib -static' --extra-libs='-lxml2 -lexpat -lfreetype' --enable-static --disable-shared --disable-ffserver --disable-doc --enable-bzlib --enable-zlib --enable-postproc --enable-runtime-cpudetect --enable-libx264 --enable-gpl --enable-libtheora --enable-libvorbis --enable-libmp3lame --enable-gray --enable-libass --enable-libfreetype --enable-libopenjpeg --enable-libspeex --enable-libvo-aacenc --enable-libvo-amrwbenc --enable-version3 --enable-libvpx
  libavutil      52. 89.100 / 52. 89.100
  libavcodec     55. 66.101 / 55. 66.101
  libavformat    55. 43.100 / 55. 43.100
  libavdevice    55. 13.101 / 55. 13.101
  libavfilter     4.  8.100 /  4.  8.100
  libswscale      2.  6.100 /  2.  6.100
  libswresample   0. 19.100 /  0. 19.100
  libpostproc    52.  3.100 / 52.  3.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/home/user/myvideo1.mp4':
  Metadata:
    major_brand     : mp42
    minor_version   : 0
    compatible_brands: isommp42
    creation_time   : 2016-01-16 05:30:03
  Duration: 00:00:11.33, start: 0.000000, bitrate: 4659 kb/s
    Stream #0:0(eng): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 640x480, 4539 kb/s, SAR 65536:65536 DAR 4:3, 30 fps, 30 tbr, 90k tbn, 180k tbc (default)
    Metadata:
      rotate          : 90
      creation_time   : 2016-01-16 05:30:03
      handler_name    : VideoHandle
    Stream #0:1(eng): Audio: aac (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 127 kb/s (default)
    Metadata:
      creation_time   : 2016-01-16 05:30:03
      handler_name    : SoundHandle
Output #0, image2, to '/home/user/Frames/image%d.png':
  Metadata:
    major_brand     : mp42
    minor_version   : 0
    compatible_brands: isommp42
    encoder         : Lavf55.43.100
    Stream #0:0(eng): Video: png, rgb24, 640x480 [SAR 1:1 DAR 4:3], q=2-31, 200 kb/s, 30 fps, 90k tbn, 30 tbc (default)
    Metadata:
      rotate          : 90
      creation_time   : 2016-01-12 05:38:03
      handler_name    : VideoHandle
      encoder         : Lavc55.66.101 png
Stream mapping:
  Stream #0:0 -> #0:0 (h264 (native) -> png (png))
Press [q] to stop, [?] for help
frame=  339 fps= 68 q=0.0 Lsize=N/A time=00:00:11.30 bitrate=N/A    
video:195852kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown

Upvotes: 5

Views: 18834

Answers (4)

Mitchell
Mitchell

Reputation: 71

ON windows 10,

ffmpeg -i <input video file>

The last portion will show fps info. Please check the line beginning with "Stream...."

The following video file is encoded in 60 fps .

C:\user\mitchell\ffmpeg -i video0001.mp4

ffmpeg version 2021-09-22-git-447cf53774-essentials_build-www.gyan.dev Copyright (c) 2000-2021 the FFmpeg developers built with gcc 10.3.0 (Rev5, Built by MSYS2 project) configuration: --enable-gpl --enable-version3 --enable-static --disable-w32threads --disable-autodetect --enable-fontconfig --enable-iconv --enable-gnutls --enable-libxml2 --enable-gmp --enable-lzma --enable-zlib --enable-libsrt --enable-libssh --enable-libzmq --enable-avisynth --enable-sdl2 --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxvid --enable-libaom --enable-libopenjpeg --enable-libvpx --enable-libass --enable-libfreetype --enable-libfribidi --enable-libvidstab --enable-libvmaf --enable-libzimg --enable-amf --enable-cuda-llvm --enable-cuvid --enable-ffnvcodec --enable-nvdec --enable-nvenc --enable-d3d11va --enable-dxva2 --enable-libmfx --enable-libgme --enable-libopenmpt --enable-libopencore-amrwb --enable-libmp3lame --enable-libtheora --enable-libvo-amrwbenc --enable-libgsm --enable-libopencore-amrnb --enable-libopus --enable-libspeex --enable-libvorbis --enable-librubberband libavutil 57. 6.100 / 57. 6.100 libavcodec 59. 9.100 / 59. 9.100 libavformat 59. 5.100 / 59. 5.100 libavdevice 59. 0.101 / 59. 0.101 libavfilter 8. 9.100 / 8. 9.100 libswscale 6. 1.100 / 6. 1.100 libswresample 4. 0.100 / 4. 0.100 libpostproc 56. 0.100 / 56. 0.100 Input #0, matroska,webm, from 'video0001.mp4': Metadata: encoder : GStreamer matroskamux version 1.14.5 creation_time : 2021-09-22T17:52:09.000000Z Duration: 00:00:25.75, start: 2.470000, bitrate: 4256 kb/s

Stream #0:0(eng): Video: hevc (Main), yuvj420p(pc, progressive), 3840x2160, SAR 1:1 DAR 16:9, 60 fps, 60 tbr, 1k tbn (default) Metadata: title : Video

Upvotes: -1

Viswanath Lekshmanan
Viswanath Lekshmanan

Reputation: 10083

What about

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

Upvotes: 4

Zombo
Zombo

Reputation: 1

ffprobe -v 0 -of compact=p=0 -select_streams 0 \
-show_entries stream=r_frame_rate 'The Master (2012).mp4'

Result:

r_frame_rate=24000/1001

Upvotes: 8

martin
martin

Reputation: 96891

You can use ffprobe and awk to grab fps for you:

ffprobe -show_streams "video" 2>&1 | \ 
    grep fps | \
    awk '{split($0,a,"fps")}END{print a[1]}' | \
    awk '{print $NF}'

Some time ago I wrote an article about generating ffmpeg previews from videos which is maybe similar to what you're trying to do. I had to count total number of frames in a video:

See https://www.binpress.com/tutorial/how-to-generate-video-previews-with-ffmpeg/138

Upvotes: 5

Related Questions