Torchaudio is not able to access FFmpeg

I am new to torchaudio, and I am trying to use StreamReader to get data from my microphone, but I cannot get it to start because it can’t access FFmpeg.

from torchaudio.io import StreamReader

streamer = StreamReader(
    src='audio=@device_cm_{33D9A762-90C8-11D0-BD43-00A0C911CE86}\wave_{A531F508-ED15-42F3-A46F-9C8ADB26C05A}',
    format='dshow',
)
for i in range(streamer.num_src_streams):
    print(streamer.get_src_stream_info(i))
ImportError                               Traceback (most recent call last)
Cell In[2], line 4
      1 import matplotlib.pyplot as plt 
      2 from torchaudio.io import StreamReader
----> 4 streamer = StreamReader(
      5     src='audio=@device_cm_{33D9A762-90C8-11D0-BD43-00A0C911CE86}\wave_{A531F508-ED15-42F3-A46F-9C8ADB26C05A}',
      6     format='dshow',
      7 )

File d:\Python\torchaudio\.venv\Lib\site-packages\torio\io\_streaming_media_decoder.py:526, in StreamingMediaDecoder.__init__(self, src, format, option, buffer_size)
    524     self._be = ffmpeg_ext.StreamingMediaDecoderFileObj(src, format, option, buffer_size)
    525 else:
--> 526     self._be = ffmpeg_ext.StreamingMediaDecoder(os.path.normpath(src), format, option)
    528 i = self._be.find_best_audio_stream()
    529 self._default_audio_stream = None if i < 0 else i

File d:\Python\torchaudio\.venv\Lib\site-packages\torio\_extension\utils.py:25, in _LazyImporter.__getattr__(self, item)
     24 def __getattr__(self, item):
---> 25     self._import_once()
     26     return getattr(self.module, item)

File d:\Python\torchaudio\.venv\Lib\site-packages\torio\_extension\utils.py:39, in _LazyImporter._import_once(self)
     37 def _import_once(self):
     38     if self.module is None:
---> 39         self.module = self.import_func()
     40         # Note:
     41         # By attaching the module attributes to self,
     42         # module attributes are directly accessible.
     43         # This allows to avoid calling __getattr__ for every attribute access.
     44         self.__dict__.update(self.module.__dict__)

File d:\Python\torchaudio\.venv\Lib\site-packages\torio\_extension\utils.py:143, in _init_ffmpeg()
    141 def _init_ffmpeg():
    142     ffmpeg_vers = _get_ffmpeg_versions()
--> 143     ext = _find_ffmpeg_extension(ffmpeg_vers)
    144     ext.init()
    145     if ext.get_log_level() > 8:

File d:\Python\torchaudio\.venv\Lib\site-packages\torio\_extension\utils.py:122, in _find_ffmpeg_extension(ffmpeg_vers)
    120         _LG.debug("Failed to load FFmpeg%s extension.", ffmpeg_ver, exc_info=True)
    121         continue
--> 122 raise ImportError(
    123     f"Failed to intialize FFmpeg extension. Tried versions: {ffmpeg_vers}. "
    124     "Enable DEBUG logging to see more details about the error."
    125 )

ImportError: Failed to intialize FFmpeg extension. Tried versions: ['6', '5', '4', '']. Enable DEBUG logging to see more details about the error.

Is the error I get. I am on windows and the version I have installed for FFmpeg is 6.1.1.


I have tried to look this up but I cannot find anyone with a similar issue. I am on windows 10 too, any help would be appreciated.
Thank you.

1 Like

I have the same probrem. I turn on DEBUG msg.

DEBUG:torio._extension.utils:Loading FFmpeg6
DEBUG:torio._extension.utils:Failed to load FFmpeg6 extension.
Traceback (most recent call last):
  File "C:\Users\XXXXXX\.venv\gijiroku\Lib\site-packages\torio\_extension\utils.py", line 116, in _find_ffmpeg_extension
    ext = _find_versionsed_ffmpeg_extension(ffmpeg_ver)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\XXXXXX\.venv\gijiroku\Lib\site-packages\torio\_extension\utils.py", line 108, in _find_versionsed_ffmpeg_extension
    _load_lib(lib)
  File "C:\Users\XXXXXX\.venv\gijiroku\Lib\site-packages\torio\_extension\utils.py", line 94, in _load_lib
    torch.ops.load_library(path)
  File "C:\Users\XXXXXX\.venv\gijiroku\Lib\site-packages\torch\_ops.py", line 1032, in load_library
    ctypes.CDLL(path)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.12_3.12.1008.0_x64__qbz5n2kfra8p0\Lib\ctypes\__init__.py", line 379, in __init__  
    self._handle = _dlopen(self._name, mode)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: Could not find module 'C:\Users\XXXXXX\.venv\gijiroku\Lib\site-packages\torio\lib\libtorio_ffmpeg6.pyd' (or one of its dependencies). Try using the full path with constructor syntax.

Installing pre-built binaries — Torchaudio 2.3.0 documentation (pytorch.org)

When searching for FFmpeg installation, TorchAudio looks for library files which have names with version numbers. That is, libavutil.so.<VERSION> for Linux, libavutil.<VERSION>.dylib for macOS, and avutil-<VERSION>.dll for Windows.