【音视频 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 函数解读

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

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

从引用内容中可以得知,使用ffmpeg可以实现直播推流。首先需要下载ffmpeg和ffplay,并将其与成品软件放置在同一个目录下。ffmpeg是一个跨平台的工具,支持多平台多账号同时推流本地视频,实现无人直播推流的地址统一为rtmp://127.0.0.1:1935/live/123。使用ffmpeg推流的方法与本地视频推流类似,只需将摄像头拍到的画面实时推流出去即可。注意,推流的过程可能会有一定的延迟,并且默认情况下没有声音。如果需要推流麦克风的声音,也可以进行相应的设置。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [利用ffmpeg实现rtmp推流直播](https://blog.csdn.net/gqg_guan/article/details/129144426)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [易语言-ffmpeg直播推流,可实现抖音等全平台无人直播](https://download.csdn.net/download/weixin_38686231/19833828)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值