【问题标题】:How to get stream info from opened file in ffmpeg?如何从 ffmpeg 中打开的文件中获取流信息?
【发布时间】:2013-05-19 19:00:29
【问题描述】:

我正在尝试使用 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

有 2 条溪流,清澈如初。

我完全被迷惑了。请帮忙。

【问题讨论】:

  • 您的代码似乎没问题。你能分享一个示例视频让我试试吗?
  • 我在默认 Windows 附带的视频上进行了尝试。野生动物.wmv
  • 我尝试了相同的视频。它对我有用。问题似乎出在其他地方。我正在使用适用于 Windows 的最新版本的 FFmpeg。 pFormatCtx->nb_streams = 2 对我来说,所以我可以正常循环流..
  • 我已经尝试获取最后的 ffmpeg 标头和二进制文件,它也对我有用。嗯。好的,写“使用最新的 ffmpeg”作为我接受的答案。

标签: c++ qt video ffmpeg


【解决方案1】:

如果av_dump_format 有效,但nb_streams 在您的代码中为零,我猜您的库和头文件不匹配。

av_dump_format 也依赖于nb_streams,正如其源代码中所见。所以你用于av_dump_format 的二进制代码可以读取nb_streams。您可能正在使用预编译的静态或动态 avformat 库,它与您的 avformat.h 标头版本不匹配。因此,例如,您的代码可能会在错误的位置或类型处查找 nb_streams

确保您使用的头文件与用于制作所用库的二进制文件的头文件完全匹配。

【讨论】:

  • 天哪,你救了我的命。我想知道为什么我的代码可以在 Android 上运行,但在 Mac 上崩溃。在链接到另一个版本的库时,我为标题引用了不同版本的 ffmpeg。
猜你喜欢
  • 2020-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-04
  • 2021-08-29
  • 2010-09-08
  • 1970-01-01
相关资源
最近更新 更多