我自己试图使用libavcodec作为后端播放媒体。我下载了ffmpeg-2.0.1,并使用./configure、make和make install进行了安装。
尝试运行应用程序以播放音频文件时,在检查第一个音频流时出现分段错误。我的程序就像
AVFormatContext* container = avformat_alloc_context();
if (avformat_open_input(&container, input_filename, NULL, NULL) < 0) {
die(“Could not open file”);
}
if (av_find_stream_info(container) < 0) {
die(“Could not find file info”);
}
av_dump_format(container, 0, input_filename, false);
int stream_id = -1;
int i;
for (i = 0; i < container->nb_streams; i++) {
if (container->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO) {
stream_id = i;
break;
}
}
我怎样才能解决这个问题?我正在Ubuntu 12.04中工作。
最佳答案
您无需在开始时分配AVFormatContext
。
同样不建议使用av_find_stream_info
函数,您必须将其更改为 avformat_find_stream_info
:
av_register_all();
avcodec_register_all();
AVFormatContext* container = NULL;
if (avformat_open_input(&container, input_filename, NULL, NULL) < 0) {
die(“Could not open file”);
}
if (avformat_find_stream_info(container, NULL) < 0) {
die(“Could not find file info”);
}
// av_dump_format(container, 0, input_filename, false);
int stream_id = -1;
int i;
for (i = 0; i < container->nb_streams; i++) {
if (container->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO) {
stream_id = i;
break;
}
}
另外我不确定
av_dump_format
在这里是否有用...编辑:
您是否尝试过类似的方法:
av_register_all();
avcodec_register_all();
AVFormatContext* container = NULL;
AVCodec *dec;
if ( avformat_open_input(&container, input_filename, NULL, NULL) < 0) {
// ERROR
}
if ( avformat_find_stream_info(container, NULL) < 0) {
// ERROR
}
/* select the audio stream */
if ( av_find_best_stream(container, AVMEDIA_TYPE_AUDIO, -1, -1, &dec, 0) < 0 ) {
// ERROR
}