• FFmpeg部分数据结构简介


    FFmpeg部分数据结构简介

    AVFormatContext

    封装格式上下文结构体,也是统称全局的结构体,保存了视频文件封装格式相关信息

    AVInputFormat

    每种封装格式(例如:FLV、MKV、MP4、AVI等)对应一个该结构体

    AVStream

    视频文件中每个视频(音频)流对应一个该结构体

    AVCodecContext

    编码器上下文结构体,保存了视频(音频)编解码相关的信息

    AVCodec

    每种视频(音频)编解码器(例如:H.264解码器)对应一个该结构体。

    AVPacket

    存储一帧压缩编码数据

    AVFrame

    存储一帧解码后像素(采样)数据。

    FFmpeg数据结构分析

    AVFormatContext

    iformat:输入视频的AVInputFormat
    nb_streams:输入视频的AVStream个数
    streams:输入视频的AVStream[]数组
    druation:输入视频的时长(以微秒为单位)
    bit_rate:输入视频码率

    AVInputFormat

    name:封装格式名称
    long_name:封装格式的长名称
    extensions:封装格式的扩展名
    id:封装格式的ID
    一些封装格式处理的接口函数

    AVStream

    id:序号
    codec:该流对应的AVCodecContext
    time_base:该流的时基
    r_frame_rate:该流的帧率
    AVCodecContext
    codec:编解码器的AVCodec
    width,height:图像的宽高(只针对视频)
    pix_fmt:像素格式(只针对视频)
    sample_rae:采样率(只针对音频)
    channels:声道数(只针对音频)
    sample_fmt:采样格式(只针对音频)

    AVCodec

    name:编解码器名称
    long_name:编解码器的长名称
    type:编解码器类型
    id:编解码器ID
    一些编解码的接口函数

    AVPacket

    pts:显示时间戳
    dts:解码时间戳
    data:压缩编码数据
    size:压缩编码数据大小
    stream_index:所属的AVStream

    AVFrame

    data:解码后的图像像素数据(音频采样数据)
    linesize:对视频来说是图像中一行像素的大小;对应音频来说是整个音频帧的大小
    width,height:图像的宽高(只针对视频)
    key_frame:是否为关键帧(只针对视频)
    pict_type:帧类型(值针对视频)。例如:I,P,B
    补充知识
    解码后的数据为什么要经过sws_scale()函数处理?
    解码后YUV格式的视频像素数据保存在AVFrame的data[0]、data[1]、data[2]中。但是这些像素值并不是连续存储的,每行有效像素之后存储了一些无效像素。以亮度Y数据为例,data[0]中一共包含了linesize[0]*height个数据。但是出于优化等方面的考虑,linesize[0]实际上并不等于宽度width,而是一个比宽度大一些的值。因此需要使用sws_scale()进行转换。转换后去除了无效数据,width和linesize[0]取值相等。

    实际去操作代码,可以更加深入了解结构数据位置

    #include 
    #include 
    
    extern "C"
    {
    #include "libavformat/avformat.h"
    #include "libavutil/dict.h"
    };
    
    #pragma comment(lib, "avformat.lib")
    #pragma comment(lib, "avutil.lib")
    #pragma comment(lib, "avcodec.lib")
    
    int main()
    {
    	AVFormatContext *pFormatCtx = NULL;
    	AVCodecContext *pCodecCtx = NULL;
    	AVCodec *pCodec;
    	AVDictionaryEntry *dict = NULL;
    	
    	int iHour, iMinute, iSecond, iTotalSeconds;//HH:MM:SS
    	int videoIndex, audioIndex;
    
    	char *fileName = "bad.mp4";
    	//char *fileName = "Titanic.ts";
    
    	av_register_all();//注册所有组件
    
    	if (avformat_open_input(&pFormatCtx, fileName, NULL, NULL) != 0)//打开输入视频文件
    	{
    		printf("Couldn't open input stream.\n");
    		return -1;
    	}
    
    	if (avformat_find_stream_info(pFormatCtx, NULL) < 0)
    	{
    		printf("Couldn't find stream information.\n");
    		return -1;
    	}
    
    	videoIndex = -1;
    	for (int i = 0; i < pFormatCtx->nb_streams/*视音频流的个数*/; i++)
    	{
    		if (pFormatCtx->streams[i]/*视音频流*/->codec->codec_type == AVMEDIA_TYPE_VIDEO)//查找音频
    		{
    			videoIndex = i;
    			break;
    		}
    	}
    	if (videoIndex == -1)
    	{
    		printf("Couldn't find a video stream.\n");
    		return -1;
    	}
    
    	pCodecCtx = pFormatCtx->streams[videoIndex]->codec;	//指向AVCodecContext的指针
    	pCodec = avcodec_find_decoder(pCodecCtx->codec_id);	//指向AVCodec的指针.查找解码器
    	if (pCodec == NULL)
    	{
    		printf("Codec not found.\n");
    		return -1;
    	}
    	//打开解码器
    	if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0)
    	{
    		printf("Could not open codec.\n");
    		return -1;
    	}
    
    	audioIndex = -1;
    	for (int i = 0; i < pFormatCtx->nb_streams; i++)
    	{
    		if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO)
    		{
    			audioIndex = i;
    			break;
    		}
    	}
    	if (audioIndex == -1)
    	{
    		printf("Couldn't find a audio stream.\n");
    		return -1;
    	}
    
    	
    
    	//打印结构体信息
    
    	puts("AVFormatContext信息:");
    	puts("---------------------------------------------");
    	printf("文件名:%s\n", pFormatCtx->filename);
    	iTotalSeconds = (int)pFormatCtx->duration/*微秒*/ / 1000000;
    	iHour = iTotalSeconds / 3600;//小时
    	iMinute = iTotalSeconds % 3600 / 60;//分钟
    	iSecond = iTotalSeconds % 60;//秒
    	printf("持续时间:%02d:%02d:%02d\n", iHour, iMinute, iSecond);
    	printf("平均混合码率:%d kb/s\n", pFormatCtx->bit_rate / 1000);
    	printf("视音频个数:%d\n", pFormatCtx->nb_streams);
    	puts("---------------------------------------------");
    
    	puts("AVInputFormat信息:");
    	puts("---------------------------------------------");
    	printf("封装格式名称:%s\n", pFormatCtx->iformat->name);
    	printf("封装格式长名称:%s\n", pFormatCtx->iformat->long_name);
    	printf("封装格式扩展名:%s\n", pFormatCtx->iformat->extensions);
    	printf("封装格式ID:%d\n", pFormatCtx->iformat->raw_codec_id);
    	puts("---------------------------------------------");
    
    	puts("AVStream信息:");
    	puts("---------------------------------------------");
    	printf("视频流标识符:%d\n", pFormatCtx->streams[videoIndex]->index);
    	printf("音频流标识符:%d\n", pFormatCtx->streams[audioIndex]->index);
    	printf("视频流长度:%d微秒\n", pFormatCtx->streams[videoIndex]->duration);
    	printf("音频流长度:%d微秒\n", pFormatCtx->streams[audioIndex]->duration);
    	puts("---------------------------------------------");
    
    	puts("AVCodecContext信息:");
    	puts("---------------------------------------------");
    	printf("视频码率:%d kb/s\n", pCodecCtx->bit_rate / 1000);
    	printf("视频大小:%d * %d\n", pCodecCtx->width, pCodecCtx->height);
    	puts("---------------------------------------------");
    
    	puts("AVCodec信息:");
    	puts("---------------------------------------------");
    	printf("视频编码格式:%s\n", pCodec->name);
    	printf("视频编码详细格式:%s\n", pCodec->long_name);
    	puts("---------------------------------------------");
    
    	printf("视频时长:%d微秒\n", pFormatCtx->streams[videoIndex]->duration);
    	printf("音频时长:%d微秒\n", pFormatCtx->streams[audioIndex]->duration);
    	printf("音频采样率:%d\n", pFormatCtx->streams[audioIndex]->codec->sample_rate);
    	printf("音频信道数目:%d\n", pFormatCtx->streams[audioIndex]->codec->channels);
    
    	puts("AVFormatContext元数据:");
    	puts("---------------------------------------------");
    	while (dict = av_dict_get(pFormatCtx->metadata, "", dict, AV_DICT_IGNORE_SUFFIX))
    	{
    		printf("[%s] = %s\n", dict->key, dict->value);
    	}
    	puts("---------------------------------------------");
    
    	puts("AVStream视频元数据:");
    	puts("---------------------------------------------");
    	dict = NULL;
    	while (dict = av_dict_get(pFormatCtx->streams[videoIndex]->metadata, "", dict, AV_DICT_IGNORE_SUFFIX))
    	{
    		printf("[%s] = %s\n", dict->key, dict->value);
    	}
    	puts("---------------------------------------------");
    
    	puts("AVStream音频元数据:");
    	puts("---------------------------------------------");
    	dict = NULL;
    	while (dict = av_dict_get(pFormatCtx->streams[audioIndex]->metadata, "", dict, AV_DICT_IGNORE_SUFFIX))
    	{
    		printf("[%s] = %s\n", dict->key, dict->value);
    	}
    	puts("---------------------------------------------");
    
    
    	av_dump_format(pFormatCtx, -1, fileName, 0);
    	printf("\n\n编译信息:\n%s\n\n", avcodec_configuration());
    
    
    	avcodec_close(pCodecCtx);
    	avformat_close_input(&pFormatCtx);
    	return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
  • 相关阅读:
    Spring Cloud Stream函数式编程整合消息中间件
    Servlet的生命周期
    网络安全--APT技术、密码学
    LeetCode 热题 HOT 100 第八十天 494. 目标和 中等题 用python3求解
    bp盐丘模型波场数值模拟matlab
    【仿真建模】第四课:AnyLogic入门基础课程 - 轨道交通仿真入门讲解
    将表情存入数据库
    Docker清理
    Vue项目中组件如何使用
    SpringBoot SpringBoot 开发实用篇 5 整合第三方技术 5.7 memcached 下载与安装
  • 原文地址:https://blog.csdn.net/u012836015/article/details/136228813