Skip to content

Conversation

@mscheltienne
Copy link

Closes #570

This PR adds the homebrew FFmpeg path to rpath at build. It should not impact in any form the current resolution of the conda-installed libraries, but will help users of plain pip or uv with homebrew. I tested locally with the following:

$ brew install cmake pkg-config pybind11 ffmpeg
$ uv venv --python 3.13
$ source .venv/bin/activate
$ uv pip install --pre torch --index-url https://download.pytorch.org/whl/nightly/cpu
$ export TORCHCODEC_CMAKE_BUILD_DIR="${PWD}/build"
$ uv pip install -e ".[dev]" --no-build-isolation -vv

Resulting install shows:

❯ otool -l src/torchcodec/libtorchcodec_core*.dylib | grep -A2 LC_RPATH
          cmd LC_RPATH
      cmdsize 48
         path /opt/homebrew/opt/ffmpeg/lib (offset 12)

And import works:

❯ python -c "import torchcodec; print('Success!')"
Success!

And locally, default tests passes with:

$ uv pip install --pre torchvision --index-url https://download.pytorch.org/whl/nightly/cpu
$ pytest
>>> 1152 passed, 399 skipped, 30 deselected in 128.39s (0:02:08)

@meta-cla
Copy link

meta-cla bot commented Jan 6, 2026

Hi @mscheltienne!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@meta-cla
Copy link

meta-cla bot commented Jan 6, 2026

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@meta-cla meta-cla bot added the CLA Signed This label is managed by the Meta Open Source bot. label Jan 6, 2026
Comment on lines +60 to +65
if(DEFINED ENV{HOMEBREW_PREFIX})
set(HOMEBREW_FFMPEG_LIB "$ENV{HOMEBREW_PREFIX}/opt/ffmpeg/lib")
else()
# Default Homebrew location on Apple Silicon
set(HOMEBREW_FFMPEG_LIB "/opt/homebrew/opt/ffmpeg/lib")
endif()
Copy link
Contributor

@NicolasHug NicolasHug Jan 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noting that the handling of a custom HOMEBREW_PREFIX is only going to work for users building from source. For users installing the built wheels with e.g. pip install, the HOMEBREW_PREFIX will be hard-coded to opt/homebrew/opt/ffmpeg/lib which may or may not be the current location of the user's homebrew.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly, can you think of a better alternative? I included the HOMEBREW_PREFIX path specifically for this case where a user would use homebrew in a non-default location, in which case he will be able to build from source.

endif()
set_target_properties(${library_name} PROPERTIES
INSTALL_RPATH "${HOMEBREW_FFMPEG_LIB}"
)
Copy link
Contributor

@NicolasHug NicolasHug Jan 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On this:

It should not impact in any form the current resolution of the conda-installed libraries

I think the change will impact the resolution of conda-installed ffmpeg, because (IIUC) the rpath will now take precedence for the path resolution order. So, if FFmpeg is installed in both the conda env and with homebrew, it's the homebrew FFmpeg that is going to be resolved first.

Copy link
Author

@mscheltienne mscheltienne Jan 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you are right. From what I could figure out, it seems that on main, libtorchcodec_core*.dylib doesn't have any LC_RPATH entries and thus the resolution relies on torch's rpath entries (which include conda paths and thus torchcodec finds the conda FFmpeg.

With this change, libtorchcodec_core*.dylib now has one LC_RPATH which gets added to the rpath list and likely searched first. Thus, we have the following scenarios:

  • Only conda FFmpeg: no change in behavior, homebrew path is searched first but no lib found, falls back to conda paths through the same path as before.
  • Only homebrew FFmpeg, fails on main, works on this PR.
  • Both installed, on main it would use the conda FFmpeg, on this PR it would likely resolve the homebrew FFmpeg first.

In this last scenario, torchcodec should still work and thus the change should be transparent to the user- the only way I can think of to retain control on which path to search first would be to add the conda path as well, but this is now a more invasive change, something along the lines of:

        set_target_properties(${library_name} PROPERTIES
            INSTALL_RPATH "@loader_path/../../..;${HOMEBREW_FFMPEG_LIB}"
        )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

torchcodec not compatible with brew-installed ffmpeg

2 participants