Bootstrap

ffmpeg制作视频播放器(五)获取音频,视频信息

#include <iostream>
extern "C"{
#include "libavformat/avformat.h"
}
using namespace std;
#pragma comment(lib,"avformat.lib")
#pragma comment(lib,"avutil.lib")

static double r2d(AVRational r)
{
    return r.den == 0 ? 0:(double)r.num / (double)r.den;
}
int main(int argc, char *argv[])
{
    cout << "Test Demux FFmpeg.club" << endl;
    const char *path = "001111.mp4";
    //初始化封装库
    av_register_all();

    //初始化网络库 (可以打开rtsp rtmp http 协议的流媒体视频)
    avformat_network_init();

    //参数设置
    AVDictionary *opts = NULL;
    //设置rtsp流已tcp协议打开
    av_dict_set(&opts, "rtsp_transport", "tcp", 0);

    //网络延时时间
    av_dict_set(&opts, "max_delay", "500", 0);


    //解封装上下文
    AVFormatContext *ic = NULL;
    int re = avformat_open_input(
        &ic,
        path,
        0,  // 0表示自动选择解封器
        &opts //参数设置,比如rtsp的延时时间
    );
    if (re != 0)
    {
        char buf[1024] = { 0 };
        av_strerror(re, buf, sizeof(buf) - 1);
        cout << "open " << path << " failed! :" << buf << endl;
        getchar();
        return -1;
    }
    cout << "open " << path << " success! " << endl;

    //获取流信息
    re = avformat_find_stream_info(ic, 0);

    //总时长 毫秒
    int totalMs = ic->duration / (AV_TIME_BASE / 1000);
    cout << "totalMs = " << totalMs << endl;

    //打印视频流详细信息
    av_dump_format(ic,0,path,0);

    //音视频索引,读取时区分音视频
    int videoStream = 0;
    int audioStream = 1;

    cout << "streams count: " << ic->nb_streams << endl;
    //获取音视频流信息 (遍历,函数获取)
    for (int i = 0; i < ic->nb_streams; i++){
        AVStream *as = ic->streams[i];

        //音频 AVMEDIA_TYPE_AUDIO
        if (as->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
        {
            audioStream = i;
            cout <<i<< "音频信息" << endl;
            cout << "sample_rate = " << as->codecpar->sample_rate << endl;
//            AVSampleFormat;
            cout << "format = " << as->codecpar->format << endl;
            cout << "channels = " << as->codecpar->channels << endl;
            cout << "codec_id = " << as->codecpar->codec_id << endl;
            cout << "audio fps = " <<r2d(as->avg_frame_rate)<<endl;
            //一帧数据?? 单通道样本数
            cout << "frame_size = " << as->codecpar->frame_size << endl;
            //1024 * 2 * 2 = 4096  fps = sample_rate/frame_size
            cout << "codec_id = " << as->codecpar->codec_id << endl;
            cout << "format = " << as->codecpar->format << endl;
        }
        //视频 AVMEDIA_TYPE_VIDEO
        else if (as->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
        {
            videoStream = i;
            cout << i << "视频信息" << endl;
            cout << "width=" << as->codecpar->width << endl;
            cout << "height=" << as->codecpar->height << endl;
            cout << "format = " << as->codecpar->format << endl;
            //帧率 fps 分数转换
            cout<<"video fps = " <<r2d(as->avg_frame_rate)<<endl;
            cout << "codec_id = " << as->codecpar->codec_id << endl;
            cout << "format = " << as->codecpar->format << endl;
        }

        cout <<"----------------"<<endl;
    }

    if (ic)
    {
        //释放封装上下文,并且把ic置0
        avformat_close_input(&ic);

    }

    getchar();
    return 0;
}

 

假设有一路音频流,有左右两声道的数据。左声道用L表示,右声道用R表示。

音频平面存储:每帧音频数据独立存储

如果是分开存储成二维数组,这种格式称为planar。格式为LLLLLLLLLLLLLL和RRRRRRRRRRRRR

音频普通(交叉)存储:

存储时,如果是左右声道数据交替存储成一维数组,这种格式称为packed。格式为LRLRLR....LRLR

视频帧率(fps)=一秒多少张画面

音频一帧尺寸(frame size):音频单通道一帧数据的大小,也就是单通道的样本数,在这个例子中,一帧数据是1024个样本数。

音频一帧大小:一帧尺寸*通道数*位数

音频帧率(fps):采样率/一帧尺寸= 48000/1024=46

一般一帧画面会有2帧的音频

;