今天说一下为IOS编译Opus音频库遇到的坑。

首先要编译IOS的库,得找一个mac环境,并且下载了XCode,Linux或者Windows下未试过,暂时不知道怎么操作。

其次IOS需要的库比较特殊,它需要的是一个集大成者,意思是这个库中需要保护多个平台架构的各种库,比如i386 x86_64 armv7 armv7s arm64等等,即编译各个平台下的库,然后用苹果自带的lipo命令将之合成。

最后将合成的库放到IOS下使用即可了。

下面提供一个比较暴力的脚本,集下载,编译,合成于一体,如果那里有失败的请自行修改。通过该脚本编译出的库在朋友的IOS APP中正常使用。

脚本默认编译1.2.1版本的opus库。

#!/bin/bash
VERSION="1.2.1"
SDKVERSION="11.1"
MINIOSVERSION="8.0"
# by default, we won't build for debugging purposes
if [ "${DEBUG}" == "true" ]; then
    echo "Compiling for debugging ..."
    OPT_CFLAGS="-O3 -fno-inline -g"
    OPT_LDFLAGS=""
    OPT_CONFIG_ARGS="--enable-assertions --disable-asm"
    OPT_CFLAGS="-Ofast -flto -g"
    OPT_LDFLAGS="-flto"
    OPT_CONFIG_ARGS=""
# No need to change this since xcode build will only compile in the
# necessary bits from the libraries we create
ARCHS="i386 x86_64 armv7 armv7s arm64"
DEVELOPER=`xcode-select -print-path`
#DEVELOPER="/Applications/Xcode.app/Contents/Developer"
#cd "`dirname \"$0\"`"
REPOROOT=$(pwd)
# Where we'll end up storing things in the end
OUTPUTDIR="${REPOROOT}/dependencies"
mkdir -p ${OUTPUTDIR}/include
mkdir -p ${OUTPUTDIR}/lib
BUILDDIR="${REPOROOT}/build"
# where we will keep our sources and build from.
SRCDIR="${BUILDDIR}/src"
mkdir -p $SRCDIR
# where we will store intermediary builds
INTERDIR="${BUILDDIR}/built"
mkdir -p $INTERDIR
########################################
cd $SRCDIR
# Exit the script if an error happens
set -e
if [ ! -e "${SRCDIR}/opus-${VERSION}.tar.gz" ]; then
	echo "Downloading opus-${VERSION}.tar.gz"
	curl -LO http://downloads.xiph.org/releases/opus/opus-${VERSION}.tar.gz
echo "Using opus-${VERSION}.tar.gz"
tar zxf opus-${VERSION}.tar.gz -C $SRCDIR
cd "${SRCDIR}/opus-${VERSION}"
set +e # don't bail out of bash script if ccache doesn't exist
CCACHE=`which ccache`
if [ $? == "0" ]; then
	echo "Building with ccache: $CCACHE"
	CCACHE="${CCACHE} "
	echo "Building without ccache"
	CCACHE=""
set -e # back to regular "bail out on error" mode
export ORIGINALPATH=$PATH
for ARCH in ${ARCHS}
    if [ "${ARCH}" == "i386" ] || [ "${ARCH}" == "x86_64" ]; then
        PLATFORM="iPhoneSimulator"
        EXTRA_CFLAGS="-arch ${ARCH}"
        EXTRA_CONFIG="--host=x86_64-apple-darwin"
        PLATFORM="iPhoneOS"
        EXTRA_CFLAGS="-arch ${ARCH}"
        EXTRA_CONFIG="--host=arm-apple-darwin"
	mkdir -p "${INTERDIR}/${PLATFORM}${SDKVERSION}-${ARCH}.sdk"
	./configure --enable-float-approx --disable-shared --enable-static --with-pic --disable-extra-programs --disable-doc ${EXTRA_CONFIG} \
    --prefix="${INTERDIR}/${PLATFORM}${SDKVERSION}-${ARCH}.sdk" \
    LDFLAGS="$LDFLAGS ${OPT_LDFLAGS} -fPIE -miphoneos-version-min=${MINIOSVERSION} -L${OUTPUTDIR}/lib" \
    CFLAGS="$CFLAGS ${EXTRA_CFLAGS} ${OPT_CFLAGS} -fPIE -miphoneos-version-min=${MINIOSVERSION} -I${OUTPUTDIR}/include -isysroot ${DEVELOPER}/Platforms/${PLATFORM}.platform/Developer/SDKs/${PLATFORM}${SDKVERSION}.sdk" \
    # Build the application and install it to the fake SDK intermediary dir
    # we have set up. Make sure to clean up afterward because we will re-use
    # this source tree to cross-compile other targets.
	make -j4
	make install
	make clean
