我使用ffmpeg将PCM数据编码为AAC格式: 以下是我设置上下文对象的代码:
-(id)encode:(short*)data{
AVCodecContext *audioCodec;
AVCodec *codec;
avcodec_register_all();
//Set up audio encoder
codec = avcodec_find_encoder(AV_CODEC_ID_AAC);
if (codec == NULL){
NSLog(@"no codec");
}
audioCodec = avcodec_alloc_context3(codec);
audioCodec->strict_std_compliance = -2;
audioCodec->bit_rate = 64000;
audioCodec->sample_fmt = AV_SAMPLE_FMT_S16;
audioCodec->sample_rate = 8000;
audioCodec->channels = 2;
audioCodec->profile = FF_PROFILE_AAC_MAIN;
audioCodec->time_base = (AVRational){1, 8000};
audioCodec->codec_type = 1;
if (avcodec_open2(audioCodec, codec, NULL)) {
NSLog(@"could not open codec");
}
return @"NO";
}
我总是得到一个错误日志: aac @ 0x151cda00]指定的样本格式s16无效或不受支持
如果我没有提供任何样本格式,我会得到相同的日志: aac @ 0x151cda00]指定的样本格式-1无效或不受支持
以及avcodec_open2()
给出日志的原因无法打开编解码器;
一些人可以告诉我这是什么问题吗?
ffmpeg编译脚本如下:
#!/bin/sh
# directories
SOURCE="ffmpeg"
FAT="fat"
VERSION="2.0.2"
SCRATCH="scratch"
# must be an absolute path
THIN=`pwd`/"thin"
# absolute path to x264 library
#X264=`pwd`/fat_x264
CONFIGURE_FLAGS="--enable-cross-compile \
--disable-network \
--disable-encoders \
--disable-decoders \
--disable-muxers \
--disable-demuxers \
--disable-protocols \
--disable-devices \
--disable-ffmpeg \
--disable-ffplay \
--disable-ffprobe \
--disable-ffserver \
--disable-avdevice \
--disable-avfilter \
--disable-iconv \
--disable-bzlib \
--disable-mmx \
--disable-mmxext \
--disable-amd3dnow \
--disable-amd3dnowext \
--disable-sse \
--disable-sse2 \
--disable-sse3 \
--disable-sse4 \
--disable-avx \
--disable-fma4 \
--disable-swresample \
--disable-postproc \
--disable-bsfs \
--disable-filters \
--disable-asm \
--disable-yasm \
--disable-debug \
--disable-doc \
--disable-armv5te \
--disable-armv6 \
--disable-armv6t2 \
--enable-protocol=file \
--enable-avformat \
--enable-avcodec \
--enable-swscale \
--enable-demuxer=mp3 \
--enable-demuxer=aac \
--enable-demuxer=image2 \
--enable-demuxer=mov \
--enable-decoder=rawvideo \
--enable-demuxer=h263 \
--enable-demuxer=h264 \
--enable-decoder=mp3 \
--enable-decoder=aac \
--enable-decoder=mjpeg \
--enable-decoder=h263 \
--enable-decoder=h264 \
--enable-decoder=mpeg4 \
--enable-encoder=mp3 \
--enable-encoder=aac \
--enable-encoder=mjpeg \
--enable-encoder=h263 \
--enable-encoder=h264 \
--enable-encoder=mpeg4 \
--enable-parser=mp3 \
--enable-parser=aac \
--enable-parser=h264 \
--enable-pic"
if [ "$X264" ]
then
CONFIGURE_FLAGS="$CONFIGURE_FLAGS --enable-gpl --enable-libx264"
fi
# avresample
#CONFIGURE_FLAGS="$CONFIGURE_FLAGS --enable-avresample"
ARCHS="arm64 armv7s armv7 x86_64 i386"
COMPILE="y"
LIPO="y"
DEPLOYMENT_TARGET="6.0"
if [ "$*" ]
then
if [ "$*" = "lipo" ]
then
# skip compile
COMPILE=
else
ARCHS="$*"
if [ $# -eq 1 ]
then
# skip lipo
LIPO=
fi
fi
fi
if [ "$COMPILE" ]
then
CWD=`pwd`
for ARCH in $ARCHS
do
echo "building $ARCH..."
mkdir -p "$SCRATCH/$ARCH"
cd "$SCRATCH/$ARCH"
CFLAGS="-arch $ARCH"
if [ "$ARCH" = "i386" -o "$ARCH" = "x86_64" ]
then
PLATFORM="iPhoneSimulator"
CFLAGS="$CFLAGS -mios-simulator-version-min=$DEPLOYMENT_TARGET"
else
PLATFORM="iPhoneOS"
CFLAGS="$CFLAGS -mios-version-min=$DEPLOYMENT_TARGET"
if [ "$ARCH" = "arm64" ]
then
EXPORT="GASPP_FIX_XCODE5=1"
fi
fi
XCRUN_SDK=`echo $PLATFORM | tr '[:upper:]' '[:lower:]'`
CC="xcrun -sdk $XCRUN_SDK clang"
CXXFLAGS="$CFLAGS"
LDFLAGS="$CFLAGS"
if [ "$X264" ]
then
CFLAGS="$CFLAGS -I$X264/include"
LDFLAGS="$LDFLAGS -L$X264/lib"
fi
$CWD/$SOURCE/configure \
--target-os=darwin \
--arch=$ARCH \
--cc="$CC" \
$CONFIGURE_FLAGS \
--extra-cflags="$CFLAGS" \
--extra-cxxflags="$CXXFLAGS" \
--extra-ldflags="$LDFLAGS" \
--prefix="$THIN/$ARCH"
make -j3 install $EXPORT
cd $CWD
done
fi
if [ "$LIPO" ]
then
echo "building fat binaries..."
mkdir -p $FAT/lib
set - $ARCHS
CWD=`pwd`
cd $THIN/$1/lib
for LIB in *.a
do
cd $CWD
lipo -create `find $THIN -name $LIB` -output $FAT/lib/$LIB
done
cd $CWD
cp -rf $THIN/$1/include $FAT
fi
答案 0 :(得分:3)
我遇到了类似的问题,因为我不知道我的Windows Phone支持哪种格式。 (原来是AV_SAMPLE_FMT_FLTP)
我最终查看了avcodec_open2源代码(https://www.ffmpeg.org/doxygen/2.1/libavcodec_2utils_8c_source.html)
并发现它正在引用编解码器以查看支持哪些样本。
考虑到这一点,你可以这样做: audioCodec-> sample_fmt = audioCodec-> codec-> sample_fmts [0];
答案 1 :(得分:1)
错误可能会产生误导。
尝试删除该行:
audioCodec->profile = FF_PROFILE_AAC_MAIN
或将其更改为audioCodec->profile = FF_PROFILE_UNKNOWN
尝试在fdk-aac
编码器中进行编译:https://github.com/mstorsjo/fdk-aac输出质量要好得多,它支持各种采样率和比特率。据我所知,原生ffmpeg编码器相比并不是很好。但也许你不能使用fdk-aac
因为许可证(你需要向Fraunhofer支付专利使用费):