pytorch / audio

Data manipulation and transformation for audio signal processing, powered by PyTorch
https://pytorch.org/audio
BSD 2-Clause "Simplified" License
2.55k stars 656 forks source link

TorchAudio does not pick up ffmpeg #3016

Closed zdposter closed 1 year ago

zdposter commented 1 year ago

🐛 Describe the bug

import torch
import torchaudio
from torchaudio.io import StreamReader

crashed with:

    from torchaudio.io import StreamReader
  File "C:\Python\Python39\lib\site-packages\torchaudio\io\__init__.py", line 21, in __getattr__
    torchaudio._extension._init_ffmpeg()
  File "C:\Python\Python39\lib\site-packages\torchaudio\_extension.py", line 73, in _init_ffmpeg
    raise ImportError("FFmpeg libraries are not found. Please install FFmpeg.") from err
ImportError: FFmpeg libraries are not found. Please install FFmpeg.

I installed torchaudio with:

pip install -U torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu116

Output for ffmpeg:

> ffmpeg -version
ffmpeg version 4.3.2-2021-02-27-full_build-www.gyan.dev Copyright (c) 2000-2021 the FFmpeg developers
built with gcc 10.2.0 (Rev6, Built by MSYS2 project)
configuration: --enable-gpl --enable-version3 --enable-static --disable-w32threads --disable-autodetect --enable-fontconfig --enable-iconv --enable-gnutls --enable-libxml2 --enable-gmp --enable-lzma --enable-libsnappy --enable-zlib --enable-libsrt --enable-libssh --enable-libzmq --enable-avisynth --enable-libbluray --enable-libcaca --enable-sdl2 --enable-libdav1d --enable-libzvbi --enable-librav1e --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxvid --enable-libaom --enable-libopenjpeg --enable-libvpx --enable-libass --enable-frei0r --enable-libfreetype --enable-libfribidi --enable-libvidstab --enable-libvmaf --enable-libzimg --enable-amf --enable-cuda-llvm --enable-cuvid --enable-ffnvcodec --enable-nvdec --enable-nvenc --enable-d3d11va --enable-dxva2 --enable-libmfx --enable-libcdio --enable-libgme --enable-libmodplug --enable-libopenmpt --enable-libopencore-amrwb --enable-libmp3lame --enable-libshine --enable-libtheora --enable-libtwolame --enable-libvo-amrwbenc --enable-libilbc --enable-libgsm --enable-libopencore-amrnb --enable-libopus --enable-libspeex --enable-libvorbis --enable-ladspa --enable-libbs2b --enable-libflite --enable-libmysofa --enable-librubberband --enable-libsoxr --enable-chromaprint
libavutil      56. 51.100 / 56. 51.100
libavcodec     58. 91.100 / 58. 91.100
libavformat    58. 45.100 / 58. 45.100
libavdevice    58. 10.100 / 58. 10.100
libavfilter     7. 85.100 /  7. 85.100
libswscale      5.  7.100 /  5.  7.100
libswresample   3.  7.100 /  3.  7.100
libpostproc    55.  7.100 / 55.  7.100

Versions

> python collect_env.py
Collecting environment information...
PyTorch version: 1.13.1+cu116
Is debug build: False
CUDA used to build PyTorch: 11.6
ROCM used to build PyTorch: N/A

OS: Microsoft Windows 10 Pro
GCC version: Could not collect
Clang version: Could not collect
CMake version: version 3.25.2
Libc version: N/A

Python version: 3.9.13 (tags/v3.9.13:6de2ca5, May 17 2022, 16:36:42) [MSC v.1929 64 bit (AMD64)] (64-bit runtime)
Python platform: Windows-10-10.0.19042-SP0
Is CUDA available: True
CUDA runtime version: 11.4.120
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: GPU 0: NVIDIA GeForce GTX 1650
Nvidia driver version: 526.47
cuDNN version: C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.4\bin\cudnn_ops_train64_8.dll
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

