你好我在为flv文件加载AVCodec时遇到了问题。 (加载mp3或avi文件时似乎没有问题)
实际错误是: [vorbis @ 0x1550aa0] Extradata缺失。
所以我会保持简单并问你:
因为我花了几个小时用谷歌搜索但没有成功
您可以在下面看到我的代码:
#ifdef __cplusplus
#define __STDC_CONSTANT_MACROS
#ifdef _STDINT_H
#undef _STDINT_H
#endif
#endif
#include <stdint.h>
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/avutil.h>
#include <libavutil/samplefmt.h>
}
#include <stdio.h>
int main(int argc, char * argv[])
{
if(argc < 2)
{
printf("you need to specify filename \n");
return -1;
}
av_register_all();
avcodec_register_all();
//opening a file
AVFormatContext *avFormatContext = NULL;
int ret = avformat_open_input(&avFormatContext, argv[1], NULL, NULL);
if (ret < 0)
return -2; // Couldn't open file
//print some basic info
printf("num of streams = %u\n", *(&avFormatContext->nb_streams));
printf("filename = %s\n", *(&avFormatContext->filename));
printf("start time = %ld \n", *(&avFormatContext->start_time));
printf("duration = %ld \n", *(&avFormatContext->duration));
printf("bit rate = %d\n", *(&avFormatContext->bit_rate));
printf("audio codec id = %d \n\n\n", *(&avFormatContext->audio_codec_id));
AVCodecContext * pCodecContext;
int audioStreamId = -1;
for(int i = 0; i < avFormatContext->nb_streams; i++)
{
if(avFormatContext->streams[i]->codec->codec_type == \
AVMEDIA_TYPE_AUDIO)//CODEC_TYPE_AUDIO)
{
audioStreamId = i;
break;
}
}
if(audioStreamId == -1)
return -3; //Didn't find an audio stream
printf("audioStreamId = %d \n", audioStreamId);
pCodecContext = avFormatContext->streams[audioStreamId]->codec;
if(pCodecContext == NULL)
return -10;
//The stream's information about the codec is in what
//we call the "codec context." This contains all the information
//about the codec that the stream is using, and now we have a pointer to it.
//But we still have to find the actual codec and open it:
AVCodec *pCodec;
AVDictionary *options;
//Find the decoder for the audio stream
pCodec = avcodec_find_decoder(pCodecContext->codec_id);
printf("TEST codec name = %s fullName = \
%s\n",pCodec->name, pCodec->long_name);
if(pCodec == NULL)
return -4; //Codec not found
//Open codec
//avcodec_open2 - This function is not thread safe!
//Prior to using this function the context has to be allocated with
// avcodec_alloc_context3().
pCodecContext = avcodec_alloc_context3(pCodec);
printf("test 0\n");
if(pCodecContext == NULL)
return -5; //Could not allocate audio codec context
printf("test 1\n");
if(avcodec_open2(pCodecContext, pCodec, NULL) < 0)
return -6; //Couldn't open codec
printf("test 2\n");
avformat_close_input(&avFormatContext);
return 0;
}
这些是我输出屏幕的最后一行:
TEST codec id = 86021
t0
t1
[vorbis @ 0x19eeaa0] Extradata missing.
答案 0 :(得分:3)
调用avformat_open_input()后需要调用avformat_find_stream_info()