I have videofile. How can I get fps for this video with ffmpeg in c++? Type full code, please.
-
7Not a code writing service; attempt the task first, post your issues (with examples of what you've tried already) – ocelot Mar 10 '17 at 12:45
-
2You should try by yourself then ask your question with the code you did. We won't do all the job for you. – Gabrielle de Grimouard Mar 10 '17 at 12:45
3 Answers
This is a simple program I wrote to dump video information to console:
#include <libavformat/avformat.h>
int main(int argc, const char *argv[])
{
if (argc < 2)
{
printf("No video file.\n");
return -1;
}
av_register_all();
AVFormatContext *pFormatCtx = NULL;
//open video file
if (avformat_open_input(&pFormatCtx, argv[1], NULL, NULL) != 0)
return -1;
//get stream info
if (avformat_find_stream_info(pFormatCtx, NULL) < 0)
return -1;
av_dump_format(pFormatCtx, 0, argv[1], 0);
}
Compile and run it, output looks like:
s@ubuntu-vm:~/Desktop/video-info-dump$ ./vdump a.mp4
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'a.mp4':
Metadata:
major_brand : isom
minor_version : 1
compatible_brands: isom
creation_time : 2014-04-23 06:18:02
encoder : FormatFactory : www.pcfreetime.com
Duration: 00:07:20.60, start: 0.000000, bitrate: 1354 kb/s
Stream #0:0(und): Video: mpeg4 (Simple Profile) (mp4v / 0x7634706D), yuv420p, 640x480 [SAR 1:1 DAR 4:3], 1228 kb/s, 24 fps, 24 tbr, 24k tbn, 24 tbc (default)
Metadata:
creation_time : 2014-04-23 06:18:02
handler_name : video
Stream #0:1(und): Audio: aac (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 123 kb/s (default)
Metadata:
creation_time : 2014-04-23 06:18:25
handler_name : sound
Recommend a very good tutorial for ffmpeg and SDL.

- 3,513
- 3
- 34
- 55
-
Thank you, but I thought there is the function (f(string filename) ->double) allows get this information. – Konstantin Dedov Mar 11 '17 at 15:02
@zhm answer was very close, I've made a small update to get the frame rate only. On my end, I need the bit_rate and this is an int64_t
value in the AVFormatContext *
.
For the FPS, you need to go through the list of streams, probably check whether it's audio or video, and then access the r_frame_rate
, which is an AVRational
value. The parameter is a nominator and denominator, you can simple divide one by the other to get a double and they even offer a function (av_q2d()
) to do it.
int main(int argc, char * argv[])
{
if (argc < 2)
{
printf("No video file.\n");
return -1;
}
av_register_all();
AVFormatContext *pFormatCtx = NULL;
//open video file
if (avformat_open_input(&pFormatCtx, argv[1], NULL, NULL) != 0)
return -1;
//get stream info
if (avformat_find_stream_info(pFormatCtx, NULL) < 0)
return -1;
// dump the whole thing like ffprobe does
//av_dump_format(pFormatCtx, 0, argv[1], 0);
// get the frame rate of each stream
for(int idx(0); idx < pFormatCtx->nb_streams; ++idx)
{
AVStream *s(pFormatCtx->streams[idx]);
std::cout << idx << ". " << s->r_frame_rate.nom
<< " / " << s->r_frame_rate.den
<< " = " << av_q2d(s->r_frame_rate)
<< "\n";
}
// get the video bit rate
std::cout << "bit rate " << pFormatCtx->bit_rate << "\n";
return 0;
}
For more information, you may want to take a look at the avformat.h
header where the AVFormatContext
and AVStream
structures are defined.

- 19,179
- 10
- 84
- 156
You could execute ffmpeg.exe like this ffmpeg -i filename
and it would output the framerate if its not variable.
Example:
Input #0, matroska,webm, from 'somerandom.mkv':
Duration: 01:16:10.90, start: 0.000000, bitrate: N/A
Stream #0.0: Video: h264 (High), yuv420p, 720x344 [PAR 1:1 DAR 90:43], 25 fps, 25 tbr, 1k tbn, 50 tbc (default)
Stream #0.1: Audio: aac, 48000 Hz, stereo, s16 (default)
This video has a fps of 25.
To execute a program you can use the answer in https://stackoverflow.com/a/17703834/58553
Source: https://askubuntu.com/questions/110264/how-to-find-frames-per-second-of-any-video-file
-
1Did you read the question ? The question is about to do it in C++. Not with command line. – Gabrielle de Grimouard Mar 10 '17 at 12:50
-
@GabrieldeGrimouard so you cant execute a program from C++, we both know you can and why on earth would that not be a valid solution? (sure its not the best but its a solution) – Peter Mar 10 '17 at 12:51
-
That's an answer which are not related to the question. If he want to make a cross compiled application on windows, linux, mac, it will be realy hard to do. Moreover, send a command like that in your code it a huge security problem. So no your answer is not good as 1/ don't answer the question, 2/ if it does bring huge problems in the code. – Gabrielle de Grimouard Mar 10 '17 at 12:57
-
@GabrieldeGrimouard sure it answers the question, 1 execute the application 2. parse the result, tada your done. yes it has drawbacks but nevertheless its a answer and it does solve the problem. We don't know how he is going to use this 1. we don't know if he wants it to be cross platform. 2. we don't know if this is a one time thing where security might not matter? – Peter Mar 10 '17 at 13:28
-
if you really wnat to answer the question, you should write for him how to call it in the c++. else the answer is not in c++. If you do that I will give you the point. – Gabrielle de Grimouard Mar 10 '17 at 13:30