########################################
echo "Build library..."
# These are the libs that comprise libopus.
OUTPUT_LIBS="libopus.a"
for OUTPUT_LIB in ${OUTPUT_LIBS}; do
	INPUT_LIBS=""
	for ARCH in ${ARCHS}; do
		if [ "${ARCH}" == "i386" ] || [ "${ARCH}" == "x86_64" ];
			PLATFORM="iPhoneSimulator"
			PLATFORM="iPhoneOS"
		INPUT_ARCH_LIB="${INTERDIR}/${PLATFORM}${SDKVERSION}-${ARCH}.sdk/lib/${OUTPUT_LIB}"
		if [ -e $INPUT_ARCH_LIB ]; then
			INPUT_LIBS="${INPUT_LIBS} ${INPUT_ARCH_LIB}"
	# Combine the three architectures into a universal library.
	if [ -n "$INPUT_LIBS"  ]; then
		lipo -create $INPUT_LIBS \
		-output "${OUTPUTDIR}/lib/${OUTPUT_LIB}"
		echo "$OUTPUT_LIB does not exist, skipping (are the dependencies installed?)"
for ARCH in ${ARCHS}; do
	if [ "${ARCH}" == "i386" ] || [ "${ARCH}" == "x86_64" ];
		PLATFORM="iPhoneSimulator"
		PLATFORM="iPhoneOS"
	cp -R ${INTERDIR}/${PLATFORM}${SDKVERSION}-${ARCH}.sdk/include/* ${OUTPUTDIR}/include/
	if [ $? == "0" ]; then
		# We only need to copy the headers over once. (So break out of forloop
		# once we get first success.)
		break
####################
echo "Building done."
echo "Cleaning up..."
rm -fr ${INTERDIR}
rm -fr "${SRCDIR}/opus-${VERSION}"
echo "Done."
                        今天说一下为IOS编译Opus音频库遇到的坑。    首先要编译IOS的库,得找一个mac环境,并且下载了XCode,Linux或者Windows下未试过,暂时不知道怎么操作。    其次IOS需要的库比较特殊,它需要的是一个集大成者,意思是这个库中需要保护多个平台架构的各种库,比如i386 x86_64 armv7 armv7s arm64等等,即编译各个平台下的库,然后用苹果自...
  .amr:体积很小,1秒到约为1kb,所以音质缩水也很厉害,一般用于手机铃声或彩信
  .mp3:比较流行的,有损音频,某些部分失真,,音质随码率的提高,越高越好
  .wav:为无损音频
  .pcm:无损的wav文件中音频数据的一种编码方式
由于App是通过AVAudioRecorder录制音频,默认格式为pcm,文件比较大,所以不适合用于聊天通信的文件格式,所以最优的选择是转换成amr格式
音频格式转换方式
				
最近还在搞桌面化视频网站系统,因为播放技术才用的是html5里标签,所以经常去了解该标签支持的格式,今天发现其支持的一个音频格式Opus格式,发现这个音频格式真的会成为未来音频发展的趋势。下面我具体介绍下Opus格式。 一、Opus格式简介: Opus是一款开源、免费、自由度高的有损音频编解码器,融合了Skype的SILK和XVID的CELT 技术,拥有比AAC、OGG等其它有损
据说Opus 比speex,aac各方面性能更好,也很适合做网络语音通话。 采样率 : 音频取样频率, 指每秒钟取得声音样本的次数。采样频率越高,声音的质量也就越好,声音的还原也就越真实,但同时它占的资源比较多。一般采样率有 8000HZ 16000HZ 44100HZ。 每秒的传输速率(位速, 也叫比特率)。如256kbps 或256000bps, 其中的
1. 安装pydub:在命令行中输入pip install pydub。 2. 导入pydub:在Python代码中输入from pydub import AudioSegment。 3. 读取opus音频文件:在Python代码中输入audio = AudioSegment.from_file("audio.opus", format="opus")。 4. 指定音频片段:在Python代码中输入segment = audio[start_time:end_time],其中start_time和end_time分别为音频片段的起始时间和结束时间,单位为毫秒。 5. 导出音频片段:在Python代码中输入segment.export("output.mp3", format="mp3"),其中output.mp3为导出的音频文件名,format为导出的音频格式。 注意:在读取opus音频文件时,需要安装ffmpeg。如果没有安装,可以在命令行中输入pip install ffmpeg-python。