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,21 @@ def load_torchcodec_shared_libraries():
8288 )
8389
8490
85- ffmpeg_major_version , core_library_path = load_torchcodec_shared_libraries ()
91+ if sys .platform == "win32" and hasattr (os , "add_dll_directory" ):
92+ # On windows we try to locate the FFmpeg DLLs and temporarily add them to
93+ # the DLL search path. This seems to be needed on some users machine, but
94+ # not on our CI. We don't know why.
95+ if ffmpeg_path := shutil .which ("ffmpeg" ):
96+
97+ def cm (): # noqa: F811
98+ ffmpeg_dir = Path (ffmpeg_path ).parent
99+ return os .add_dll_directory (str (ffmpeg_dir )) # that's the actual CM
100+
101+ else :
102+ cm = nullcontext
103+
104+ with cm ():
105+ ffmpeg_major_version , core_library_path = load_torchcodec_shared_libraries ()
86106
87107
88108# Note: We use disallow_in_graph because PyTorch does constant propagation of
0 commit comments