目前,我正在尝试学习FFmpeg API,遵循本教程。然而,我已经对视频解码的第一课有了问题。我的代码与本教程中的代码基本相同,但我使用的是C++。我的问题是视频流与av_read_frame
返回的数据包中的视频流不匹配。
在可用流上循环获得视频流,直到找到视频流为止。
for(int i = 0; i < pFormatCtx->nb_streams; i++) { // nb_streams == 2
if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO) {
videoStream = i;
break; // videoStream == 0
}
}
然后,当检索帧数据时,它接缝抓住音频通道。
while(av_read_frame(pFormatCtx, &packet) >= 0) { // read returns 0
// Is this a packet from the video stream?
if(packet.stream_index == videoStream) {
//packet.stream_index == 1, which correspond to the audio stream
}
}
我还没有在网上找到这个测试实际上失败的例子。我是否错过了一些方法来指定本教程中没有的stream_index
?也许这个教程不是最新的,是不是做错了什么?如果是,那么提取帧数据的正确方法是什么?如果这很重要,我将在Windows64位上使用最新的FFmpeg 4.0.2版本,并使用Visual 2017进行编译。
在没有声音的视频上,两个流匹配,我能够正确地解码和显示帧。
发布于 2018-08-01 10:38:56
就像这样:
while(av_read_frame(pFormatCtx, &packet) == 0) {
AVStream *st = pFormatCtx->streams[packet.stream_index];
switch (st->codecpar->codec_type)
{
case AVMEDIA_TYPE_AUDIO:
/* handle audio */
break;
case AVMEDIA_TYPE_VIDEO:
/* handle video */
break;
...
}
}
https://stackoverflow.com/questions/51595363
复制相似问题