cafegale(LeafCage備忘録)

LeafCage備忘録(はてなダイアリー)と統一しました。

Windows用のlibfdk_aacが使えるffmpegを手に入れるまでの長い道のり

以前からAACエンコーディングするならFDK-AACという情報は得ていた。
ffmpegのオーディオコーデックの指定:tech.ckme.co.jp
ただ、ライセンスの問題から、FDK-AACを組み込んだffmpegは配布されておらず、使いたければ、自分でビルドするしかないらしく、かつて一度ビルドしようとして失敗して、あきらめてAACエンコーディングを行わない方針をとっていた。
TVのTSファイルをmp4にエンコードするのに、-c:a copyで音声部分はエンコードしないで無劣化音声をそのまま使うようにしていた。ただ、それだとエラーが出てmp4エンコードできないファイルを発見したので、再びFDK-AAC付きのビルドに挑戦することにした。
(以下のようなメッセージが出てエンコードが開始しない。)

Last message repeated 6 times
[mp4 @ 000001af4198b2c0] Error applying bitstream filters to an output packet for stream #1: Not yet implemented in FFmpeg, patches welcome
av_interleaved_write_frame(): Not yet implemented in FFmpeg, patches welcome
[mp4 @ 000001af4198b2c0] Error applying bitstream filters to an output packet for stream #1: Not yet implemented in FFmpeg, patches welcome
av_interleaved_write_frame(): Not yet implemented in FFmpeg, patches welcome
[mp4 @ 000001af4198b2c0] Error applying bitstream filters to an output packet for stream #1: Not yet implemented in FFmpeg, patches welcome
av_interleaved_write_frame(): Not yet implemented in FFmpeg, patches welcome
[mp4 @ 000001af4198b2c0] Error applying bitstream filters to an output packet for stream #1: Not yet implemented in FFmpeg, patches welcome
av_interleaved_write_frame(): Not yet implemented in FFmpeg, patches welcome
[mp4 @ 000001af4198b2c0] Error applying bitstream filters to an output packet for stream #1: Not yet implemented in FFmpeg, patches welcome
av_interleaved_write_frame(): Not yet implemented in FFmpeg, patches welcome
[mp4 @ 000001af4198b2c0] Error applying bitstream filters to an output packet for stream #1: Not yet implemented in FFmpeg, patches welcome
av_interleaved_write_frame(): Not yet implemented in FFmpeg, patches welcome
[mp4 @ 000001af4198b2c0] Error applying bitstream filters to an output packet for stream #1: Not yet implemented in FFmpeg, patches welcome
av_interleaved_write_frame(): Not yet implemented in FFmpeg, patches welcome
[mp4 @ 000001af4198b2c0] Error applying bitstream filters to an output packet for stream #1: Not yet implemented in FFmpeg, patches welcome
av_interleaved_write_frame(): Not yet implemented in FFmpeg, patches welcome

先に結論

ffmpeg-windows-build-helpersを使う

Windows Subsystem for Linux (WSL) 上でWindows用のffmpegをビルドしてくれるスクリプト
github.com
Windows用FFmpegをWindows 10でビルドする (Bash on Ubuntu on Windows)
ffmpeg-windows-build-helpersの使い方 | EncTools
ffmpeg-windows-build-helpersでWindows版ffmpegをビルドする | つくみ島だより←最も参考にした。

MSYS2/MinGwを使ってビルドしようと苦戦するより、クロスコンパイル(linuxからwindows用プログラムをコンパイルする)できるならそうした方が謎のエラーに格闘して無駄な時間を過ごさなくていいと学んだ。

実行したコマンド

$ ./cross_compile_ffmpeg.sh --build-ffmpeg-static=y --build-intel-qsv=y --disable-nonfree=n

dllを使わず単体にしたいから--build-ffmpeg-static=y、fdk-aacを使いたいから--disable-nonfree=n。
そうしたらいろいろ不足しているパッケージを要求される。
大抵のパッケージはlinuxbrewからでもインストールできるが、 clangとpaxはlinuxbrewでは提供されていないので、それだけはaptで入れなければいけない。

$ sudo apt install clang pax

あと、パッケージが揃っていてもなんか実行前にエラーが出るときは

$ sudo bash -c 'echo 0 > /proc/sys/fs/binfmt_misc/WSLInterop'

