0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Windows向けのほぼ全部入りのffmpegをmsys2を使いビルドする話

0
Posted at

やっぱり欲しい全部入りffmpeg

ビルドする遊びの一環で配布されていない、全部入りのffmpegをビルドしてみたかった。
fdkaacなどノンフリーなライセンスの機能は公式、または公式に準ずるサイトではバイナリが提供されていません。ライセンス違反になってしまうので。
それで、どうしたら良いかというと、自分でバイナリを作る、すなわちビルドをする、という方法です。今回は環境構築などをGoogle Geminiにお手伝いをしてもらいました。いわゆるスタティック版ではなく、dllが分離する形を取りました。

では作っていきましょう

MSYS2の黄色っぽいアイコン(UCRT64)の環境で作業していきます。Windows10以降のモダンな環境により一層Windowsに適応しているとGeminiが言ってました。

pacman -Syu

でまずはパッケージデータベースの更新と古いパッケージの更新をします。
そしてビルドツールを一式入れます。下のコマンド。

pacman -S --needed base-devel mingw-w64-ucrt-x86_64-toolchain \
          git python nasm yasm cmake

ハードウェアエンコードを有効にするため、以下をインストールします。AMFがAMD、NVCODECがNVIDIA、oneVPLがINTELのGPUにそれぞれ対応しています。

pacman -S --needed \
mingw-w64-ucrt-x86_64-amf-headers \
mingw-w64-ucrt-x86_64-ffnvcodec-headers \
mingw-w64-ucrt-x86_64-onevpl

入れたら次は主なコーデック類を入れていきます。以下のコマンドで。

pacman -S --needed \
mingw-w64-ucrt-x86_64-lame \
mingw-w64-ucrt-x86_64-x264 \
mingw-w64-ucrt-x86_64-x265 \
mingw-w64-ucrt-x86_64-libvpx \
mingw-w64-ucrt-x86_64-fdk-aac \
mingw-w64-ucrt-x86_64-opus \
mingw-w64-ucrt-x86_64-dav1d \
mingw-w64-ucrt-x86_64-aom \
mingw-w64-ucrt-x86_64-libass \
mingw-w64-ucrt-x86_64-freetype \
mingw-w64-ucrt-x86_64-fontconfig \
mingw-w64-ucrt-x86_64-fribidi \
mingw-w64-ucrt-x86_64-zimg \
mingw-w64-ucrt-x86_64-libvorbis \
mingw-w64-ucrt-x86_64-libwebp \
mingw-w64-ucrt-x86_64-gnutls \
mingw-w64-ucrt-x86_64-SDL2 \
mingw-w64-ucrt-x86_64-libsoxr

入りました。ffmpegのソースを落としてきます。

cd ~
rm -rf ffmpeg  # 既存のディレクトリがある場合は削除
git clone https://git.ffmpeg.org/ffmpeg.git ffmpeg
cd ffmpeg

configureスクリプトを走らせてMakefileを作ります。

./configure \
  --prefix=/usr/local \
  --enable-gpl \
  --enable-version3 \
  --enable-nonfree \
  --enable-shared \
  --disable-static \
  --enable-amf \
  --enable-nvenc \
  --enable-libvpl \
  --enable-libx264 \
  --enable-libx265 \
  --enable-libvpx \
  --enable-libfdk-aac \
  --enable-libmp3lame \
  --enable-libopus \
  --enable-libdav1d \
  --enable-libaom \
  --enable-libass \
  --enable-libzimg \
  --enable-libsoxr \
  --enable-libvorbis \
  --enable-libwebp \
  --enable-gnutls \
  --enable-sdl2 \
  --extra-cflags="-I/ucrt64/include" \
  --extra-ldflags="-L/ucrt64/lib"

成功すると対応するコーデックやフィルタなどがずらーっと並んで、そしてスクリプトが終了します。成功しているとMakefileができていますのでmakeします。

make -j$(nproc)
make install

ffmpegのWindowsバイナリを作る作業としては以上になります。でもこのままだとmsys2上でしか上手く動かないので、ffmpegのバイナリと使用するDLL群を持ってきます。「ffmpeg_dist」みたいなディレクトリを作って、ffmpeg.exeを入れて、そのディレクトリ内で、以下スクリプトで。

#!/bin/bash
# 1. 念のためビルドされたavcodec等の本体DLLをコピー
echo "Copying FFmpeg core DLLs..."
cp /usr/local/bin/*.dll .

# 2. ntlddの出力を整形して1つずつコピーする
echo "Collecting dependencies..."
# ntldd -R の出力を受け取り、パス部分を抽出。
# バックスラッシュをスラッシュに変換し、/c/Windows 以外をコピー。
ntldd -R ffmpeg.exe | awk -F' ' '{print $3}' | sed 's/\\/\//g' | grep -i "/bin/" | sort -u | while read -r line; do
    if [ -f "$line" ]; then
        echo "Copying: $line"
        cp "$line" .
    else
        # パスが不完全な場合、/ucrt64/bin を補完して再試行
        dllname=$(basename "$line")
        if [ -f "/ucrt64/bin/$dllname" ]; then
            echo "Found in /ucrt64/bin: $dllname"
            cp "/ucrt64/bin/$dllname" .
        fi
    fi
done

echo "Finished!"

ntlddがない場合は

pacman -S --needed --noconfirm mingw-w64-ucrt-x86_64-ntldd-git

で入ります。
これで他のディレクトリ(フォルダ、他のPC)に入れる準備が整いました。ディレクトリをZIP等で固めると持ち運び易くなるかと思います。
お疲れ様でした。

なぜスタティックビルドにしなかったのか

エラーでconfigureスクリプトが終わってしまったからですねー。どれだけGeminiに聞いてもダメだったのでダイナミックリンク版にしました。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?