😇

torchaudio==2.9.1 (torchcodec==0.8.1)をWindowsで使う場合のffmpegのエラーの解決法

に公開

最近、torchaudioのバージョンが2.9に上がって、音声読み込みにはtorchcodecが必要になるようになったらしい。
が、Windows環境でどうしてもうまく動かず、色々調べたのでそのメモ。

TL;DR

  • ffmpeg の shared build をインストールする
  • os.add_dll_directoryffmpeg\bin を追加してから import torchcodec する

あるいは

  • torchaudioを使わずlibrosaやsoundfileを使え

遭遇したこと

環境

  • uv
  • torchcodec==0.8.1
  • ffmpeg: 手動で C:\ffmpeg\essentials build をインストール済み
    • C:\ffmpeg\bin\ の中には3つの .exe ファイルしか存在しない状況

エラー

uv run python
import torchcodec

これだけでエラー:

>>> import torchcodec
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\<user>\foo\.venv\Lib\site-packages\torchcodec\__init__.py", line 10, in <module>
    from . import decoders, samplers  # noqa
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\<user>\foo\.venv\Lib\site-packages\torchcodec\decoders\__init__.py", line 7, in <module>
    from .._core import AudioStreamMetadata, VideoStreamMetadata
  File "C:\Users\<user>\foo\.venv\Lib\site-packages\torchcodec\_core\__init__.py", line 8, in <module>
    from torchcodec._core.ops import (
  File "C:\Users\<user>\foo\.venv\Lib\site-packages\torchcodec\_core\ops.py", line 69, in load_torchcodec_shared_libraries       
    raise RuntimeError(
RuntimeError: Could not load libtorchcodec. Likely causes:
          1. FFmpeg is not properly installed in your environment. We support
             versions 4, 5, 6, and 7 on all platforms, and 8 on Mac and Linux.
          2. The PyTorch version (2.9.1+cu128) is not compatible with
             this version of TorchCodec. Refer to the version compatibility
             table:
             https://github.com/pytorch/torchcodec?tab=readme-ov-file#installing-torchcodec.
          3. Another runtime dependency; see exceptions below.
        The following exceptions were raised as we tried to load libtorchcodec:

[start of libtorchcodec loading traceback]
FFmpeg version 8: Could not load this library: C:\Users\<user>\foo\.venv\Lib\site-packages\torchcodec\libtorchcodec_core8.dll    
FFmpeg version 7: Could not load this library: C:\Users\<user>\foo\.venv\Lib\site-packages\torchcodec\libtorchcodec_core7.dll    
FFmpeg version 6: Could not load this library: C:\Users\<user>\foo\.venv\Lib\site-packages\torchcodec\libtorchcodec_core6.dll    
FFmpeg version 5: Could not load this library: C:\Users\<user>\foo\.venv\Lib\site-packages\torchcodec\libtorchcodec_core5.dll    
FFmpeg version 4: Could not load this library: C:\Users\<user>\foo\.venv\Lib\site-packages\torchcodec\libtorchcodec_core4.dll    
[end of libtorchcodec loading traceback].

また、以下のIssueに書いたが、みんなだいすきtransformersライブラリで音声認識をさせようとすると、「もしtorchcodecが環境にインストールされていたら」その瞬間に上と同じエラーで書き起こしができない:

from transformers import pipeline

transcriber = pipeline(
    task="automatic-speech-recognition", model="openai/whisper-small"
)
result = transcriber("example.wav")
print(f"transcription: {result['text']}")

しかしtorchcodecをアンインストールすれば正常に書き起こしできる。呪いのアイテム。

https://github.com/huggingface/transformers/issues/42499

原因

  • ffmpeg の essentials buildでは torchcodec が動かず、shared buildが必要 (少なくともWindowsと現在のtorchcodec 0.8.1では)
  • しかも、それだけ(インストール & PATHに追加)ではダメで、手動で os.add_dll_directoryffmpeg\bin を追加する必要がある

解決策

  • ffmpegshared buildhttps://www.gyan.dev/ffmpeg/builds/ などからダウンロードしてインストール、例えば C:\ffmpeg\ に展開
    • すると、 C:\ffmpeg\bin\.dll ファイルがたくさん入っているはず
  • 以下のように、torchcodec をインポートする前に os.add_dll_directoryffmpeg\bin を追加する
import os
os.add_dll_directory(r"C:\ffmpeg\bin")
import torchcodec

これでやっとエラー無しで通った……

公式のドキュメントには??

https://github.com/meta-pytorch/torchcodec#windows

On Windows (experimental support), you'll need to rely on conda to install both pytorch and TorchCodec: ...

のように、 condaを使え と書いてあって、conda使いたくないので無視していたらこういうことになりました。

代替案

torchaudioを使わず、librosaやsoundfileを使う

参考

Discussion