Versions of relevant libraries:
[pip3] mypy==0.991
[pip3] mypy-extensions==0.4.3
[pip3] numpy==1.21.6
[pip3] open-clip-torch==2.7.0
[pip3] torch==1.13.1+cu116
[pip3] torchaudio==0.13.1+cu116
[pip3] torchvision==0.14.1+cu116
[conda] Could not collect

cc @peterjc123 @maxluk @nbcsm @guyang3532 @gunandrose4u @smartcat2010 @mszhanyi

mthrok commented 1 year ago

Hi

It seems like you have statically linked ffmpeg command. What TorchAudio requires is not ffmpeg command itself but the library used by ffmpeg, that is libavformat and such. The message says FFmpeg for simplicity.

The website you used to download ffmpeg, https://www.gyan.dev/ffmpeg/builds/ also provides shared libraries, so you can download and place the DLL files to proper locations according to your system's configuration.

zdposter commented 1 year ago

Thank you very much for response. As far as I see gyan.dev/ffmpeg/builds provide 5.x ffmpeg version but Readme mentions this:

Note This software was compiled against an unmodified copy of FFmpeg (licensed under the LGPLv2.1), with the specific rpath removed so as to enable the use of system libraries. The LGPL source can be downloaded here.

which should be 4.1. and it is very old version.

gyan.dev older releases are without version info (there is just build date info)... I am convinced windows wheel of torchaudio should include all necessary libs (that can not be installed with pip) especially if package requires specific version of external library. Another reason for distribution of ffmpeg dlls is change of ctypes behaviour on windows from python 3.8: dll are loaded only from trusted location.

mthrok commented 1 year ago

TorchAudio is complied against FFmpeg 4.1, so that it works with all the versions from 4.1 to 4.4. FFmpeg 5 was too new when we implemented the feature. We do not want to drop the support for FFmpeg 4 yet, but supporting both 4 and 5 requires major work, and we do not have a bandwidth to do so at the moment. If you want to use FFmpeg 5, you can build torchaudio from source.

We cannot always include third party libraries due to licenses they impose.

zdposter commented 1 year ago

I do not want to use FFmpeg 5 - I want to use TorchAudio on Windows ;-) The whole process for making TorchAudio work is quite frustrating: e.g. bucket401.blogspot.com suggest to use ffmpeg-n4.3.2-160-gfbb9368226-win64-lgpl-shared-4.3.zip but with this version torch.classes.torchaudio.ffmpeg_StreamReader (later) raise another error: RuntimeError: Unsupported device/format: "avfoundation"...

So FFmpeg=< 4.1<4.4 is not sufficient description for requirement... It also need support for avfoundation. Can you just provide link to FFmpeg(compiled) version that will work with TorchAudio?

I understand you cannot always include third party libraries, but for me it looks like I can not use TorchAudio without FFmpeg. And I see a lot other projects to distribute ffmpeg (at least I found this program installing ffmpeg.dll on my computer: VS Code, MS Teams, Skype, Edge, LogiTune, oneAPI, Dropbox, Obsidian...)

zdposter commented 1 year ago

I just find out that problem with "avfoundation" is problem of script I am testing (AVFoundation is the currently recommended framework by Apple for streamgrabbing on OSX >= 10.7 as well as on iOS.), but rest of points (you you can not distribute it, provide link to package that works with TorchAudio) are valid

ASatWeinmannEMT commented 2 months ago

I have been confused by this as well. What helped me is to bind the ffmpeg dlls like so :

import os ffmpeg_path = r"C:\localTools\ffmpeg-n5.1.3-12-g3344d47a88-win64-lgpl-shared-5.1\bin" os.environ["PATH"] += os.pathsep + ffmpeg_path os.add_dll_directory(ffmpeg_path)

I did not been aware of the os.add_dll_directrory

hope this helps.

happyTonakai commented 1 week ago

According to https://pytorch.org/audio/master/installation.html conda install -c conda-forge 'ffmpeg<7' works for me.