使用ffmpeg打开RTMP文件时崩溃

时间:2020-07-21 12:13:59

标签: ffmpeg rtmp

我已经编写了使用ffmpeg播放视频的代码。

当我打开AVI文件时,代码工作正常,但是当我尝试打开RTMP提要时却出现错误。

在utils.c文件的以下功能中的utils.c文件中

utils.c

int avcodec_parameters_to_context(AVCodecContext *codec,
 const AVCodecParameters *par)

{
    codec->codec_type = par->codec_type;  // crash happens at this line.


}

** par是nullptr

这是我的代码

 if (load_frame("rtmp://192.168.1.2/live/sumit", &file_width, &file_height, &myData)) {
        std::cout << "file Loaded";
    }

为load_frame函数定义

AVFormatContext *av_format_ctx = avformat_alloc_context();
    if (!av_format_ctx) {
        std::cout << "could not create a format context\n";
        return false;
    }
    
    if (avformat_open_input(&av_format_ctx, filename, NULL, NULL) < 0) {
        std::cout << "Couldn't open video file\n";
        return false;
    }
    AVCodecParameters* av_codec_params = nullptr;
    AVCodec* av_codec = nullptr;
    int video_stream_index = -1;
    for (unsigned int i = 0; i < av_format_ctx->nb_streams; i++) {
        auto stream = av_format_ctx->streams[i];
        av_codec_params = av_format_ctx->streams[i]->codecpar;
        av_codec = avcodec_find_decoder(av_codec_params->codec_id);
        
        if (!av_codec) {
            std::cout << "Couldn't find the codec\n";
            continue;
        }

        if (av_codec_params->codec_type == AVMEDIA_TYPE_VIDEO)
        {
            video_stream_index = i;
            std::cout << "Video stream found" << std::endl;
            break;
        }

        if (video_stream_index < 0)
            return false;
    }

    // set up codec context for the decoder
    AVCodecContext* av_codec_ctx = avcodec_alloc_context3(av_codec);
    if (!av_codec_ctx) {
        std::cout << "Couldn't create AV context";
        return false;
    }
      
   // this function invokes the error
    if (avcodec_parameters_to_context(av_codec_ctx, av_codec_params) < 0) {
        std::cout << "Couldn't initialize AVCodecContext";
        return false;
    }

//////////////////编辑/////

我正在流传输mpeg文件,并且av_format_ctx-> nb_streams返回值为0,为什么它找不到任何流。

我可以通过vlc中的流式传输选项在vlc上查看同一文件。

1 个答案:

答案 0 :(得分:2)

avformat_open_input这样思考fopen。它将打开一个流/文件,但是您仍然没有关于流/文件内容的信息,只有一个可以进行进一步操作的句柄。

如果要实际查看流/文件中的数据,则必须首先阅读标题以确定内部内容。 avformat_find_stream_info将实现这一目标