美文网首页
【Qt】使用ffmpeg加载视频第一帧作为图标

【Qt】使用ffmpeg加载视频第一帧作为图标

作者: 怒放的脚毛 | 来源:发表于2021-01-06 14:15 被阅读0次

    环境说明

    我这里使用的是Qt5环境,当然你可以使用其他环境,原理应该都差不多的。

    导入ffmpeg库

    首先在Qt的工程文件,就是.pro格式的那个,打开,然后加入下面代码,我这里对windows和mac系统做了一下兼容,你选择其中一个即可,如果是windows系统,那你得改一下INCLUDEPATH 路径为你存放ffmpeg的路径。

    win32 {
        INCLUDEPATH += "F:\\cpppath\\ffmpeg-win64-dev\\include"
        LIBS += -LF:\cpppath\ffmpeg-win64-dev\lib -lavutil -lavformat -lavcodec -lavfilter -lswscale -lswresample -lopengl32 -luser32
    }
    
    mac {
        LIBS += -L/usr/local/lib -lavutil -lavformat -lavcodec -lavfilter -lswscale -lswresample -framework CoreFoundation
        INCLUDEPATH = /usr/local/include
    }
    

    流程分析

    大概流程是这样的:

    1. 使用ffmpeg打开视频文件
    2. 读取视频第一帧数据
    3. 对视频帧进行解码,得到图片原始数据
    4. 对原始数据进行加工处理,转换成我们需要的图片对象

    关键代码

    void MainWindow::createPreviewWidthFile(const char *file){
        AVFormatContext* fmt_ctx_ = nullptr;
    
        //打开视频文件
        int errCode = avformat_open_input(&fmt_ctx_, file, nullptr, nullptr);
        if(errCode != 0){
            qDebug() << "avformat_open_input fail" << errCode;
            return;
        }
    
        //读取音视频流信息
        errCode = avformat_find_stream_info(fmt_ctx_, nullptr);
        if(errCode != 0){
            qDebug() << "avformat_find_stream_info fail" << errCode;
            avformat_close_input(&fmt_ctx_);
            return;
        }
        //打印输出视频相关信息
        av_dump_format(fmt_ctx_, 0, file, 0);
    
        AVPacket* pkt = av_packet_alloc();
        AVFrame* temp_frame = av_frame_alloc();
        SwsContext* sws_ctx = nullptr;
        int ret = 0;
        QImage preview;
        bool preview_done = false;
    
        for (int i=0; i<int(fmt_ctx_->nb_streams) && !preview_done; i++){
            //只处理视频信息
            if (fmt_ctx_->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
                //查找视频解码器
                AVCodec* codec = avcodec_find_decoder(fmt_ctx_->streams[i]->codecpar->codec_id);
                AVCodecContext *codec_ctx = avcodec_alloc_context3(codec);
                //根据提供的编解码器参数的值填充编解码器上下文
                avcodec_parameters_to_context(codec_ctx, fmt_ctx_->streams[i]->codecpar);
                //打开解码器
                avcodec_open2(codec_ctx, codec, nullptr);
    
                //读取帧数据
                while (av_read_frame(fmt_ctx_, pkt) >= 0){
                    av_frame_unref(temp_frame);
    
                    //对视频帧数据进行解码
                    while ((ret = avcodec_receive_frame(codec_ctx, temp_frame)) == AVERROR(EAGAIN)){
                        ret = avcodec_send_packet(codec_ctx, pkt);
                        if (ret < 0) {
                            qCritical() << "Failed to send packet to decoder." << ret;
                            break;
                        }
                    }
    
                    if(ret < 0 && ret != AVERROR_EOF){
                        qDebug() << "Failed to receive packet from decoder." << ret;
                        continue;
                    }
    
                    //等比例缩放
                    int dstH = 240;
                    int dstW = qRound(dstH * (float(temp_frame->width)/float(temp_frame->height)));
                    //消除可能的告警
                    dstH = (dstH >> 4) << 4;
                    dstW = (dstW >> 4) << 4;
    
                    sws_ctx = sws_getContext(
                                            temp_frame->width,
                                            temp_frame->height,
                                            static_cast<AVPixelFormat>(temp_frame->format),
                                            dstW,
                                            dstH,
                                            static_cast<AVPixelFormat>(AV_PIX_FMT_RGBA),
                                            SWS_FAST_BILINEAR,
                                            nullptr,
                                            nullptr,
                                            nullptr
                                            );
                    int linesize[AV_NUM_DATA_POINTERS];
                    linesize[0] = dstW*4;
    
                    //生成图片
                    preview = QImage(dstW, dstH, QImage::Format_RGBA8888);
                    uint8_t* data = preview.bits();
                    sws_scale(sws_ctx,
                              temp_frame->data,
                              temp_frame->linesize,
                              0,
                              temp_frame->height,
                              &data,
                              linesize);
                    sws_freeContext(sws_ctx);
    
                    avcodec_close(codec_ctx);
                    avcodec_free_context(&codec_ctx);
                    preview_done = true;
                    break;
                }
            }
        }
        //释放资源
        av_frame_free(&temp_frame);
        av_packet_free(&pkt);
        avformat_close_input(&fmt_ctx_);
        
        //使用该图片,贴图到textlabel
        if(preview_done){
            ui->label->setPixmap(QPixmap::fromImage(preview));
        }
    }
    
    

    该代码只是完成了一个基本的功能,而且可能并不完善,具体使用需要自己完善了。

    效果:


    在这里插入图片描述

    完整代码

    相关文章

      网友评论

          本文标题:【Qt】使用ffmpeg加载视频第一帧作为图标

          本文链接:https://www.haomeiwen.com/subject/jkbnoktx.html