【问题标题】:Properly open audio files with libav/ffmpeg使用 libav/ffmpeg 正确打开音频文件
【发布时间】:2012-10-19 06:35:35
【问题描述】:

我正在尝试使用 ffmpeg 解码来自各种文件格式的音频样本。因此,我根据本次讨论中的代码开始了一些实验:How to decode audio via FFmpeg in Android。我使用最新的 FFMPEG 版本 (1.0) 并使用 https://github.com/halfninja/android-ffmpeg-x264 编译它

AVFormatContext * pFormatCtx;

avcodec_register_all();
av_register_all();

int lError;
if ((lError = avformat_open_input(&pFormatCtx, filename, NULL, 0))
        != 0) {
    LOGE("Error open source file: %d", lError);
    return;
}
if ((lError = avformat_find_stream_info(pFormatCtx, 0)) < 0) {
    LOGE("Error find stream information: %d (Streams: %d)", lError, pFormatCtx->nb_streams);
    return;
}
LOGE("audio format: %s", pFormatCtx->iformat->name);
LOGE("audio bitrate: %d", pFormatCtx->bit_rate);
audioStreamIndex = av_find_best_stream(pFormatCtx, AVMEDIA_TYPE_AUDIO,
        -1, -1, &codec, 0);

//if (audioStreamIndex < 0 || audioStreamIndex >= pFormatCtx->nb_streams)
//  audioStreamIndex = 0;

LOGE("Stream: %d (total: %d)", audioStreamIndex, pFormatCtx->nb_streams);
LOGE("audio codec: %s", codec->name);

FFMPEG 使用 enable-decoder=mp1/mp2/mp3/ogg/vorbis/wav/aac/theora 编译,没有任何外部库(例如 libmp3lame、libtheora 等)

打开 mp3 和 wav 文件可以毫无问题地生成以下输出,例如 mp3:

音频格式:mp3

音频比特率:256121

流:0(总计:1)

音频编解码器:mp3

但是当我尝试打开一个 ogg 文件时,我得到了这个:

错误查找流信息:-1(流:1)

当我手动设置audioStreamIndex=0并注释掉return语句时:

错误查找流信息:-1(流:1)

音频格式:mp3

音频比特率:0

流:0(总计:1)

音频编解码器:mp3

对于 m4a (AAC),我得到了这个:

音频格式:mp3

音频比特率:288000

流:0(总计:1)

音频编解码器:mp1

但后来它在avcodec_decode_audio3 中失败了。

我也尝试手动强制格式化但没有成功:

AVInputFormat *pForceFormat= av_find_input_format("ogg");
if ((lError = avformat_open_input(&pFormatCtx, filename, pForceFormat, 0))
// continue

加载代码是否有问题,使其仅适用于 mp3 和 wav,而无法用于其他格式?

问候,

【问题讨论】:

    标签: audio ffmpeg libavcodec libav libavformat


    【解决方案1】:

    问题是缺少解复用器。

    【讨论】:

      猜你喜欢
      • 2013-12-24
      • 2021-01-07
      • 2014-04-23
      • 2015-05-21
      • 1970-01-01
      • 2013-05-22
      • 1970-01-01
      • 2013-01-05
      • 2023-02-08
      相关资源
      最近更新 更多