Eyes, JAPAN Blog > Small trick to enable ffmpeg on iOS devices and iPhone simulator

Small trick to enable ffmpeg on iOS devices and iPhone simulator

denvazh

この記事は1年以上前に書かれたもので、内容が古い可能性がありますのでご注意ください。

Recently, within development team we was discussing how it would be possible to build ffmpeg libraries for iOS.
We have found, that there was already a project which was doing this: ffmpeg4ios.org

Unfortunately, because it provided only dynamically linked libraries (which wasn’t so useful for our case), I had to dig deep into how ffmpeg was compiled for arm platform.
Fortunately, it provided build scripts, which was possible to use as a draft for our own build.

Few words about using 3d party libraries in iOS project.
Even though it is not possible to use dynamically linked binaries (*.dylib), it is completely legal to use static ones (*.a).

To have full set of build for all platforms and cpu (armv7,armv7s and iOS simulator) code had to be compiled 3 times and then single fat binary had to be created using lipo tool.

Important note: ffmpeg requires gas-preprocessor to be installed. It can be found here.

In order to compile native code for armv7/armv7s build scripts from ffmpeg4ios.org had to be changed to generate static libraries, i.e. config parameters had to be changed to “–enable-static –disable-shared”.

iOS simulator code was more complex to figure out, but in the end I was able to come up with stable working build script:

#!/bin/bash

SRC_PREFIX=$HOME/src
INST_PREFIX=$HOME/build

cd $SRC_PREFIX/ffmpeg

./configure \
--disable-doc \
--disable-ffmpeg \
--disable-ffplay \
--disable-ffserver \
--disable-ffprobe \
--arch=i386 \
--target-os=darwin \
--cpu=i386 \
--cc=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/gcc \
--sysroot=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.1.sdk \
--extra-cflags='-arch i386' \
--extra-ldflags="-arch i386 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.1.sdk" \
--disable-logging \
--disable-debug \
--enable-static \
--disable-shared \
--enable-pic \
--disable-asm \
--prefix=$INST_PREFIX/iossim

make
make install

This would produce ffmpeg static libraries for iOS simulator in “$HOME/build/iossim”

Finally, all three build has to be used to create single fat binary suitable for both iOS native platform and iOS simulator.

#!/bin/bash

#
# This build file will create universal binaries for multiple platforms:
# 
# - iOS armv7 cpu
# - iOS armv7s cpu (newer)
# - iOS simulator

INST_PREFIX=$HOME/build
ARMV7_PREFIX="${INST_PREFIX}/armv7/lib"
ARMV7S_PREFIX="${INST_PREFIX}/armv7s/lib"
IOSSIM_PREFIX="${INST_PREFIX}/iossim/lib"
UNIVERSAL_PREFIX="${INST_PREFIX}/universal"

cd $INST_PREFIX
[[ ! -d "${INST_PREFIX}/universal" ]] && mkdir "${INST_PREFIX}/universal"

LIBS=( libavcodec.a libavdevice.a libavfilter.a libavformat.a libavutil.a libswresample.a libswscale.a )

for i in ${LIBS[@]}; do
        LIBNAME=`basename $i .a`
        xcrun -sdk iphoneos lipo -output $UNIVERSAL_PREFIX/$LIBNAME -create -arch armv7 $ARMV7_PREFIX/$i \
        -create -arch armv7s $ARMV7S_PREFIX/$i \
        -create -arch i386 $IOSSIM_PREFIX/$i
done

Finally, it is important to add header files together with library files, otherwise it won’t be possible to build project with those libraries.

Comments are closed.