如何使用C 中的FFMPEG获取视频fps



我有视频。如何在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

为FFMPEG和SDL推荐一个很好的教程。

@zhm答案非常接近,我进行了一个小更新以获取帧速率。最后,我需要bit_rate,这是AVFormatContext *中的int64_t值。

对于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;
}

有关更多信息,您可能需要查看定义AVFormatContextAVStream结构的avformat.h标头。

您可以像以下ffmpeg -i filename一样执行ffmpeg.exe,如果其不变,它将输出帧。

示例: 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/177703834/58553

中使用答案。

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

最新更新