66
77import io
88import json
9+ import os
10+ import shutil
11+ import sys
912import warnings
13+ from contextlib import nullcontext
14+ from pathlib import Path
1015from types import ModuleType
1116from typing import List , Optional , Tuple , Union
1217
2227_pybind_ops : Optional [ModuleType ] = None
2328
2429
25- def load_torchcodec_shared_libraries ():
30+ def load_torchcodec_shared_libraries () -> tuple [ int , str ] :
2631 # Successively try to load the shared libraries for each version of FFmpeg
2732 # that we support. We always start with the highest version, working our way
2833 # down to the lowest version. Once we can load ALL shared libraries for a
@@ -70,7 +75,8 @@ def load_torchcodec_shared_libraries():
7075 raise RuntimeError (
7176 f"""Could not load libtorchcodec. Likely causes:
7277 1. FFmpeg is not properly installed in your environment. We support
73- versions 4, 5, 6, 7, and 8.
78+ versions 4, 5, 6, 7, and 8. On Windows, ensure you've installed
79+ the "full-shared" version which ships DLLs.
7480 2. The PyTorch version ({ torch .__version__ } ) is not compatible with
7581 this version of TorchCodec. Refer to the version compatibility
7682 table:
@@ -82,7 +88,20 @@ def load_torchcodec_shared_libraries():
8288 )
8389
8490
85- ffmpeg_major_version , core_library_path = load_torchcodec_shared_libraries ()
91+ expose_ffmpeg_dlls = nullcontext
92+ if sys .platform == "win32" and hasattr (os , "add_dll_directory" ):
93+ # On windows we try to locate the FFmpeg DLLs and temporarily add them to
94+ # the DLL search path. This seems to be needed on some users machine, but
95+ # not on our CI. We don't know why.
96+ if ffmpeg_path := shutil .which ("ffmpeg" ):
97+
98+ def expose_ffmpeg_dlls (): # noqa: F811
99+ ffmpeg_dir = Path (ffmpeg_path ).parent
100+ return os .add_dll_directory (str (ffmpeg_dir )) # that's the actual CM
101+
102+
103+ with expose_ffmpeg_dlls ():
104+ ffmpeg_major_version , core_library_path = load_torchcodec_shared_libraries ()
86105
87106
88107# Note: We use disallow_in_graph because PyTorch does constant propagation of
0 commit comments