build-ffmpeg.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #!/bin/sh
  2. # directories
  3. FF_VERSION="4.3.1"
  4. #FF_VERSION="snapshot-git"
  5. if [[ $FFMPEG_VERSION != "" ]]; then
  6. FF_VERSION=$FFMPEG_VERSION
  7. fi
  8. SOURCE="ffmpeg-$FF_VERSION"
  9. FAT="FFmpeg-iOS"
  10. SCRATCH="scratch"
  11. # must be an absolute path
  12. THIN=`pwd`/"thin"
  13. # absolute path to x264 library
  14. X264=`pwd`/x264-iOS
  15. #FDK_AAC=`pwd`/../fdk-aac-build-script-for-iOS/fdk-aac-ios
  16. CONFIGURE_FLAGS="--enable-cross-compile --disable-debug \
  17. --disable-doc --enable-pic"
  18. #添加FFmpeg配置选项->默认配置
  19. #--enable-cross-compile: 交叉编译
  20. #--disable-debug: 禁止使用调试模式
  21. #--disable-programs:禁用程序(不允许建立命令行程序)
  22. #--disable-doc:不需要编译文档
  23. #--enable-pic:允许建立与位置无关代码
  24. CONFIGURE_FLAGS="--enable-cross-compile --disable-debug --disable-doc --enable-pic"
  25. #核心库(编解码->最重要的库):avcodec
  26. CONFIGURE_FLAGS="$CONFIGURE_FLAGS --enable-avdevice --enable-avcodec --enable-avformat"
  27. CONFIGURE_FLAGS="$CONFIGURE_FLAGS --enable-swresample --enable-swscale --disable-postproc"
  28. CONFIGURE_FLAGS="$CONFIGURE_FLAGS --enable-avfilter --enable-avutil --enable-avresample "
  29. if [ "$X264" ]
  30. then
  31. CONFIGURE_FLAGS="$CONFIGURE_FLAGS --enable-gpl --enable-libx264"
  32. fi
  33. if [ "$FDK_AAC" ]
  34. then
  35. CONFIGURE_FLAGS="$CONFIGURE_FLAGS --enable-libfdk-aac --enable-nonfree"
  36. fi
  37. # avresample
  38. #CONFIGURE_FLAGS="$CONFIGURE_FLAGS --enable-avresample"
  39. ARCHS="arm64 armv7"
  40. COMPILE="y"
  41. LIPO="y"
  42. DEPLOYMENT_TARGET="8.0"
  43. if [ "$*" ]
  44. then
  45. if [ "$*" = "lipo" ]
  46. then
  47. # skip compile
  48. COMPILE=
  49. else
  50. ARCHS="$*"
  51. if [ $# -eq 1 ]
  52. then
  53. # skip lipo
  54. LIPO=
  55. fi
  56. fi
  57. fi
  58. if [ "$COMPILE" ]
  59. then
  60. if [ ! `which yasm` ]
  61. then
  62. echo 'Yasm not found'
  63. if [ ! `which brew` ]
  64. then
  65. echo 'Homebrew not found. Trying to install...'
  66. ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" \
  67. || exit 1
  68. fi
  69. echo 'Trying to install Yasm...'
  70. brew install yasm || exit 1
  71. fi
  72. if [ ! `which gas-preprocessor.pl` ]
  73. then
  74. echo 'gas-preprocessor.pl not found. Trying to install...'
  75. (curl -L https://github.com/libav/gas-preprocessor/raw/master/gas-preprocessor.pl \
  76. -o /usr/local/bin/gas-preprocessor.pl \
  77. && chmod +x /usr/local/bin/gas-preprocessor.pl) \
  78. || exit 1
  79. fi
  80. if [ ! -r $SOURCE ]
  81. then
  82. echo 'FFmpeg source not found. Trying to download...'
  83. curl http://www.ffmpeg.org/releases/$SOURCE.tar.bz2 | tar xj \
  84. || exit 1
  85. fi
  86. # grep -rl AVMediaType ./$SOURCE | xargs sed -i .bak s@AVMediaType@FFmpegAVMediaType@g
  87. CWD=`pwd`
  88. for ARCH in $ARCHS
  89. do
  90. echo "building $ARCH..."
  91. mkdir -p "$SCRATCH/$ARCH"
  92. cd "$SCRATCH/$ARCH"
  93. CFLAGS="-arch $ARCH"
  94. if [ "$ARCH" = "i386" -o "$ARCH" = "x86_64" ]
  95. then
  96. PLATFORM="iPhoneSimulator"
  97. CFLAGS="$CFLAGS -mios-simulator-version-min=$DEPLOYMENT_TARGET"
  98. else
  99. PLATFORM="iPhoneOS"
  100. CFLAGS="$CFLAGS -mios-version-min=$DEPLOYMENT_TARGET"
  101. if [ "$ARCH" = "arm64" ]
  102. then
  103. EXPORT="GASPP_FIX_XCODE5=1"
  104. fi
  105. fi
  106. XCRUN_SDK=`echo $PLATFORM | tr '[:upper:]' '[:lower:]'`
  107. CC="xcrun -sdk $XCRUN_SDK clang"
  108. # force "configure" to use "gas-preprocessor.pl" (FFmpeg 3.3)
  109. if [ "$ARCH" = "arm64" ]
  110. then
  111. AS="gas-preprocessor.pl -arch aarch64 -- $CC"
  112. else
  113. AS="gas-preprocessor.pl -- $CC"
  114. fi
  115. CXXFLAGS="$CFLAGS"
  116. LDFLAGS="$CFLAGS"
  117. if [ "$X264" ]
  118. then
  119. CFLAGS="$CFLAGS -I$X264/include"
  120. LDFLAGS="$LDFLAGS -L$X264/lib"
  121. fi
  122. if [ "$FDK_AAC" ]
  123. then
  124. CFLAGS="$CFLAGS -I$FDK_AAC/include"
  125. LDFLAGS="$LDFLAGS -L$FDK_AAC/lib"
  126. fi
  127. TMPDIR=${TMPDIR/%\/} $CWD/$SOURCE/configure \
  128. --target-os=darwin \
  129. --arch=$ARCH \
  130. --cc="$CC" \
  131. --as="$AS" \
  132. $CONFIGURE_FLAGS \
  133. --extra-cflags="$CFLAGS" \
  134. --extra-ldflags="$LDFLAGS" \
  135. --prefix="$THIN/$ARCH" \
  136. || exit 1
  137. make -j3 install $EXPORT || exit 1
  138. cd $CWD
  139. done
  140. fi
  141. if [ "$LIPO" ]
  142. then
  143. echo "building fat binaries..."
  144. mkdir -p $FAT/lib
  145. set - $ARCHS
  146. CWD=`pwd`
  147. cd $THIN/$1/lib
  148. for LIB in *.a
  149. do
  150. cd $CWD
  151. echo lipo -create `find $THIN -name $LIB` -output $FAT/lib/$LIB 1>&2
  152. lipo -create `find $THIN -name $LIB` -output $FAT/lib/$LIB || exit 1
  153. done
  154. cd $CWD
  155. cp -rf $THIN/$1/include $FAT
  156. fi
  157. echo Done