marzzzello / mpv_thumbnail_script

A Lua script to show preview thumbnails in mpv's OSC seekbar, sans external dependencies (fork)
GNU General Public License v3.0
276 stars 20 forks source link

Locate mpv, ffmpeg binaries on macOS (resolves #29). #39

Open rucker opened 1 year ago

rucker commented 1 year ago

Add manual lookup of binaries on macOS due to the fact that the user's PATH isn't exposed to GUI applications, considering the following scenarios:

  1. Homebrew: Homebrew's prefix differs between Intel Macs (/usr/local/) and Apple Silicon Macs (/opt/homebrew/)
  2. Without Homebrew: mpv.app could be installed at /Applications or ~/Applications (likely the former). The mpv binary itself lives here -- Homebrew just symlinks to it.
SemperPeritus commented 1 year ago

I almost don't know but think something like this will be better. Not sure it works in mpv script environment.

function find_existing_path(paths)
    for _, path in ipairs(paths) do
        if file_exists(path) then
            return path
        end
    end
    return nil
end
ffmpeg_path = "ffmpeg"
if ON_MAC then
  ffmpeg_path = find_existing_path({"/opt/homebrew/bin/ffmpeg", "/usr/local/bin/ffmpeg"})

Or may be even

ffmpeg_path = find_existing_path({"ffmpeg", "/opt/homebrew/bin/ffmpeg", "/usr/local/bin/ffmpeg"})
rucker commented 1 year ago

@SemperPeritus I like the idea! I pushed a change that mostly does what you suggested but still handles Mac as a special case. It also looks for ffmpeg at HOME/bin since that's one suggested prefix in their compilation guide.

We could take that a step further and just use the ExecutableFinder on all platforms but that would require some further refactoring. Right now it looks like ExecutableFinder:get_executable_path() calls find_executable() but its return value is only used in a truthy context: i.e. if something is returned, we know we can just invoke it by name later.

@marzzzello Do I have this right? Do you have a strong opinion?

po5 commented 1 year ago

If your mpv is already in PATH, this is pointless. Just launch the subprocess with mpv/ffmpeg, it will resolve.

If you didn't install via Homebrew, and are using an app bundle, this won't work. You'll need to locate the currently mpv executable, and strip -bundle to use the command line version. The app bundle can be located anywhere, it may not be in Applications. https://github.com/po5/thumbfast/blob/ccec64d13883fdd509244bb1b19db4b5ac1dc402/thumbfast.lua#L261-L263

Note that launching cli mpv from within an app bundle results in an icon quickly appearing and disappearing in Dock. If this is undesirable (it most certainly is since this script spawns one process per thumbnail), then you have to make a symlink to it from outside the app bundle. Then there won't be a Dock icon. File I linked has logic for this, and for notifying the user of it.