使用libavcodec播放媒体时出现分段故障

Segmentation fault when media played using libavcodec

本文关键字:分段 故障 libavcodec 播放 媒体 使用      更新时间:2023-10-16

我自己尝试使用libavcodec作为后端播放媒体。我下载了ffmpeg-2.0.1并使用安装/配置、制作和安装。当我试图运行一个应用程序来播放音频文件时,我在检查第一个音频流时遇到了分段错误。我的程序就像

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;
    }
}

如果(容器->流[i]->编解码器->codec_type==AVMEDIA_type_AUDIO)出现分段故障

我该怎么解决这个问题?我在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
}