我正在尝试使用 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 会产生以下输出:
audio format: mp3
audio bitrate: 256121
stream: 0 (total: 1)
audio codec: mp3
但是当我尝试打开 ogg 文件时,我得到以下信息:
Error find stream information: -1 (Streams: 1)
当我手动设置audioStreamIndex=0
并注释掉return语句时:
Error find stream information: -1 (Streams: 1)
audio format: mp3
audio bitrate: 0
stream: 0 (total: 1)
audio codec: mp3
对于 m4a (AAC) 我得到这个:
audio format: mp3
audio bitrate: 288000
stream: 0 (total: 1)
audio codec: mp1
但后来在 avcodec_decode_audio3
中失败。
我也尝试手动强制格式但没有成功:
AVInputFormat *pForceFormat= av_find_input_format("ogg");
if ((lError = avformat_open_input(&pFormatCtx, filename, pForceFormat, 0))
// continue
加载代码是否有问题,导致它只能处理 mp3 和 wav,而无法处理其他格式?
问候,
最佳答案
问题是缺少解复用器。
关于audio - 使用 libav/ffmpeg 正确打开音频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12968650/