1.解封装

解封装代码

#include <iostream>
#include <thread>
extern "C" {
#include "libavutil/avutil.h"
#include "libavcodec/avcodec.h "
#include "libavformat/avformat.h"
}

#pragma comment(lib, "avcodec.lib")
#pragma comment(lib, "avutil.lib")
#pragma comment(lib, "avformat.lib")
#pragma warning(disable:4996)
using namespace std;

//线程sleep c++11
void Xsleep(int ms)
{
    //c++11支持
    chrono::milliseconds du(ms);
    this_thread::sleep_for(du);
}

//防止分母是0 挂掉
static double r2d(AVRational in)
{
    return (in.den == 0) ? 0 : ((double)in.num / (double)in.den);
}

int main()
{
    //avcodec_configuration();
    av_register_all();
    avformat_network_init();
    const char *path = "zxy10.mp4";
    AVFormatContext *ic = NULL;
    int ret = avformat_open_input(&ic, path, NULL, NULL);
    if (ret != 0)
    {
        char buf[1024] = { 0 };
        av_strerror(ret, buf, sizeof(buf) - 1);
        av_log(NULL, AV_LOG_ERROR, "open file erro %s\n:", buf);
        getchar();
        return -1;
    }
    cout << "open file :" << ic->filename << " success" << endl;

    avformat_find_stream_info(ic, NULL);

    cout << "file time is " << ic->duration / AV_TIME_BASE << endl;

    av_dump_format(ic, 0, path, 0);


    int audioStream = 0;  //记录音频和视频的编号
    int videostream = 0;
#if 0

    {
        //获取音视频
        for (int i = 0; i < ic->nb_streams; i++)
        {
            AVStream *as = ic->streams[i];

            if (as->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)        //视屏
            {
                videostream = i;
                cout << "视频信息 stream :" << videostream << endl;
                cout << "height :" << as->codecpar->height << endl;
                cout << "weith :" << as->codecpar->width << endl;
                cout << "codec_id :" << as->codecpar->codec_id << endl;
                cout << "format :" << as->codecpar->format << endl;
                cout << "frame_size :" << as->codecpar->frame_size << endl;
                cout << "fps : " << r2d(as->avg_frame_rate) << endl;
                //AVPixelFormat;
            }
            else if (as->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)//音频
            {
                audioStream = i;
                cout << endl;
                cout << "音频信息,stream " << audioStream << endl;
                cout << "codec_id :" << as->codecpar->codec_id << endl;
                cout << "channels :" << as->codecpar->channels << endl;
                cout << "sample_rate :" << as->codecpar->sample_rate << endl;
                cout << "format :" << as->codecpar->format << endl;
                cout << "frame_size :" << as->codecpar->frame_size << endl;
                cout << "fps : " << r2d(as->avg_frame_rate) << endl;
                //AVSampleFormat;
            }
        }
    }

#endif
    //下面方法可以替换上面的
    videostream = av_find_best_stream(ic, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);
    if (videostream < 0)
    {
        char buf[1024] = { 0 };
        av_strerror(videostream, buf, sizeof(buf) - 1);
        av_log(NULL, AV_LOG_ERROR, "Not find Video Stream : %s\n:", buf);
        if (ic)
        {
            avformat_close_input(&ic);
        }
        getchar();
        return -1;
    }
    else
    {
        cout << endl;
        cout << "视频信息 stream :" << videostream << endl;
        cout << "height :" << ic->streams[videostream]->codecpar->height << endl;
        cout << "weith :" << ic->streams[videostream]->codecpar->width << endl;
        cout << "codec_id :" << ic->streams[videostream]->codecpar->codec_id << endl;
        cout << "format :" << ic->streams[videostream]->codecpar->format << endl;
        cout << "frame_size :" << ic->streams[videostream]->codecpar->frame_size << endl;
        cout << "fps : " << r2d(ic->streams[videostream]->avg_frame_rate) << endl;
        //AVPixelFormat;
    }

    audioStream = av_find_best_stream(ic, AVMEDIA_TYPE_AUDIO, -1, -1, NULL, 0);
    if (audioStream < 0)
    {
        char buf[1024] = { 0 };
        av_strerror(audioStream, buf, sizeof(buf) - 1);
        av_log(NULL, AV_LOG_ERROR, "Not find Audio Stream : %s\n:", buf);
        if (ic)
        {
            avformat_close_input(&ic);
        }
        getchar();
        return -1;
    }
    else
    {
        cout << endl;
        cout << "音频信息,stream " << audioStream << endl;
        cout << "codec_id :" << ic->streams[audioStream]->codecpar->codec_id << endl;
        cout << "channels :" << ic->streams[audioStream]->codecpar->channels << endl;
        cout << "sample_rate :" << ic->streams[audioStream]->codecpar->sample_rate << endl;
        cout << "format :" << ic->streams[audioStream]->codecpar->format << endl;
        cout << "frame_size :" << ic->streams[audioStream]->codecpar->frame_size << endl;
        cout << "fps : " << r2d(ic->streams[audioStream]->avg_frame_rate) << endl;
    }
    AVPacket * pkt = av_packet_alloc();
    for (;;)
    {
        int ret = av_read_frame(ic, pkt);
        if (ret != 0)  //有可能是文件读完了;也有可能是出错了
        {
            //{
            //    //seek到3s的位置
            //    int ms = 3000;
            //    long long pos = (double)ms / (double)(r2d(ic->streams[pkt->stream_index]->time_base) * 1000);
            //    av_seek_frame(ic, videostream, pos, AVSEEK_FLAG_BACKWARD | AVSEEK_FLAG_FRAME);
            //}


            break; //失败不需要怎么样,直接跳出循环
        }
        {
            cout << "pkt->size : " << pkt->size << endl;
            //显示的时间
            cout << " pkt->pts :" << pkt->pts << endl;
            //显示的ms
            cout << (double)r2d(ic->streams[pkt->stream_index]->time_base) << endl;
            cout << "pkt->pts ms :" << (pkt->pts) * ((double)r2d(ic->streams[pkt->stream_index]->time_base) * 1000) << endl;
            cout << "pkt->dts :" << pkt->dts << endl;
            if (pkt->stream_index == audioStream)
            {
                cout << "音频:" << endl;
            }
            else if (pkt->stream_index == videostream)
            {
                cout << "图片:" << endl;
            }
        }
        //释放引用计数;为零 清空pkt.buff 指向的空间
        av_packet_unref(pkt);
        Xsleep(500); //sleep 500ms
    }
    av_packet_free(&pkt);

    if (ic)
    {
        avformat_close_input(&ic);
    }
#ifdef _WIN32
    //cout << "windows app" << endl;
#ifdef _WIN64
    //cout << "win64" << endl;
#else
    //cout << "win32" << endl;
#endif // _WIN64
#else
    //cout << " linux" << endl;

#endif //_WIN32


    cin.get();
    return 0;

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值