ffmpeg制作视频播放器(七)音视频解码器上下文创建配置打开

#include <iostream>
extern "C"{
#include "libavformat/avformat.h"
}
using namespace std;
#pragma comment(lib,"avformat.lib")
#pragma comment(lib,"avutil.lib")
#pragma comment(lib,"avcodec.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;
    }


    //获取视频流
    videoStream = av_find_best_stream(ic, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);

    //
    ///视频解码器打开
    ///找到视频解码器
    AVCodec *vcodec = avcodec_find_decoder(ic->streams[videoStream]->codecpar->codec_id);
    if (!vcodec)
    {
        cout << "can't find the codec id " << ic->streams[videoStream]->codecpar->codec_id;
        getchar();
        return -1;
    }
    cout << "find the AVCodec " << ic->streams[videoStream]->codecpar->codec_id << endl;

    AVCodecContext *vc = avcodec_alloc_context3(vcodec);

    ///配置解码器上下文参数
    avcodec_parameters_to_context(vc, ic->streams[videoStream]->codecpar);
    //八线程解码
    vc->thread_count = 8;

    ///打开解码器上下文
    re = avcodec_open2(vc, 0, 0);
    if (re != 0)
    {
        char buf[1024] = { 0 };
        av_strerror(re, buf, sizeof(buf) - 1);
        cout << "avcodec_open2  failed! :" << buf << endl;
        getchar();
        return -1;
    }
    cout << "video avcodec_open2 success!" << endl;

    ///音频解码器打开
    AVCodec *acodec = avcodec_find_decoder(ic->streams[audioStream]->codecpar->codec_id);
    if (!acodec)
    {
        cout << "can't find the codec id " << ic->streams[audioStream]->codecpar->codec_id;
        getchar();
        return -1;
    }
    cout << "find the AVCodec " << ic->streams[audioStream]->codecpar->codec_id << endl;
    ///创建解码器上下文呢
    AVCodecContext *ac = avcodec_alloc_context3(acodec);

    ///配置解码器上下文参数
    avcodec_parameters_to_context(ac, ic->streams[audioStream]->codecpar);
    //八线程解码
    ac->thread_count = 8;

    ///打开解码器上下文
    re = avcodec_open2(ac, 0, 0);
    if (re != 0)
    {
        char buf[1024] = { 0 };
        av_strerror(re, buf, sizeof(buf) - 1);
        cout << "avcodec_open2  failed! :" << buf << endl;
        getchar();
        return -1;
    }
    cout << "audio avcodec_open2 success!" << endl;


    ///ic->streams[videoStream]
    //malloc AVPacket并初始化
    AVPacket *pkt = av_packet_alloc();

//    for (;;){
//        int re = av_read_frame(ic, pkt);
//        if(re!=0){
//            //循环播放
//            cout << "==============================end==============================" << endl;
//            int ms = 1000; //三秒位置 根据时间基数(分数)转换
//            long long pos = (double)ms/ (double)1000/r2d(ic->streams[pkt->stream_index]->time_base);
//            cout << "pos" << pos << endl;
//            cout << "num" << ic->streams[pkt->stream_index]->time_base.num;
//            cout << "den" << ic->streams[pkt->stream_index]->time_base.den;
//            av_seek_frame(ic, videoStream, pos, AVSEEK_FLAG_BACKWARD | AVSEEK_FLAG_FRAME);
//            continue;
//        }

//        cout << "pkt->size = "<< pkt->size << endl;
//        //显示的时间
//        cout << "pkt->pts = " << pkt->pts << endl;

//        //转换为毫秒,方便做同步
//        cout << "pkt->pts ms = " << pkt->pts * (r2d(ic->streams[pkt->stream_index]->time_base)*1000) << endl;


//        //解码时间
//        cout << "pkt->dts = " << pkt->dts << endl;
//        if (pkt->stream_index == videoStream)
//        {
//            cout << "图像" << endl;
//        }
//        if (pkt->stream_index == audioStream)
//        {
//            cout << "音频" << endl;
//        }