を実行する(この状態は再起動すると元に戻る。)。

あと、linuxbrewでインストールする際に、pyenvのバージョンが上がったせいで、pythonが呼び出せなくなって一度スクリプトが異常停止した。
pyenvがバージョンアップしたときには

pyenv rehash

を実行して環境を更新しなければいけない。*1
参考:pyenvをアップデートしたらlibexec/pyenv: No such file or directoryとか言われた話 | エンジニアもどきの技術メモ

3時間くらい?して、ビルドが完了し、

ffmpeg-windows-build-helpers\sandbox\win64\ffmpeg_git_with_fdk_aac

以下にffmpeg.exe などのファイルができているので、それをWindowsのパスの通ったディレクトリに移して終了。

杜撰ブログ : WSLのUbuntuでWindows用ffmpegをビルドするPart2←でもうまくいかない例もあるようだ。


失敗したルート

msys2のmingwで自前でビルドする

参考情報
fdk-aacとx264が使えるffmpegをビルドする(Windows)←もっとも参考にした
ffmpegをwindows向けにビルドした方法 - Qiita←msys2の他にVisual Studioでの方法も載ってる
Windows10 でffmpegをコンパイル:ろくさぶろ:So-netブログ

いくらやっても最終的に

$ ./configure --prefix=/mingw64/x86_64-w64-mingw32 --enable-gpl --enable-version3 --enable-nonfree --enable-libfdk-aac --enable-libx264 --extra-ldflags=-static --optflags=-O2
ERROR: libfdk_aac not found

というエラーで停止してしまう。

$ pacman -S mingw-w64-x86_64-fdk-aac

していても、

・fdk-aacコンパイル
cd fdk-aac
./configure --prefix=/mingw64/x86_64-w64-mingw32
make -j3
make install

していてもだ。なぜかfdk-aacライブラリを認識してくれない。

media-autobuild_suite

github.com
QSV 対応の ffmpeg をつくる | ニコラボ←ここでお勧めされていて知った。

灰色のプロンプト画面からの結構長いダイアログに答えていくと最終的に自動ビルドしてくれるスクリプト
途中でビルドが失敗する。

Likely error (tail of the failed operation logfile):
libtoolize: linking file 'm4/ltversion.m4'
libtoolize: linking file 'm4/lt~obsolete.m4'
configure.ac:41: error: installing './ar-lib'; error while making link: Operation not permitted
configure.ac:39: error: installing './compile'; error while making link: Operation not permitted
configure.ac:42: error: installing './config.guess'; error while making link: Operation not permitted
configure.ac:42: error: installing './config.sub'; error while making link: Operation not permitted
configure.ac:26: error: installing './install-sh'; error while making link: Operation not permitted
configure.ac:26: error: installing './missing'; error while making link: Operation not permitted
examples/c/decode/file/Makefile.am: error: installing './depcomp'; error while making link: Operation not permitted
autoreconf: automake failed with exit status: 1
autogen failed. Check D:/media-autobuild_suite/build/flac-git/ab-suite.autogen.log
This is required for other packages, so this script will exit.
  Creating diagnostics file...

All relevant logs have been anonymously uploaded to https://0x0.st/iDAb.zip
Copy and paste [logs.zip](https://0x0.st/iDAb.zip) in the GitHub issue.
Make sure the suite is up-to-date before reporting an issue. It might've been fixed already.
Try running the build again at a later time.

こんなエラーが出る。"Operation not permitted" と出ることから何かしらの権限的なエラーだとは思うのだが。

試していないルート

FFMPEGビルド改自動化シェルスクリプトX0008

ffmpegビルド(AAC+.FDK-AAC)・がぁんさん自動ビルド編:moribitoブロマガ - ブロマガ
ツールテスト-ニコニコミュニティ
古そうなので試していない。

【AAC+】 HE-AAC が使える ffmpeg をつくる方法 - ニコニコ動画研究所←ほかにもビルド自動化スクリプトはあったようだ。これは2012年の話なので現在ではほとんどのスクリプトは削除されたか更新終了したかしたようだ。

*1:こういうことがあるから私はpythonをグルー言語として使うことには否定的だ。python本体やpyenvがバージョンアップするとパスが変わって動かなくなるということが以前にもあって、気づかないうちに動かなくなってるの怖って思った。