LAME for iOS

MP3编码引擎,使用LAME转码MP3有着良好的音质,同时产生的文件体积也非常小,可以说是物美价廉的典范。
在iOS项目中使用LAME,首先我们可以去LAME MP3 Encoder下载下源码。
下载完源码后,将以下脚本放在lame的目录下,使用编译脚本编译出静态库,可以自行选择使用单独架构的静态库,也可以使用合成的静态库,分别在fat-lame文件夹和thin-lame文件夹。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/bin/sh

CONFIGURE_FLAGS="--disable-shared --disable-frontend"

ARCHS="arm64 armv7s x86_64 i386 armv7"

# directories
# 记得改成自己的路径
SOURCE=""
SCRATCH="/Users/owenhong/lame"

FAT="fat-lame"

# must be an absolute path
THIN=`pwd`/"thin-lame"

COMPILE="y"
LIPO="y"

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"

if [ "$ARCH" = "i386" -o "$ARCH" = "x86_64" ]
then
PLATFORM="iPhoneSimulator"
if [ "$ARCH" = "x86_64" ]
then
SIMULATOR="-mios-simulator-version-min=7.0"
HOST=x86_64-apple-darwin
else
SIMULATOR="-mios-simulator-version-min=5.0"
HOST=i386-apple-darwin
fi
else
PLATFORM="iPhoneOS"
SIMULATOR=
HOST=arm-apple-darwin
fi

XCRUN_SDK=`echo $PLATFORM | tr '[:upper:]' '[:lower:]'`
CC="xcrun -sdk $XCRUN_SDK clang -arch $ARCH"
#AS="$CWD/$SOURCE/extras/gas-preprocessor.pl $CC"
CFLAGS="-arch $ARCH $SIMULATOR"
if ! xcodebuild -version | grep "Xcode [1-6]\."
then
CFLAGS="$CFLAGS -fembed-bitcode"
fi
CXXFLAGS="$CFLAGS"
LDFLAGS="$CFLAGS"

CC=$CC $CWD/$SOURCE/configure \
$CONFIGURE_FLAGS \
--host=$HOST \
--prefix="$THIN/$ARCH" \
CC="$CC" CFLAGS="$CFLAGS" LDFLAGS="$LDFLAGS"

make -j3 install
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
  • –disable-shared 关闭动态链接库
  • –disable-frontend 编译不生成可执行文件
  • –host 指定编译出的库所用的平台
  • –prefix 指定编译后库的路径
  • CC 指定编译器
  • CFLAGS 指定编译参数,表示支持的架构,bitcode开关,支持iOS最低版本
  • LDFLAGS 链接器参数

编译成功后我们可以看看fat-lame里的合成静态库的MachO
$ file libmp3lame.a

正常情况下应该是这样的

1
2
3
4
5
libmp3lame.a: Mach-O universal binary with 4 architectures: [arm_v7:current ar archive random library] [arm64]
libmp3lame.a (for architecture armv7): current ar archive random library
libmp3lame.a (for architecture armv7s): current ar archive random library
libmp3lame.a (for architecture x86_64): current ar archive random library
libmp3lame.a (for architecture arm64): current ar archive random library