libavcodec获取视频时长和帧速率

问题描述 投票:5回答:2

我有一个用.3gp h.264编码的视频,我希望在C中获得其帧速率和持续时间。这是在打开文件并找到适当的编解码器后使用的代码:

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));

这是输出:

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

从文档中我了解到,持续时间是“ duration / time_base”,这给了我6594490 / 180000 = 36.6。我的视频文件的持续时间为6 seconds,我不知道6的这个因素来自哪里。

而且帧率似乎已完全关闭。

很难找到帮助,因为许多教程都使用不推荐使用的方法,并且文档中没有提供示例。

任何帮助将不胜感激。

谢谢

编辑:由于下面的评论,我设法打印出以下内容

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

我还设法在msec中找出帧的时间戳:

int msec = 1000*(packet.pts * timeBase * gVideoCodecCtx->ticks_per_frame);

这会给我返回大致33fps的值(我希望是30)。但是我不知道如何获取持续时间。该文档说“流的持续时间,以AV_TIME_BASE小数秒为单位”,但6594490 * 0.000006 = 39.5-正确的持续时间为6.3秒)。同样,精确的fps是30,但也不确定如何根据上述数字从0.000006转换为30

谢谢

video ffmpeg duration frame-rate libavcodec
2个回答
0
投票

可以通过这种方式获得FPS:

const double FPS = (double)videoStream->r_frame_rate.num / (double)videoStream->r_frame_rate.den;

videoStream在哪里:

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 ];

-1
投票

您的fps打印是垃圾,因为它应该是%lf而不是%d。为什么不同时检查其他参数类型。

© www.soinside.com 2019 - 2024. All rights reserved.