avformat_find_stream_info不填充nb_streams和流

avformat_find_stream_info not filling out the nb_streams and streams

本文关键字:nb streams 和流 填充 info find stream avformat      更新时间:2023-10-16

嗨,这是我第一次找不到已经回答的问题,所以这是我首次在这里发布内容。

我有这个代码剪辑,这是我尝试遵循我找到的一些关于获得一些基本libav功能(提取视频帧的方法)的教程。

      1 #ifndef INT64_C
      2 #define INT64_C(c) (c ## LL)
      3 #define UINT64_C(c) (c ## ULL)
      4 #endif
      5 
      6 extern "C" {
      7 #include <libavformat/avformat.h>
      8 #include <libavcodec/avcodec.h>
      9 #include <libavutil/avutil.h>
      10 }
      11 #include <iostream>
      12 using namespace std;
      13 #define FILENAME "/home/jon/Videos/testvideo.avi"
      14 
      15 
      16 int main(int argc, char** argv)
      17 {
      18     av_register_all();
      19     AVFormatContext * avFormatPtr = avformat_alloc_context();
      20     if (avformat_open_input(&avFormatPtr, FILENAME, NULL, NULL) != 0)
      21         cout<<"Error while calling avformat_open_input (probably invalid file  format)"<<endl;
      22     if (avformat_find_stream_info(avFormatPtr, NULL) < 0)
      23         cout<<"Error while calling avformat_find_stream_info"<<endl;
      24     av_dump_format(avFormatPtr,0,FILENAME,false);
      25 
      26     cout<<"There are "<<avFormatPtr->nb_streams<<" streams"<<endl;
      27     unsigned int video_codec_id = -1;
      28     for (unsigned int i = 0; i < avFormatPtr->nb_streams; ++i) {
      29         cout<<"loop iteration "<<i<<endl;
      30         if(avFormatPtr->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO)
      31         {
      32             cout<<"Found video "<<i<<endl;
      33             video_codec_id = i;
      34         }
      35         cout<<"debug point";
      36     }
      37 
      38     cout<<"fin"<<endl;
      39}           

现在的问题是,这总是分段故障,这是的输出

    Input #0, avi, from '/home/jon/Videos/testvideo.avi':
      Metadata:
        encoder         : Lavf53.21.0
      Duration: 00:01:51.08, start: 0.000000, bitrate: 17129 kb/s
        Stream #0.0: Video: mpeg4 (Simple Profile), yuv420p, 1920x1088 [PAR 1:1 DAR 30:17], 25 tbr, 25 tbn, 25 tbc
    There are 157328928 streams
    loop iteration 0
    Segmentation fault (core dumped)

流部分的数量每次都会变化,这让我认为它只是指向内存中的一个随机位置,所以我想我一定对有误解

    avformat_find_stream_info

现在,这在第一次循环迭代中也总是segfault,所以我想流也没有初始化。提前感谢您的帮助。

有趣的是av_dump_format()能够正确地看到流。我能想到的一个可能的原因是你的Libav安装已经损坏了——你使用的是不同主要版本的头文件和库。

尝试打印LIBAVFORMAT_VERSION_INTavformat_version()