Skip to content

Commit 0f09518

Browse files
aboood40091NicolasHug
authored andcommitted
Fix load_torchcodec_shared_libraries on Windows (#1109)
Co-authored-by: Nicolas Hug <[email protected]>
1 parent ce82c7c commit 0f09518

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

src/torchcodec/_core/ops.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@
66

77
import io
88
import json
9+
import os
10+
import shutil
11+
import sys
912
import warnings
13+
from contextlib import nullcontext
14+
from pathlib import Path
1015
from types import ModuleType
1116
from typing import List, Optional, Tuple, Union
1217

@@ -22,7 +27,7 @@
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

Comments
 (0)