如何修复这些错误?

3
我正在尝试编写FFmpeg客户端流媒体,但出现了以下错误:

Error C4996 'av_register_all':已被声明为过时
Error C4996 'av_free_packet':已被声明为过时
Error C4996 'AVStream::codec':已被声明为过时
Error C4996 'avcodec_decode_video2':已被声明为过时
Error C4996 'avcodec_copy_context':已被声明为过时

这些错误意味着使用了过时的FFmpeg函数。建议您更新代码以使用新的函数来解决这些问题。
  int size = avpicture_get_size(AV_PIX_FMT_YUV420P, 
  ccontext>width,ccontext->height);
  uint8_t* picture_buf = (uint8_t*)(av_malloc(size));
  AVFrame* pic = avcodec_alloc_frame();
  AVFrame* picrgb = avcodec_alloc_frame();
  int size2 = avpicture_get_size(AV_PIX_FMT_RGB24, ccontext->width, 
  ccontext->height);
  uint8_t* picture_buf2 = (uint8_t*)(av_malloc(size2));
  avpicture_fill((AVPicture *)pic, picture_buf, AV_PIX_FMT_YUV420P, 
  ccontext->width, ccontext->height);
  avpicture_fill((AVPicture *)picrgb, picture_buf2,AV_PIX_FMT_RGB24, 
  ccontext->width, ccontext->height);

  while (av_read_frame(context, &packet) >= 0 && cnt < 1000)
 {//read 100 frames

    std::cout << "1 Frame: " << cnt << std::endl;
    if (packet.stream_index == video_stream_index) 
    {//packet is video
        std::cout << "2 Is Video" << std::endl;
        if (stream == NULL)
        {//create stream in file
            std::cout << "3 create stream" << std::endl;
            stream = avformat_new_stream(oc, context- 
            >streams[video_stream_index]->codec->codec);
            avcodec_copy_context(stream->codec, context- 
            >streams[video_stream_index]->codec);
            stream->sample_aspect_ratio = context- 
            >streams[video_stream_index]->codec->sample_aspect_ratio;
        }
        int check = 0;
        packet.stream_index = stream->id;
        std::cout << "4 decoding" << std::endl;
        int result = avcodec_decode_video2(ccontext, pic,          
        &check,&packet);
        std::cout << "Bytes decoded " << result << " check " << check << 
        std::endl;
        if (cnt > 100)//cnt < 0)
        {
            sws_scale(img_convert_ctx, pic->data, pic->linesize, 0, 
            ccontext->height, picrgb->data, picrgb->linesize);
            std::stringstream name;
            name << "test" << cnt << ".ppm";
            myfile.open(name.str());
            myfile << "P3 " << ccontext->width << " " << ccontext->height 
                  << " 255\n";
            for (int y = 0; y < ccontext->height; y++)
            {
                for (int x = 0; x < ccontext->width * 3; x++)
                    myfile << (int)(picrgb-> 
                data[0] + y * picrgb->linesize[0])[x] << " ";
            }
            myfile.close();
        }

你有没有试着查一下这些错误的含义?你有没有阅读你正在使用的ffmpeg版本的文档? - Useless
Gnoses 应该是警告而不是错误。编译时禁用警告作为错误标志。 - szatmary
如果某个东西被弃用了,意味着你应该停止使用它,并且有一个新的、更好的替代品。为什么不直接修改你的代码,使用这个新的东西,以此修复错误呢? - undefined
1个回答

4

首先,这些应该是警告而不是错误。如果它们是错误,则编译器设置过于严格。

av_register_all - 不再使用,只需删除此行

av_free_packet -> av_packet_unref

AVStream::codec -> AVStream::codecpar

avcodec_decode_video2 -> 用avcodec_send_packetavcodec_receive_frame机制替换

最后:

avcodec_copy_context -> 根据文档:

该函数的语义定义不明确,不应使用。如果您需要将流参数从一个编解码器上下文传输到另一个编解码器上下文,请使用中间的AVCodecParameters实例和avcodec_parameters_from_context() / avcodec_parameters_to_context()函数。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接