opencv 采集图像ffmpeg推流(rtmp)到rtmp服务器上

13 篇文章 1 订阅

前提是配置好nginx+rtmp服务器配置


#include <opencv2/highgui.hpp>
#include <iostream>
extern "C"
{
#include <libswscale/swscale.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
}
#pragma comment(lib, "swscale.lib")
#pragma comment(lib, "avcodec.lib")
#pragma comment(lib, "avutil.lib")
#pragma comment(lib, "avformat.lib")
//#pragma comment(lib,"opencv_world300.lib")
using namespace std;
using namespace cv;
int main(int argc, char *argv[])
{
    //海康相机的rtsp url
    char *inUrl = "rtsp://test:test123456@192.168.1.64";
    //nginx-rtmp 直播服务器rtmp推流URL
    char *outUrl = "rtmp://192.168.1.101/live";

    //注册所有的编解码器
    avcodec_register_all();

    //注册所有的封装器
    av_register_all();

    //注册所有网络协议
    avformat_network_init();


    VideoCapture cam;
    Mat frame;
    namedWindow("video");

    //像素格式转换上下文
    SwsContext *vsc = NULL;

    //输出的数据结构
    AVFrame *yuv = NULL;

    //编码器上下文
    AVCodecContext *vc = NULL;

    //rtmp flv 封装器                                                           
    AVFormatContext *ic = NULL;


    try
    {   ////////////////////////////////////////////////////////////////
        /// 1 使用opencv打开rtsp相机
        cam.open(inUrl);
        //cam.open(0); // 本地相机
        if (!cam.isOpened())
        {
            throw exception("cam open failed!");
        }
        cout << inUrl << " cam open success" << endl;
        int inWidth = cam.get(CAP_PROP_FRAME_WIDTH);
        int inHeight = cam.get(CAP_PROP_FRAME_HEIGHT);
        int fps = cam.get(CAP_PROP_FPS);
        fps = 25;

        ///2 初始化格式转换上下文
        vsc = sws_getCachedContext(vsc,
            inWidth, inHeight, AV_PIX_FMT_BGR24,     //源宽、高、像素格式
            inWidth, inHeight, AV_PIX_FMT_YUV420P,//目标宽、高、像素格式
            SWS_BICUBIC,  // 尺寸变化使用算法
            0, 0, 0
        );
        if (!vsc)
        {
            throw exception("sws_getCachedContext failed!");
        }
        ///3 初始化输出的数据结构
        yuv = av_frame_alloc();
        yuv->format = AV_PIX_FMT_YUV420P;
        yuv->width = inWidth;
        yuv->height = inHeight;
        yuv->pts = 0;
        //分配yuv空间
        int ret = av_frame_get_buffer(yuv, 32);
        if (ret != 0)
        {
            char buf[1024] = { 0 };
            av_strerror(ret, buf, sizeof(buf) - 1);
            throw exception(buf);
        }

        ///4 初始化编码上下文
        //a 找到编码器
        AVCodec *codec = avcodec_find_encoder(AV_CODEC_ID_H264);
        if (!codec)
        {
            throw exception("Can`t find h264 encoder!");
        }
        //b 创建编码器上下文
        vc = avcodec_alloc_context3(codec);
        if (!vc)
        {
            throw exception("avcodec_alloc_context3 failed!");
        }
        //c 配置编码器参数
        vc->flags |= AV_CODEC_FLAG_GLOBAL_HEADER; //全局参数
        vc->codec_id = codec->id;
        vc->thread_count = 8;

        vc->bit_rate = 50 * 1024 * 8;//压缩后每秒视频的bit位大小 50kB
        vc->width = inWidth;
        vc->height = inHeight;
        vc->time_base = { 1,fps };
        vc->framerate = { fps,1 };

        //画面组的大小,多少帧一个关键帧
        vc->gop_size = 50;
        vc->max_b_frames = 0;
        vc->pix_fmt = AV_PIX_FMT_YUV420P;
        //d 打开编码器上下文
        ret = avcodec_open2(vc, 0, 0);
        if (ret != 0)
        {
            char buf[1024] = { 0 };
            av_strerror(ret, buf, sizeof(buf) - 1);
            throw exception(buf);
        }
        cout << "avcodec_open2 success!" << endl;

        ///5 输出封装器和视频流配置
        //a 创建输出封装器上下文
        ret = avformat_alloc_output_context2(&ic, 0, "flv", outUrl);
        if (ret != 0)
        {
            char buf[1024] = { 0 };
            av_strerror(ret, buf, sizeof(buf) - 1);
            throw exception(buf);
        }
        //b 添加视频流 
        AVStream *vs = avformat_new_stream(ic, NULL);
        if (!vs)
        {
            throw exception("avformat_new_stream failed");
        }
        vs->codecpar->codec_tag = 0;
        //从编码器复制参数
        avcodec_parameters_from_context(vs->codecpar, vc);
        av_dump_format(ic, 0, outUrl, 1);


        ///打开rtmp 的网络输出IO
        ret = avio_open(&ic->pb, outUrl, AVIO_FLAG_WRITE);
        if (ret != 0)
        {
            char buf[1024] = { 0 };
            av_strerror(ret, buf, sizeof(buf) - 1);
            throw exception(buf);
        }

        //写入封装头
        ret = avformat_write_header(ic, NULL);
        if (ret != 0)
        {
            char buf[1024] = { 0 };
            av_strerror(ret, buf, sizeof(buf) - 1);
            throw exception(buf);
        }

        AVPacket pack;
        memset(&pack, 0, sizeof(pack));
        int vpts = 0;
        for (;;)
        {
            ///读取rtsp视频帧,解码视频帧
            if (!cam.grab())
            {
                continue;
            }
            ///yuv转换为rgb
            if (!cam.retrieve(frame))
            {
                continue;
            }
            imshow("video", frame);
            waitKey(20);


            ///rgb to yuv
            //输入的数据结构
            uint8_t *indata[AV_NUM_DATA_POINTERS] = { 0 };
            //indata[0] bgrbgrbgr
            //plane indata[0] bbbbb indata[1]ggggg indata[2]rrrrr 
            indata[0] = frame.data;
            int insize[AV_NUM_DATA_POINTERS] = { 0 };
            //一行(宽)数据的字节数
            insize[0] = frame.cols * frame.elemSize();
            int h = sws_scale(vsc, indata, insize, 0, frame.rows, //源数据
                yuv->data, yuv->linesize);
            if (h <= 0)
            {
                continue;
            }
            //cout << h << " " << flush;
            ///h264编码
            yuv->pts = vpts;
            vpts++;
            ret = avcodec_send_frame(vc, yuv);
            if (ret != 0)
                continue;

            ret = avcodec_receive_packet(vc, &pack);
            if (ret != 0 || pack.size > 0)
            {
                //cout << "*" << pack.size << flush;
            }
            else
            {
                continue;
            }
            //推流
            pack.pts = av_rescale_q(pack.pts, vc->time_base, vs->time_base);
            pack.dts = av_rescale_q(pack.dts, vc->time_base, vs->time_base);
            pack.duration = av_rescale_q(pack.duration, vc->time_base, vs->time_base);
            ret = av_interleaved_write_frame(ic, &pack);
            if (ret == 0)
            {
                cout << "#" << flush;
            }
        }

    }
    catch (exception &ex)
    {
        if (cam.isOpened())
            cam.release();
        if (vsc)
        {
            sws_freeContext(vsc);
            vsc = NULL;
        }

        if (vc)
        {
            avio_closep(&ic->pb);
            avcodec_free_context(&vc);
        }

        cerr << ex.what() << endl;
    }
    getchar();
    return 0;
}
  • 12
    点赞
  • 75
    收藏
    觉得还不错? 一键收藏
  • 15
    评论
