ffmpeg4教程8:rtmp流保存flv

基于vs2017 vc++  ffmpeg4.0.2下测试

ffmpeg 环境配置请百度(vs2017 ffmpeg ) 

部分方法在https://blog.csdn.net/Java_lilin/article/details/85118365中查找
头文件参考上篇

int main33333 ()
{  
    AVOutputFormat *ofmt = NULL;
    //Input AVFormatContext and Output AVFormatContext
    AVFormatContext *ifmt_ctx = avformat_alloc_context(), *ofmt_ctx = NULL;
    AVPacket pkt;
    const char *in_filename, *out_filename;
    int ret, i;
    int videoindex = -1;
    int frame_index = 0;
    in_filename = "rtmp://192.168.0.108/live/a";//"rtmp://192.168.0.108/live/f";
    //in_filename  = "rtp://233.233.233.233:6666";
    //out_filename = "receive.ts";
    //out_filename = "receive.mkv";
    out_filename = "C:\\Users\\lilin\\Desktop\\out.flv";
     
    //Network
    avformat_network_init(); 
    //av_dict_set(&options, "video_size","1920*1080",0);//大小  默认全部
    cchaoshitime = av_gettime();//每次循环帧都要这个呢
    ifmt_ctx->interrupt_callback.callback = interrupt_test; //超时判断设置

    if ((ret = avformat_open_input(&ifmt_ctx, in_filename, 0, 0)) < 0) {
        printf("Could not open input file.");
        return -1;
    }
    
    if ((ret = avformat_find_stream_info(ifmt_ctx, 0)) < 0) {
        printf("Failed to retrieve input stream information");
        return -1;
    }
    

    for (i = 0; i < ifmt_ctx->nb_streams; i++) {
        if (ifmt_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
            videoindex = i;
            break;
        }
    } 
    av_dump_format(ifmt_ctx, 0, in_filename, 0);
    
    //Output
    avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, out_filename); // 
    if (!ofmt_ctx) {
        printf("Could not create output context\n"); 
        return -1;
    }
    ofmt = ofmt_ctx->oformat;
    for (i = 0; i < ifmt_ctx->nb_streams; i++) {
        //Create output AVStream according to input AVStream
        AVStream *in_stream = ifmt_ctx->streams[i];
        AVCodec *incodec = avcodec_find_encoder(in_stream->codecpar->codec_id);
        if (!incodec) {
            printf("not found encoder 1 ");
            return -1;
        }
        AVStream *out_stream = avformat_new_stream(ofmt_ctx, incodec);
        if (!out_stream) {
            printf("Failed allocating output stream\n");
            ret = AVERROR_UNKNOWN;
            return -1;
        }
        //Copy the settings of AVCodecContext
        AVCodecContext *codec_ctx = avcodec_alloc_context3(incodec);
        ret = avcodec_parameters_to_context(codec_ctx, in_stream->codecpar);
        if (ret < 0) {
            printf("Failed to copy in_stream codecpar to codec context\n");
            return -1;
        }
        codec_ctx->codec_tag = 0;
        if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
            codec_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;

        ret = avcodec_parameters_from_context(out_stream->codecpar, codec_ctx);
        if (ret < 0) {
            printf("Failed to copy codec context to out_stream codecpar context\n");
        }
     
    }
    //Dump Format------------------
    av_dump_format(ofmt_ctx, 0, out_filename, 1);
    
    //Open output URL
    if (!(ofmt->flags & AVFMT_NOFILE)) {
        ret = avio_open(&ofmt_ctx->pb, out_filename, AVIO_FLAG_WRITE);
        if (ret < 0) {
            printf("Could not open output URL '%s'", out_filename);
            return -1;
        }
    }
    //Write file header
    ret = avformat_write_header(ofmt_ctx, NULL);
    if (ret < 0) {
        printf("Error occurred when opening output URL\n");
        return -1;
    }

    //AVBitStreamFilterContext* h264bsfc = av_bitstream_filter_init("h264_mp4toannexb"); 

    while (1) {
        cchaoshitime = av_gettime();
        AVStream *in_stream, *out_stream;
        //Get an AVPacket
        ret = av_read_frame(ifmt_ctx, &pkt);
        
        if (ret < 0)
            break;

        in_stream = ifmt_ctx->streams[pkt.stream_index];
        out_stream = ofmt_ctx->streams[pkt.stream_index];
        //简化的时间帧计算  非简化参考上pian
        av_packet_rescale_ts(&pkt, in_stream->time_base, out_stream->time_base);
        ret = av_interleaved_write_frame(ofmt_ctx, &pkt);

        if (ret < 0) {
            printf("Error muxing packet\n");
            break;
        }

        av_packet_unref(&pkt);
    }
    //Write file trailer
    av_write_trailer(ofmt_ctx); 

    avformat_close_input(&ifmt_ctx);
    /* close output */
    if (ofmt_ctx && !(ofmt->flags & AVFMT_NOFILE)) {
        avio_close(ofmt_ctx->pb);
    } 
    avformat_free_context(ofmt_ctx);
    if (ret < 0 && ret != AVERROR_EOF) {
        printf("Error occurred.\n");
        return -1;
    }
      

    printf("ok1"); 
    getchar();

    return 0;
}

讨论群261074724

猜你喜欢

转载自blog.csdn.net/Java_lilin/article/details/85123814