//        //释放,引用计数-1 为0释放空间
//        av_packet_unref(pkt);
//    }

//    av_packet_free(&pkt);

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

    getchar();
    return 0;
}

 

你好!对于使用Qt和FFmpeg开发音视频播放器,你可以按照以下步骤进行: 1. 下载和安装FFmpeg库:你可以从FFmpeg官方网站下载预编译的库文件,根据你的操作系统选择相应的版本并进行安装。 2. 创建Qt项目:打开Qt Creator,创建一个新的Qt Widgets或Qt Quick应用程序项目。 3. 配置项目文件(.pro):在项目文件中添加FFmpeg库的路径和链接选项。例如,你可以添加以下内容: ``` INCLUDEPATH += /path/to/ffmpeg/include LIBS += -L/path/to/ffmpeg/lib -lavformat -lavcodec -lavutil -lswscale ``` 确保将`/path/to/ffmpeg`替换为你的FFmpeg库的实际路径。 4. 创建音视频播放器界面:使用Qt Designer创建一个用户界面,包含播放按钮、进度条、音量控制等。 5. 编写播放器逻辑:在Qt代码中使用FFmpeg库进行音视频解码和播放。你需要了解如何使用FFmpeg的API来打开音视频文件、读取音视频帧、解码帧数据,并将解码后的数据渲染到屏幕上。 示例代码: ```c++ // 引入FFmpeg头文件 extern "C" { #include <libavformat/avformat.h> #include <libswscale/swscale.h> #include <libavcodec/avcodec.h> } // 初始化FFmpeg库 av_register_all(); // 打开音视频文件 AVFormatContext* formatContext = avformat_alloc_context(); if (avformat_open_input(&formatContext, "/path/to/video.mp4", nullptr, nullptr) < 0) { // 打开文件失败 return; } // 查找音视频流信息 if (avformat_find_stream_info(formatContext, nullptr) < 0) { // 获取流信息失败 avformat_close_input(&formatContext); return; } // 查找视频流索引 int videoStreamIndex = -1; for (int i = 0; i < formatContext->nb_streams; i++) { if (formatContext->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { videoStreamIndex = i; break; } } if (videoStreamIndex == -1) { // 没有找到视频流 avformat_close_input(&formatContext); return; } // 获取视频解码器参数 AVCodecParameters* codecParameters = formatContext->streams[videoStreamIndex]->codecpar; // 查找视频解码器 AVCodec* codec = avcodec_find_decoder(codecParameters->codec_id); if (!codec) { // 没有找到解码器 avformat_close_input(&formatContext); return; } // 创建解码器上下文 AVCodecContext* codecContext = avcodec_alloc_context3(codec); if (!codecContext) { // 解码器上下文创建失败 avformat_close_input(&formatContext); return; } // 配置解码器上下文参数 if (avcodec_parameters_to_context(codecContext, codecParameters) < 0) { // 配置解码器上下文参数失败 avcodec_free_context(&codecContext); avformat_close_input(&formatContext); return; } // 打开解码器 if (avcodec_open2(codecContext, codec, nullptr) < 0) { // 打开解码器失败 avcodec_free_context(&codecContext); avformat_close_input(&formatContext); return; } // 读取视频帧并渲染 AVPacket packet; while (av_read_frame(formatContext, &packet) >= 0) { if (packet.stream_index == videoStreamIndex) { // 解码视频帧 AVFrame* frame = av_frame_alloc(); int ret = avcodec_send_packet(codecContext, &packet); if (ret >= 0) { ret = avcodec_receive_frame(codecContext, frame); if (ret >= 0) { // 渲染视频帧 // ... } } av_frame_free(&frame); } av_packet_unref(&packet); } // 清理资源 avcodec_free_context(&codecContext); avformat_close_input(&formatContext); ``` 这只是一个简单的示例,实际的音视频播放器可能需要更多功能和错误处理。你可以根据你的需求进行适当的调整和扩展。 希望这些信息能对你有所帮助!如果你有任何进一步的问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值