简体   繁体   English

如何在 C++ 中使用 ffmpeg 获取视频的 fps?

[英]How to get fps for video with ffmpeg in c++?

I have videofile.我有视频文件。 How can I get fps for this video with ffmpeg in c++?如何在 C++ 中使用 ffmpeg 获得此视频的 fps? Type full code, please.请输入完整代码。

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 .推荐一个非常好的ffmpeg和SDL教程

@zhm answer was very close, I've made a small update to get the frame rate only. @zhm 的回答非常接近,我做了一个小更新以仅获取帧速率。 On my end, I need the bit_rate and this is an int64_t value in the AVFormatContext * .最后,我需要 bit_rate,这是AVFormatContext *int64_t值。

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.对于 FPS,您需要查看流列表,大概检查它是音频还是视频,然后访问r_frame_rate ,这是一个AVRational值。 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.参数是一个分母和分母,你可以简单地将一个除以另一个得到一个双av_q2d() ,他们甚至提供了一个函数( 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;
}

For more information, you may want to take a look at the avformat.h header where the AVFormatContext and AVStream structures are defined.有关更多信息,您可能需要查看avformat.h标头,其中定义了AVFormatContextAVStream结构。

You could execute ffmpeg.exe like this ffmpeg -i filename and it would output the framerate if its not variable.您可以像这样ffmpeg -i filename执行 ffmpeg.exe 并且如果它不是可变的,它将输出帧率。

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)示例: 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.此视频的 fps 为 25。

To execute a program you can use the answer in https://stackoverflow.com/a/17703834/58553要执行程序,您可以使用https://stackoverflow.com/a/17703834/58553 中的答案

Source: https://askubuntu.com/questions/110264/how-to-find-frames-per-second-of-any-video-file来源: https : //askubuntu.com/questions/110264/how-to-find-frames-per-second-of-any-video-file

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM