From 03cde67c72d401c5c656a6cd2bb55c2da5af8026 Mon Sep 17 00:00:00 2001 From: Alberto Sottile Date: Sat, 23 Jan 2021 15:40:40 +0100 Subject: [PATCH] Re-enable youtube-dl on macOS plus mpv and IINA (#381) * Determine correct PYTHONPATH for youtube-dl and pass it to mpv subprocess (follows c07206c) * Homebrew on arm64 has a different prefix --- syncplay/players/mpv.py | 16 +++++++++++++--- .../python_mpv_jsonipc/python_mpv_jsonipc.py | 2 +- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/syncplay/players/mpv.py b/syncplay/players/mpv.py index 678eda0..585d442 100755 --- a/syncplay/players/mpv.py +++ b/syncplay/players/mpv.py @@ -590,15 +590,25 @@ class MpvPlayer(BasePlayer): # to allow that version of python to be executed in the mpv subprocess. if isMacOS(): try: - pythonLibs = subprocess.check_output(['/usr/bin/python', '-E', '-c', + env['PATH'] = '/opt/homebrew/bin:/usr/local/bin:/usr/bin' + ytdl_path = subprocess.check_output(['which', 'youtube-dl'], text=True, env=env).rstrip('\n') + with open(ytdl_path, 'rb') as f: + ytdl_shebang = f.readline() + ytdl_python = ytdl_shebang.decode('utf-8').lstrip('!#').rstrip('\n') + if '/usr/bin/env' in ytdl_python: + python_name = ytdl_python.split(' ')[1] + python_executable = subprocess.check_output(['which', python_name], text=True, env=env).rstrip('\n') + else: + python_executable = ytdl_python + pythonLibs = subprocess.check_output([python_executable, '-E', '-c', 'import sys; print(sys.path)'], text=True, env=dict()) pythonLibs = ast.literal_eval(pythonLibs) pythonPath = ':'.join(pythonLibs[1:]) - except: + except Exception as e: pythonPath = None if pythonPath is not None: - env['PATH'] = '/usr/bin:/usr/local/bin' + env['PATH'] = python_executable + ':' + env['PATH'] env['PYTHONPATH'] = pythonPath try: socket = self.mpv_arguments.get('input-ipc-server') diff --git a/syncplay/vendor/python_mpv_jsonipc/python_mpv_jsonipc.py b/syncplay/vendor/python_mpv_jsonipc/python_mpv_jsonipc.py index 64b3dc2..5c8b442 100644 --- a/syncplay/vendor/python_mpv_jsonipc/python_mpv_jsonipc.py +++ b/syncplay/vendor/python_mpv_jsonipc/python_mpv_jsonipc.py @@ -211,7 +211,7 @@ class MPVProcess: self._start_process(ipc_socket, args, env=env) def _start_process(self, ipc_socket, args, env): - self.process = subprocess.Popen(args) + self.process = subprocess.Popen(args, env=env) ipc_exists = False for _ in range(100): # Give MPV 10 seconds to start. time.sleep(0.1)