推流

int main() {

    //注册
    av_register_all();
    avformat_network_init();


        //打开音视频
    AVFormatContext* infContext = avformat_alloc_context();
    AVFormatContext* outContext = avformat_alloc_context();
    AVPacket* pkt = av_packet_alloc();
    const char *inpath = "rtsp://184.72.239.149/vod/mp4://BigBuckBunny_175k.mov";
    const char* outpath = "udp://127.0.0.1:1234";

    AVStream *inputstream;
    AVStream *outputstream;

    AVRational src_tb;
    AVRational dst_tb;
    int reg = avformat_open_input(&infContext, inpath, NULL, NULL);
    if (reg<0)
    {
        cout << "open url fail\n" << endl;
        goto ERROR;

    }
    //寻找音视频流信息
    reg = avformat_find_stream_info(infContext, NULL);
    if (reg<0)
    {
        cout << "open input file stream fail\n" << endl;
        goto ERROR;
    }
    else
    {
        cout << "open input file stream sucesse"<<reg << endl;
    }

    //打开输出地址

    reg = avformat_alloc_output_context2(&outContext, NULL, "mpegts", outpath);
    

    if (reg<0)
    {
        cout << "open output file fail\n" << endl;
        goto ERROR;

    }
    else
    {
        cout << "open output file success\n" << endl;
    }
    
    if (avio_open2(&outContext->pb, outpath, AVIO_FLAG_WRITE, NULL, NULL)<0)
    {
        cout << "open avio fail\n"<<endl;

        goto ERROR;
    }
    cout << "open avio success\n" << endl;
    for (int i = 0; i < infContext->nb_streams; i++)
    {
        AVStream* stream = avformat_new_stream(outContext, infContext->streams[i]->codec->codec);

        reg = avcodec_copy_context(stream->codec, infContext->streams[i]->codec);
        if (reg<0)
        {
            cout << "copy context fail\n" << endl;
            goto ERROR;
        }
        cout << "copy context success\n" << endl;
    }
    reg = avformat_write_header(outContext, NULL);
        if (reg<0)
        {

            cout<<"write hearder fail\n"<<endl;
            goto ERROR;
        }
        //av_init_packet(pkt);


        while (true)
        {
            reg = av_read_frame(infContext, pkt);
            if (reg < 0)
            {
                cout << "read frame fail\n" << endl;
                goto ERROR;
            }

            //写入数据

            inputstream = infContext->streams[pkt->stream_index];
            outputstream = outContext->streams[pkt->stream_index];
            src_tb = inputstream->time_base;
            dst_tb = outputstream->time_base;

            if (pkt->pts != AV_NOPTS_VALUE)
                pkt->pts = av_rescale_q(pkt->pts, src_tb, dst_tb);
            if (pkt->dts != AV_NOPTS_VALUE)
                pkt->dts = av_rescale_q(pkt->dts, src_tb, dst_tb);
            if (pkt->duration > 0)
                pkt->duration = av_rescale_q(pkt->duration, src_tb, dst_tb);
            reg = av_interleaved_write_frame(outContext, pkt);
            if (reg < 0)
            {
                cout << "write fail\n" << endl;

            }
            else
            {
                cout << "write success\n" << endl;
            }
        
        }
ERROR:

    cout << "错误信息" << reg << endl;
    return reg;


    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值