【音视频 ffmpeg 】直播推流QT框架搭建

6 篇文章 0 订阅

在这里插入图片描述
3个线程
一个做视频解码一个做音频解码一个做复用推流

视频解码线程展示

#include "videodecodethread.h"

VideodecodeThread::VideodecodeThread(QObject *parent)
    :QThread(parent)
{
    avdevice_register_all();
    avformat_network_init();
}

void VideodecodeThread::run()
{
    fmt = av_find_input_format("dshow");
    av_dict_set(&options, "video_size",  "640*480", 0);
    av_dict_set(&options, "framerate",  "30", 0);
    ret = avformat_open_input(&pFormatCtx, "video=ov9734_azurewave_camera", fmt, &options);
    if (ret < 0)
    {
        qDebug() << "Couldn't open input stream." << ret;
        return;
    }

    ret = avformat_find_stream_info(pFormatCtx, &options);
    if(ret < 0)
    {
        qDebug()<< "Couldn't find stream information.";
        return;
    }

    videoIndex = av_find_best_stream(pFormatCtx, AVMEDIA_TYPE_VIDEO, -1, -1, &pAvCodec, 0);
    if(videoIndex < 0)
    {
        qDebug()<< "Couldn't av_find_best_stream.";
        return;
    }

    pAvCodec = avcodec_find_decoder(pFormatCtx->streams[videoIndex]->codecpar->codec_id);
    if(!pAvCodec)
    {
        qDebug()<< "Couldn't avcodec_find_decoder.";
        return;
    }
    qDebug()<<"avcodec_open2 pAVCodec->name:" << QString::fromStdString(pAvCodec->name);
    if(pFormatCtx->streams[videoIndex]->avg_frame_rate.den != 0)
    {
            float fps_ = pFormatCtx->streams[videoIndex]->avg_frame_rate.num / pFormatCtx->streams[videoIndex]->avg_frame_rate.den;
            qDebug() <<"fps:" << fps_;
    }
    int64_t video_length_sec_ = pFormatCtx->duration/AV_TIME_BASE;
    qDebug() <<"video_length_sec_:" << video_length_sec_;

    pAvCodecCtx = avcodec_alloc_context3(pAvCodec);
    if(!pAvCodecCtx)
    {
        qDebug()<< "Couldn't avcodec_alloc_context3.";
        return;
    }

    ret = avcodec_parameters_to_context(pAvCodecCtx, pFormatCtx->streams[videoIndex]->codecpar);
    if(ret < 0)
    {
        qDebug()<< "Couldn't avcodec_parameters_to_context.";
        return;
    }

    pAvFrame = av_frame_alloc();
    pAvFrameYUV = av_frame_alloc();

//    pSwsCtx = sws_getContext(pAvCodecCtx->width, pAvCodecCtx->height, pAvCodecCtx->pix_fmt,
//                                          pAvCodecCtx->width, pAvCodecCtx->height, AV_PIX_FMT_YUV420P9,
//                                          SWS_BICUBIC, NULL, NULL, NULL);
    //m_size = av_image_get_buffer_size(AVPixelFormat(AV_PIX_FMT_YUV420P9), pAvCodecCtx->width, pAvCodecCtx->height, 1);
    //为已经分配的空间的结构体AVPicture挂上一段用于保存数据的空间
    //av_image_fill_arrays(pAvFrameYUV->data, pAvFrameYUV->linesize, buffer, AV_PIX_FMT_BGR32, pAvCodecCtx->width, pAvCodecCtx->height, 1);
    av_new_packet(packet, pAvCodecCtx->width * pAvCodecCtx->height);
    while(run_flag && !av_read_frame(pFormatCtx, packet))
    {
        if (packet->stream_index == videoIndex)
        {
            //解码一帧视频数据
            int iGotPic = avcodec_send_packet(pAvCodecCtx, packet);
            if(iGotPic != 0)
            {
                qDebug()<<"iVideoIndex avcodec_send_packet error";
                continue;
            }
            iGotPic = avcodec_receive_frame(pAvCodecCtx, pAvFrame);
            if(iGotPic == 0){
                //转换像素
//                sws_scale(pSwsCtx, (uint8_t const * const *)pAvFrame->data, pAvFrame->linesize, 0,
//                        pAvFrame->height, pAvFrameYUV->data, pAvFrameYUV->linesize);
                //buffer = (uint8_t*)av_malloc(pAvFrame->height * pAvFrame->linesize[0]);
                byte = QByteArray((char*)pAvFrame->data);
                videoQueue.push(byte);
                videoCount++;
            }

        }
        av_packet_unref(packet);
    }
}

ffmpeg 函数解读

在这里插入图片描述
在这里插入图片描述

后面加上音频以及复用推流过程

  • 10
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值