gpt4 book ai didi

c++ - FFmpeg av_read_frame 从音频流返回数据包

转载 作者:太空宇宙 更新时间:2023-11-04 12:45:56 25 4
gpt4 key购买 nike

我目前正在尝试学习 FFmpeg API,关注 this tutorial .但是,我已经对视频解码的第一课产生了疑问。除了我使用的是 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
}
}

然后在检索帧数据时,它接缝抓取音频 channel 。

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 的方法?也许教程不是最新的并且做错了什么?如果是这样,提取帧数据的正确方法是什么?以防万一,我在 Windows 64 位 上使用最新的 FFmpeg 4.0.2 版本,使用 Visual Studio 2017 进行编译。

在没有声音的视频中,两个流匹配,我能够正确解码和显示帧。

最佳答案

像这样尝试:

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;

...
}
}

关于c++ - FFmpeg av_read_frame 从音频流返回数据包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51595363/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com