如何在ffmpeg中从已打开的文件获取流信息?

5
我正在尝试使用ffmpeg读取视频文件。我有一个对应于旧版本的工作代码,尝试升级到最新版本并替换所有废弃功能为它们的实际模拟。
然而,我遇到了一个问题。没有任何流被检索到,视频加载停滞不前。
以下是我正在使用的代码:
   // Open video file
   if(avformat_open_input(&pFormatCtx, filename.toStdString().c_str(), NULL, NULL)!=0)
       return FILE_NOT_OPENED; // Couldn't open file

   // Retrieve stream information
   if(avformat_find_stream_info(pFormatCtx,NULL)<0)
       return NO_STREAM_INFO; // Couldn't find stream information

   // Dump information about file onto standard error
   av_dump_format(pFormatCtx, 0, filename.toStdString().c_str(), false);

   // Find the first video stream
   videoStream=-1;
   for(unsigned i=0; i<pFormatCtx->nb_streams; i++)
       if(pFormatCtx->streams[i]->codec->codec_type==ffmpeg::AVMEDIA_TYPE_VIDEO)
       {
           videoStream=i;
           break;
       }
   if(videoStream==-1)
       return OTHER; // Didn't find a video stream

   // Get a pointer to the codec context for the video stream
   pCodecCtx=pFormatCtx->streams[videoStream]->codec;

   // Find the decoder for the video stream
   pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
   if(pCodec==NULL)
       return CODEC_NOT_FOUND; // Codec not found

   // Open codec
   if(avcodec_open2(pCodecCtx, pCodec,NULL)<0)
       return CODEC_NOT_OPENED; // Could not open codec

问题出现在通过ffmpeg::AVFormatContext *pFormatCtx循环处理视频流时。nb_streams字段为0,我实际上从未进入循环,并且编解码器未加载等。奇怪的是,av_dump_format提供以下输出:
License: GPL version 3 or later
AVCodec version 3606372
AVFormat configuration: --disable-static --enable-shared --enable-gpl --enable-version3 --disable-w32threads --enable-avisynth --enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libcaca --enable-libfreetype --enable-libgsm --enable-libilbc --enable-libmodplug --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-librtmp --enable-libschroedinger --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libtwolame --enable-libvo-aacenc --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libxavs --enable-libxvid --enable-zlib
[asf @ 004e9540] Stream #0: not enough frames to estimate rate; consider increasing probesize
Input #0, asf, from 'C:/Users/Public/Videos/Sample Videos/Wildlife.wmv':
  Metadata:
    SfOriginalFPS   : 299700
    WMFSDKVersion   : 11.0.6001.7000
    WMFSDKNeeded    : 0.0.0.0000
    comment         : Footage: Small World Productions, Inc; Tourism New Zealand | Producer: Gary F. Spradling | Music: Steve Ball
    title           : Wildlife in HD
    copyright       : В© 2008 Microsoft Corporation
    IsVBR           : 0
    DeviceConformanceTemplate: AP@L3
  Duration: 00:00:30.09, start: 0.000000, bitrate: 6977 kb/s
    Stream #0:0(eng): Audio: wmav2 (a[1][0][0] / 0x0161), 44100 Hz, 2 channels, fltp, 192 kb/s
    Stream #0:1(eng): Video: vc1 (Advanced) (WVC1 / 0x31435657), yuv420p, 1280x720, 5942 kb/s, 29.97 tbr, 1k tbn, 1k tbc

这里有两个流,非常清晰。

我完全困惑了,请帮忙。


你的代码看起来很好。你能分享一个样本视频给我试一下吗? - Wagner Patriota
我正在尝试在默认情况下随Windows附带的视频Wildlife.wmv上运行它。 - Srv19
我尝试了同样的视频,它对我起作用了。问题似乎出在别的地方。我正在使用最新版本的Windows FFmpeg。对于我来说,pFormatCtx->nb_streams = 2,所以我可以正常循环流。 - Wagner Patriota
我已经尝试获取最新的FFmpeg头文件和二进制文件,对我也有效。哎呀。 好的,请写下“使用最新的FFmpeg”作为我的答案以便我接受。 - Srv19
1个回答

6
如果您的代码中av_dump_format可用,但nb_streams为零,则我猜测您的库和头文件不匹配。如其源代码中所示,av_dump_format也依赖于nb_streams。因此,您使用的二进制代码可以读取nb_streams。很可能您正在使用一个预编译的静态或动态avformat库,它与您的avformat.h标头版本不匹配。因此,您的代码可能会在错误的位置或类型上寻找nb_streams,例如。确保您使用与用于生成所使用的库的二进制文件完全匹配的标头文件。

哦,太感谢了,你拯救了我的一天。我一直在想为什么我的代码在 Android 上能运行,但在 Mac 上会崩溃。原来我引用了不同版本的 ffmpeg 头文件,同时链接到了另一个版本的库文件。 - neevek

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