要在C++中实现OpencvFFmpegRTMP,需要使用FFmpeg的API和Opencv的VideoCapture类。 首先,需要初始化FFmpeg的网络库和注册所有的组件。可以使用如下代码: ``` av_register_all(); avformat_network_init(); ``` 然后,需要打开视频文件或者摄像头,并将其转换为FFmpeg的AVFormatContext结构体。可以使用如下代码: ``` AVFormatContext *pFormatContext = nullptr; avformat_open_input(&pFormatContext, "video.mp4", nullptr, nullptr); if (avformat_find_stream_info(pFormatContext, nullptr) < 0) { // 处理错误 } ``` 接下来,需要找到视频和音频的索引,并打开它们。可以使用如下代码: ``` int videoStreamIndex = -1; int audioStreamIndex = -1; for (int i = 0; i < pFormatContext->nb_streams; i++) { AVStream *pStream = pFormatContext->streams[i]; if (pStream->codec->codec_type == AVMEDIA_TYPE_VIDEO) { videoStreamIndex = i; } if (pStream->codec->codec_type == AVMEDIA_TYPE_AUDIO) { audioStreamIndex = i; } } AVCodecContext *pVideoCodecContext = pFormatContext->streams[videoStreamIndex]->codec; AVCodecContext *pAudioCodecContext = pFormatContext->streams[audioStreamIndex]->codec; AVCodec *pVideoCodec = avcodec_find_decoder(pVideoCodecContext->codec_id); AVCodec *pAudioCodec = avcodec_find_decoder(pAudioCodecContext->codec_id); if (pVideoCodec == nullptr || pAudioCodec == nullptr) { // 处理错误 } if (avcodec_open2(pVideoCodecContext, pVideoCodec, nullptr) < 0 || avcodec_open2(pAudioCodecContext, pAudioCodec, nullptr) < 0) { // 处理错误 } ``` 然后,需要创建一个FFmpeg的AVOutputFormat结构体来表示输出格式,并打开输出文件。可以使用如下代码: ``` AVOutputFormat *pOutputFormat = av_guess_format("flv", nullptr, nullptr); AVFormatContext *pOutputContext = nullptr; if (avformat_alloc_output_context2(&pOutputContext, pOutputFormat, nullptr, "rtmp://127.0.0.1/live/test") < 0) { // 处理错误 } if (avio_open(&pOutputContext->pb, "rtmp://127.0.0.1/live/test", AVIO_FLAG_WRITE) < 0) { // 处理错误 } ``` 接着,需要创建视频和音频的AVStream结构体,并设置它们的编码器和参数。可以使用如下代码: ``` AVStream *pVideoStream = avformat_new_stream(pOutputContext, pVideoCodec); AVStream *pAudioStream = avformat_new_stream(pOutputContext, pAudioCodec); // 设置编码器参数 avcodec_parameters_from_context(pVideoStream->codecpar, pVideoCodecContext); avcodec_parameters_from_context(pAudioStream->codecpar, pAudioCodecContext); ``` 最后,需要循环读取视频帧和音频帧,并将它们写入输出。可以使用如下代码: ``` AVPacket packet; av_init_packet(&packet); while (av_read_frame(pFormatContext, &packet) >= 0) { if (packet.stream_index == videoStreamIndex) { // 处理视频帧 av_interleaved_write_frame(pOutputContext, &packet); } if (packet.stream_index == audioStreamIndex) { // 处理音频帧 av_interleaved_write_frame(pOutputContext, &packet); } av_packet_unref(&packet); } ``` 完整的代码示例可以参考[这里](https://github.com/charles-wangkai/opencv-ffmpeg-rtmp)。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值