main.cpp

#pragma warning(disable : 4996)
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>       // 字符串操作

#ifdef __cplusplus
    extern "C" 
    {            // ffmpeg 相关头文件
#endif
    #include <libavformat/avformat.h>
#ifdef __cplusplus
    }
#endif

#pragma comment(lib, "avformat.lib")

std::string fileName = "C:\\Users\\Arnold\\source\\repos\\20221120_ffmpeg_test5_rtspToH265Mp4\\video_out.mp4";//文件地址
//std::string fileName = "C:\\Users\\Arnold\\Web\\RecordFiles\\2022-11-22\\192.168.1.67_01_20221122165315123.mp4";//文件地址

int main()
{
    AVFormatContext* fmt_ctx = avformat_alloc_context();//创建对象并初始化
    int ret = 0;

    do {
        //打开文件
        if ((ret = avformat_open_input(&fmt_ctx, fileName.c_str(), NULL, NULL)) < 0)
            break;//Cannot open video file

        //查找流信息(音频流和视频流)
        if ((ret = avformat_find_stream_info(fmt_ctx, NULL)) < 0) {
            printf("Cannot find stream information\n");
            break;
        }

        av_dump_format(fmt_ctx, 0, fileName.c_str(), 0);//输出视频信息
    } while (0);

    avformat_close_input(&fmt_ctx);//关闭文件

    return ret;
}

VS运行结果:

[h264 @ 0000018bcf349600] Stream #0: not enough frames to estimate rate; consider increasing probesize
Input #0, h264, from 'C:\Users\Arnold\source\repos\20221120_ffmpeg_test5_rtspToH265Mp4\video_out.mp4':
  Duration: N/A, bitrate: N/A
    Stream #0:0: Video: h264 (High), yuv420p(progressive), 1920x1080, 25 fps, 25 tbr, 1200k tbn, 50 tbc

可以看出我这个视频有点问题。。。


20230816


文章目录

  • 代码分析: 使用FFmpeg解析视频文件
  • 导入的头文件
  • 文件路径与FFmpeg上下文
  • 打开并解析视频文件
  • 参考资料


代码分析: 使用FFmpeg解析视频文件

本段代码是使用C++编写的,用于打开并解析一个视频文件的基本信息。它依赖于一种名为FFmpeg的库。FFmpeg是一个强大的工具,可用于处理各种类型的媒体文件,包括音频和视频。

android ffmpeg 视频播放进度获取 ffmpeg 查看视频信息_#include

导入的头文件

在代码中,我们首先看到一些头文件被导入:

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>

#ifdef __cplusplus
    extern "C" 
    {            
#endif
    #include <libavformat/avformat.h>
#ifdef __cplusplus
    }
#endif

这些头文件包含了C++标准库中的一些常用功能(如I/O操作、字符串处理等),以及FFmpeg库中的libavformat/avformat.h,这个头文件是用于处理多媒体数据格式的主要组件。

注意extern "C"的使用,这是因为FFmpeg是用C语言编写的,而我们正在使用C++编译器。extern "C"确保C++编译器以C语言的方式处理这些代码。

android ffmpeg 视频播放进度获取 ffmpeg 查看视频信息_ide_02

文件路径与FFmpeg上下文

接下来的部分定义了要处理的视频文件的路径,并初始化了一个AVFormatContext对象:

std::string fileName = "C:\\Users\\Arnold\\source\\repos\\20221120_ffmpeg_test5_rtspToH265Mp4\\video_out.mp4";
AVFormatContext* fmt_ctx = avformat_alloc_context();

fileName存储了文件的完整路径。AVFormatContext是FFmpeg中的一个重要结构,用于存储有关输入或输出媒体文件的所有信息。

android ffmpeg 视频播放进度获取 ffmpeg 查看视频信息_c++_03

打开并解析视频文件

主要的操作在main()函数中完成,其中包括打开视频文件,查找流信息,然后将这些信息打印出来:

int main()
{
    // ... 初始化代码 ...

    do {
        if ((ret = avformat_open_input(&fmt_ctx, fileName.c_str(), NULL, NULL)) < 0)
            break;

        if ((ret = avformat_find_stream_info(fmt_ctx, NULL)) < 0) {
            printf("Cannot find stream information\n");
            break;
        }

        av_dump_format(fmt_ctx, 0, fileName.c_str(), 0);
    } while (0);

    avformat_close_input(&fmt_ctx);

    return ret;
}

avformat_open_input()用于打开输入文件并读取其头部。如果成功,avformat_find_stream_info()则获取文件中的流信息。av_dump_format()函数则打印出输入或输出格式的详细信息,以便于调试。最后,avformat_close_input()关闭输入文件并释放资源。

android ffmpeg 视频播放进度获取 ffmpeg 查看视频信息_ide_04

参考资料

  1. FFmpeg Documentation
  2. Cplusplus.com - Standard Library
  3. Understanding FFmpeg: Part I
  4. The Basics of the FFmpeg C API