pythonpython-3.xffmpegctypestorchaudio

torchaudio can't find FFmpeg


Windows, vscode, Python 3.11.4-64bit

import torch
import torchaudio

print(torch.__version__)
print(torchaudio.__version__)
print(torchaudio._extension._FFMPEG_INITIALIZED)
2.0.1+cu117
2.0.2+cu117
False

and i try

torchaudio._extension._init_ffmpeg()
Traceback (most recent call last):
  File "C:\Users\USER\AppData\Local\Programs\Python\Python311\Lib\site-packages\torchaudio\_extension\utils.py", line 85, in _init_ffmpeg
    _load_lib("libtorchaudio_ffmpeg")
  File "C:\Users\USER\AppData\Local\Programs\Python\Python311\Lib\site-packages\torchaudio\_extension\utils.py", line 61, in _load_lib
    torch.ops.load_library(path)
  File "C:\Users\USER\AppData\Local\Programs\Python\Python311\Lib\site-packages\torch\_ops.py", line 643, in load_library
    ctypes.CDLL(path)
  File "C:\Users\USER\AppData\Local\Programs\Python\Python311\Lib\ctypes\__init__.py", line 376, in __init__
    self._handle = _dlopen(self._name, mode)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: Could not find module 'C:\Users\USER\AppData\Local\Programs\Python\Python311\Lib\site-packages\torchaudio\lib\libtorchaudio_ffmpeg.pyd' (or one of its dependencies). Try using the full path with constructor syntax.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "c:\Users\USER\Desktop\MyChatBot\0.py", line 6, in <module>
    torchaudio._extension._init_ffmpeg()
  File "C:\Users\USER\AppData\Local\Programs\Python\Python311\Lib\site-packages\torchaudio\_extension\utils.py", line 87, in _init_ffmpeg
    raise ImportError("FFmpeg libraries are not found. Please install FFmpeg.") from err
ImportError: FFmpeg libraries are not found. Please install FFmpeg.

my libtorchaudio_ffmpeg.pyd exists and it's path is correct, but ctypes can't seem to find it. how should i do?

I have also downloaded ffmpeg.

C:\Users\USER>ffmpeg
ffmpeg version 4.3.1 Copyright (c) 2000-2020 the FFmpeg developers
  built with gcc 10.2.1 (GCC) 20200726

but still not working. how should i do?


Solution

  • You need to install ffmpeg libraries, not CLI.

    What the error message means is that the dependencies of libtorchaudio_ffmpeg.pyd is not found. The dependencies here mean the libraries that consist FFmpeg, such as libavcodec and libavformat.

    Usually installing ffmpeg CLI also intall the libraries, but I often see people use statically built ffmpeg CLI on Windows due to its ease of installation.

    I cannot tell what ffmpeg CLI you have, but if your ffmpeg CLI is statically built, it does not work.

    As of how to install dynamically built FFmpeg, I cannot advise. There seems to be no straightforward way to install dynamically built FFmpeg on Windows unless using Anacaonda.