#include <iostream>
#include <vector>
#include <opencv2/highgui.hpp>
#include <opencv2/video.hpp>
#include <opencv2/opencv.hpp>
extern "C"
{
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libavutil/imgutils.h>
#include <libswscale/swscale.h>
}
using namespace std;
using namespace cv;

int main() {
    const char* out_url = "rtmp://192.168.27.128:1935/live/test";

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

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


    Mat frame;

    // 1.使用opencv 打开usb 摄像头
    VideoCapture video_ptr;
    video_ptr.open(0);

    if (!video_ptr.isOpened()) {
        cout << "camera open usb camera error" << endl;
        return -1;
    }

    cout << "open usb camera successful." << endl;

    int width = video_ptr.get(CAP_PROP_FRAME_WIDTH);
    int height = video_ptr.get(CAP_PROP_FRAME_HEIGHT);
    int fps = video_ptr.get(CAP_PROP_FPS);

    // 如果fps为0,这里就设置25。因为在fps=0时,调用avcodec_open2返回-22,
    // 参数不合法
    if (0 == fps) { fps = 25; }

    // 2.初始化格式转换上下文
    SwsContext* sws_context = NULL;
    sws_context = sws_getCachedContext(sws_context,
        width,
        height,
        AV_PIX_FMT_BGR24,    // 源格式
        width,
        height,
        AV_PIX_FMT_YUV420P,  // 目标格式
        SWS_BICUBIC,    // 尺寸变化使用算法
        0, 0, 0);

    if (NULL == sws_context) {
        cout << "sws_getCachedContext error" << endl;
        return -1;
    }



    // 3.初始化输出的数据结构
    yuv = av_frame_alloc();
    yuv->format = AV_PIX_FMT_YUV420P;
    yuv->width = width;
    yuv->height = height;
    yuv->pts = 0;

    // 分配yuv空间
    int ret_code = av_frame_get_buffer(yuv, 32);
    if (0 != ret_code) {
        cout << "  yuv init fail" << endl;
        return -1;
    }

    // 4.初始化编码上下文
    // 4.1找到编码器
    const AVCodec* codec = avcodec_find_encoder(AV_CODEC_ID_H264);
    if (NULL == codec) {
        cout << "Can't find h264 encoder." << endl;
        return -1;
    }

    // 4.2创建编码器上下文
    AVCodecContext* codec_context = avcodec_alloc_context3(codec);
    if (NULL == codec_context) {
        cout << "avcodec_alloc_context3 failed." << endl;
        return -1;
    }


    // 4.3配置编码器参数
    codec_context->flags           |= AV_CODEC_FLAG_GLOBAL_HEADER;
    codec_context->codec_id = codec->id;
    codec_context->thread_count = 8;

    // 压缩后每秒视频的bit流 50k
    codec_context->bit_rate = 50 * 1024 * 8;
    codec_context->width = width;
    codec_context->height = height;
    codec_context->time_base = { 1, fps };
    codec_context->framerate = { fps, 1 };

    // 画面组的大小,多少帧一个关键帧
    codec_context->gop_size = 50;
    codec_context->max_b_frames = 0;
    codec_context->pix_fmt = AV_PIX_FMT_YUV420P;
    codec_context->qmin = 10;
    codec_context->qmax = 51;

    AVDictionary* codec_options = nullptr;
    //(baseline | high | high10 | high422 | high444 | main)
    av_dict_set(&codec_options, "profile", "baseline", 0);
    av_dict_set(&codec_options, "preset", "superfast", 0);
    av_dict_set(&codec_options, "tune", "zerolatency", 0);

    // 4.4打开编码器上下文
    ret_code = avcodec_open2(codec_context, codec, &codec_options);
    if (0 != ret_code) {

        return -1;
    }
    cout << "avcodec_open2 success!" << endl;

    // 5.输出封装器和视频流配置
    // 5.1创建输出封装器上下文
    // rtmp flv封装器
    AVFormatContext* format_context = nullptr;
    ret_code = avformat_alloc_output_context2(&format_context, 0, "flv", out_url);
    if (0 != ret_code) {

        return -1;
    }

    // 5.2添加视频流
    AVStream* vs = avformat_new_stream(format_context, NULL);
    if (NULL == vs) {
        cout << "avformat_new_stream failed." << endl;
        return -1;
    }

    vs->codecpar->codec_tag = 0;
    // 从编码器复制参数
    avcodec_parameters_from_context(vs->codecpar, codec_context);
    av_dump_format(format_context, 0, out_url, 1);

    // 打开rtmp 的网络输出IO
    ret_code = avio_open(&format_context->pb, out_url, AVIO_FLAG_WRITE);
    if (0 != ret_code) {
        cout << "avio_open failed." << endl;
        return -1;
    }

    // 写入封装头
    ret_code = avformat_write_header(format_context, NULL);
    if (0 != ret_code) {
        cout << "avformat_write_header failed." << endl;
        return -1;
    }

    AVPacket pack = {0};
    //memset(&pack, 0, sizeof(pack));
    int vpts = 0;
    uint8_t* in_data[AV_NUM_DATA_POINTERS] = { 0 };

    int in_size[AV_NUM_DATA_POINTERS] = { 0 };
    for (;;) {

        // 读取rtsp视频帧,解码视频帧
        video_ptr >> frame;
        // If the frame is empty, break immediately
        if (frame.empty()) break;

        /*imshow("video", frame);
        waitKey(1);*/

        // rgb to yuv
        in_data[0] = frame.data;
        // 一行(宽)数据的字节数
        in_size[0] = frame.cols * frame.elemSize();
        int h = sws_scale(sws_context,
            in_data, 
            in_size,
            0, 
            frame.rows,
            yuv->data,
            yuv->linesize);
        if (h <= 0) { continue; }

        //av_packet_unref(&pack);
        // h264编码
        yuv->pts = vpts;
        vpts++;
       
        ret_code = avcodec_send_frame(codec_context, yuv);
        if (0 != ret_code) { continue; }

        ret_code = avcodec_receive_packet(codec_context, &pack);

        if (0 != ret_code ||pack.size<=0) {//
            cout << "avcodec_receive_packet." << endl;
            continue;
        }
       /* else {
            cout << "avcodec_receive_packet contiune." << endl;
            continue;
        }*/

        // 推流
        pack.pts = av_rescale_q(pack.pts, codec_context->time_base, vs->time_base);
        pack.dts = av_rescale_q(pack.dts, codec_context->time_base, vs->time_base);
        pack.duration = av_rescale_q(pack.duration,
            codec_context->time_base,
            vs->time_base);
        ret_code = av_interleaved_write_frame(format_context, &pack);
        if (0 != ret_code)
        {
            cout << "pack is error" << endl;
        }
       
        frame.release();
    }
    av_dict_free(&codec_options);
    avcodec_free_context(&codec_context);
    av_frame_free(&yuv);
    avio_close(format_context->pb);
    avformat_free_context(format_context);
    sws_freeContext(sws_context);
    video_ptr.release();


    destroyAllWindows();
    return 0;
}

Logo

音视频技术社区,一个全球开发者共同探讨、分享、学习音视频技术的平台,加入我们,与全球开发者一起创造更加优秀的音视频产品!

更多推荐