我有视频文件。如何在 C++ 中使用 ffmpeg 获取该视频的 fps? 请输入完整代码。
这是我编写的一个简单程序,用于将视频信息转储到控制台:
#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);
}
编译并运行它,输出如下:
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
@zhm 答案非常接近,我做了一个小更新以仅获取帧速率。就我而言,我需要 bit_rate,这是
int64_t
中的 AVFormatContext *
值。
对于 FPS,您需要遍历流列表,可能检查它是音频还是视频,然后访问
r_frame_rate
,这是一个 AVRational
值。参数是分子和分母,您可以简单地将一个除以另一个以获得双精度,他们甚至提供了一个函数(av_q2d()
)来做到这一点。
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;
}
有关更多信息,您可能需要查看定义
avformat.h
和 AVFormatContext
结构的 AVStream
标头。
来自 ffmpeg dump_stream_format 函数的片段
if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
int fps = st->avg_frame_rate.den && st->avg_frame_rate.num;
int tbr = st->r_frame_rate.den && st->r_frame_rate.num;
int tbn = st->time_base.den && st->time_base.num;
if (fps || tbr || tbn)
av_log(NULL, AV_LOG_INFO, "%s", separator);
if (fps)
print_fps(av_q2d(st->avg_frame_rate), tbr || tbn ? "fps, " : "fps");
if (tbr)
print_fps(av_q2d(st->r_frame_rate), tbn ? "tbr, " : "tbr");
if (tbn)
print_fps(1 / av_q2d(st->time_base), "tbn");
}
您可以像这样执行 ffmpeg.exe
ffmpeg -i filename
,如果帧率不可变,它将输出帧率。
示例:
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)
该视频的 fps 为 25。
要执行程序,您可以使用https://stackoverflow.com/a/17703834/58553
中的答案来源:https://askubuntu.com/questions/110264/how-to-find-frames-per-second-of-any-video-file