首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用libav/ffmpeg正确打开音频文件

使用libav/ffmpeg正确打开音频文件
EN

Stack Overflow用户
提问于 2012-10-19 06:35:35
回答 1查看 2.1K关注 0票数 2

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

代码语言:javascript
复制
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并注释掉返回语句时:

错误查找流信息:-1 (流: 1) 音频格式: mp3 音频比特率:0 流:0(总数: 1) 音频编解码器: mp3

对于m4a (AAC),我理解如下:

音频格式: mp3 音频比特率: 288000 流:0(总数: 1) 音频编解码器: mp1

但是后来它在avcodec_decode_audio3中失败了。

我还试图手动强制格式,但没有成功:

代码语言:javascript
复制
AVInputFormat *pForceFormat= av_find_input_format("ogg");
if ((lError = avformat_open_input(&pFormatCtx, filename, pForceFormat, 0))
// continue

加载代码是否有问题,使其只适用于mp3和wav,而其他格式却失败?

致以敬意,

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-10-29 10:26:19

问题是一个失踪的辩证员。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12968650

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档