56
56

More than 5 years have passed since last update.

macでffmpegをビルドして、アプリ内でffmpegを使う(static libraryとして)

Last updated at Posted at 2015-04-17

iOSでffmpegを使いたいときは、この辺を見ればいいと思いますが、macアプリの場合に関しては探してもあんまり出てこないです。知ってて当然て感じなのでしょうか。

唯一参考になるqiitaの記事がありましたので、そこを見ながらやってみました。下記そのときのメモです。

事前に必要なもの

  • Xcode command line tools
  • Homebrew

は予めインストールしておくこと

1. まずはffmpegをビルド

https://trac.ffmpeg.org/wiki/CompilationGuide/MacOSX
こちらの公式ページの、Compiling FFmpeg yourself を見ながらビルドします。brew install ffmpegでももしかしたら、本手順を全て代替できるかもしれないですが、試していません。

ffmpegのビルドに必要なライブラリをbrew install

$ brew install automake fdk-aac git lame libass libtool libvorbis libvpx \
opus sdl shtool texi2html theora wget x264 xvid yasm

$HOME/.profileに環境変数設定を追加

$HOME/.profile
CFLAGS=`freetype-config --cflags`
LDFLAGS=`freetype-config --libs` 
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig:/usr/lib/pkgconfig:/usr/X11/lib/pkgconfig
# .profileを念のため現在のシェルに反映します
$ source ~/.profile

ffmpegソースをgit submodule add

追加したいプロジェクトの任意のディレクトリで

$ git submodule add git@github.com:FFmpeg/FFmpeg.git ffmpeg

として、submoduleを追加して、ソースのダウンロード完了を待ちます。

ソースをconfigureする(失敗)

$ cd ffmpeg
$ ./configure --prefix=/usr/local --enable-gpl --enable-nonfree --enable-libass --enable-libfdk-aac --enable-libfreetype --enable-libmp3lame --enable-libopus --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libxvid

とすると、ここで標準出力にこんなエラーが

ERROR: libass not found using pkg-config

If you think configure made a mistake, make sure you are using the latest
version from Git.  If the latest version fails, report the problem to the
ffmpeg-user@ffmpeg.org mailing list or IRC #ffmpeg on irc.freenode.net.
Include the log file "config.log" produced by configure as this will help
solve the problem.

config.logにどうやらログが吐かれてるので見てみます。

check_pkg_config libass ass/ass.h ass_library_init
false --exists --print-errors libass

libassはインストールされていたので、pkg-configかと思って探したら、なるほど無い。ので、brew installします。

$ brew install pkg-config

再度configureする(成功)

$ ./configure --prefix=/usr/local --enable-gpl --enable-nonfree --enable-libass --enable-libfdk-aac --enable-libfreetype --enable-libmp3lame --enable-libopus --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libxvid
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
install prefix            /usr/local
source path               .
C compiler                gcc
C library                 
ARCH                      x86 (generic)
big-endian                no
runtime cpu detection     yes
yasm                      yes
MMX enabled               yes
MMXEXT enabled            yes
3DNow! enabled            yes
3DNow! extended enabled   yes
SSE enabled               yes
SSSE3 enabled             yes
AVX enabled               yes
XOP enabled               yes
FMA3 enabled              yes
FMA4 enabled              yes
i686 features enabled     yes
CMOV is fast              yes
EBX available             yes
EBP available             yes
debug symbols             yes
strip symbols             yes
optimize for size         no
optimizations             yes
static                    yes
shared                    no
postprocessing support    yes
new filter support        yes
network support           yes
threading support         pthreads
safe bitstream reader     yes
SDL support               yes
opencl enabled            no
texi2html enabled         yes
perl enabled              yes
pod2man enabled           yes
makeinfo enabled          yes
makeinfo supports HTML    no

External libraries:

~ 中略 ~

License: nonfree and unredistributable
Creating config.mak, config.h, and doc/config.texi...

成功!
一応ソース中に、GPLのライセンスの部分があるので、その辺の切り分けをconfigureで行えるようです。

make && sudo make install

$ make && sudo make install

無事ffmpegのビルドが完了したら、/usr/local/lib に.aファイルが新規に追加されていたり、/usr/local/Cellar/ffmpeg/2.6.1/ あたりに色々ファイルが追加されていることを確認します(Finderで、Date Addedで並び替えるとわかりやすいです)。

2. スタティックライブラリ(.aファイル)と、.hファイルをプロジェクトに読ませる

ここからはXcodeの操作です。

.aファイルをプロジェクトに読ませる

赤丸の部分を番号の順にタップ/入力していきます。

/usr/local/lib 配下に.aファイルがあるので、それらを Link Binary With Libraries に追加します。ここでは使うものだけでいいかと思います。

Screen Shot 2015-04-17 at 03.15.59.png

これだけだと、リンカエラーになってしまうので、ライブラリサーチパスにも追加します。
Screen Shot 2015-04-17 at 14.01.03.png

.hファイルをプロジェクトに読ませる

赤丸の部分を番号の順にタップ/入力などして、最終的に /usr/local/includeをプロジェクトに読ませればOKです(手順5の部分はダブルタップ)。

Screen Shot 2015-04-17 at 03.26.24.png

Bridging-Header.hを追加 & 読み込ませる

BridgingHeaderの作成に関しては、こちらを参考にしてみてください。
【iOS】BridgingHeaderの使い方と設定方法

これで設定は完了です。あとは実際にBridging-Headerに利用するライブラリのヘッダファイルを読ませて、ソースを作っていきます。

実行してみる

と、0からやるのは非常に辛かったので、冒頭に言及した方のソースをお借りして少し改変して(ppmファイル保存場所変更)実行してみました。ありがとうございますm(_ _)m

オリジナル:FFmpegチュートリアル1: キャプチャする

改変版:https://github.com/mitolog/ffmpegtest

実行すると、下記のようなログがコンソールに吐かれます。同時に、アプリケーションのmainBundle直下にppmファイルという画像サムネイルが100フレーム毎にキャプチャされて出力されます。この場合は、
/Users/mito/Library/Developer/Xcode/DerivedData/ffmpegtest-cpiehyeubshhjqhhflzjizzzecfv/Build/Products/Debug/ffmpegtest.app/がmainbundleのpathになります。

Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/Users/mito/Library/Developer/Xcode/DerivedData/ffmpegtest-cpiehyeubshhjqhhflzjizzzecfv/Build/Products/Debug/ffmpegtest.app/Contents/Resources/sample.m4v':
  Metadata:
    major_brand     : M4V 
    minor_version   : 1
    compatible_brands: M4V M4A mp42isom
    creation_time   : 2015-04-17 05:53:54
  Duration: 00:00:14.85, start: 0.000000, bitrate: 3175 kb/s
    Stream #0:0(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv, smpte170m/smpte170m/bt709), 640x360 [SAR 1:1 DAR 16:9], 3171 kb/s, 29.97 fps, 29.97 tbr, 30k tbn, 60k tbc (default)
    Metadata:
      creation_time   : 2015-04-17 05:53:54
      handler_name    : Core Media Video

参考

ffmpegライブラリ doxygenドキュメント

[Git] git submodule は癖がすごいとの噂だったが素直につきあっていけそうという話

configureの設定を変更してみる

GPLやMITやCCなど主要ライセンスの内容と意味のまとめ

56
56
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
56
56