【问题标题】:Segmentation fault when media played using libavcodec使用 libavcodec 播放媒体时出现分段错误
【发布时间】:2013-08-20 17:02:15
【问题描述】:

我自己尝试使用 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;
    }
}

在 if(container->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO) 处发生分段错误

我该如何解决这个问题?我在 ubuntu 12.04 工作。

【问题讨论】:

  • 当我执行 gdb 时,我从 /usr/lib/i386-linux-gnu/i686/cmov/libavutil.so.51 的 av_strstart () 中的 0xb6f74985 得到分段错误

标签: c++ ffmpeg codec


【解决方案1】:

您不需要在开始时分配您的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
}

【讨论】:

  • 但是我仍然在同一个地方遇到分段错误。我还没有完成分配 AVFormatContext。
  • @sarahjohn 段错误仍然出现在同一行?
  • @sarahjohn 在段错误之前循环执行了多少次?
  • 循环没有被执行。
  • @sarahjohn 你能测试一下container-&gt;streams[i]-&gt;codec 不是NULL 吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-03
  • 2017-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多