I have a video encoded in .3gp
h.264
and I am looking to get its framerate and duration in C. Here is the code I use after opening the file and finding the appropriate codecs:
AVRational rational = gVideoCodecCtx->time_base;
LOGI(10, "numerator is %i", rational.num);
LOGI(10, "denominator is %i", rational.den);
LOGI(10, "duration is %d", gFormatCtx->duration);
LOGI(10, "fps is %d", (double)av_q2d(rational));
And here is the output:
12-02 12:30:19.819: I/FFmpegTest(23903): numerator is 1
12-02 12:30:19.819: I/FFmpegTest(23903): denominator is 180000
12-02 12:30:19.819: I/FFmpegTest(23903): duration is 6594490
12-02 12:30:19.819: I/FFmpegTest(23903): fps is 1692926992
From the documentation I understand that the duration is meant to be "duration/time_base" which gives me 6594490 / 180000 = 36.6
. The duration of my video file is 6 seconds
and I do not know where this factor of 6
would come from.
Also the framerate seems to be completely off.
It is currenlty hard to find help as a lot of tutorials use deprecated methods and the documentation does not give examples.
Any help would be appreciated.
Thanks
Edit: Thanks to the comment below I managed to print the following
12-02 18:59:36.279: I/FFmpegTest(435): numerator is 1
12-02 18:59:36.279: I/FFmpegTest(435): denominator is 180000
12-02 18:59:36.279: I/FFmpegTest(435): duration is 6594490
12-02 18:59:36.279: I/FFmpegTest(435): fps is 0.000006
I also managed to find out a frame's timestamp in msec
with this:
int msec = 1000*(packet.pts * timeBase * gVideoCodecCtx->ticks_per_frame);
This returns me something that's roughly 33fps
(I expect 30
). But I can't figure out how to retrieve the duration. The documentation says "duration of the stream, in AV_TIME_BASE
fractional seconds" but 6594490 * 0.000006 = 39.5
- the correct duration is 6.3
seconds). Also the exact fps is 30
but nor sure how to get from 0.000006
to 30
with the above figures)
Thanks
FPS can be obtained this way:
const double FPS = (double)videoStream->r_frame_rate.num / (double)videoStream->r_frame_rate.den;
where videoStream
is:
AVFormatContext * format = NULL;
if ( avformat_open_input( & format, "my_video.mkv", NULL, NULL ) != 0 ) ) on_error();
if ( avformat_find_stream_info( format, NULL ) < 0) on_error();
//av_dump_format( format, 0, "my_video.mkv", false );
AVCodec * video_dec = (AVCodec*)1;
const auto video_stream_index = av_find_best_stream( format, AVMEDIA_TYPE_VIDEO, -1, -1, & video_dec, 0 );
if ( video_stream_index < 0 ) on_error();
const auto videoStream = format->streams[ video_stream_index ];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With