From 1f8a53406918a64e10851ce5196db79554136870 Mon Sep 17 00:00:00 2001 From: Lovi <62809003+Lovi-0@users.noreply.github.com> Date: Sat, 28 Dec 2024 17:21:55 +0100 Subject: [PATCH] Change ffmpeg check --- StreamingCommunity/Util/ffmpeg_installer.py | 23 +++++++++---- StreamingCommunity/Util/os.py | 36 +++++++++------------ 2 files changed, 32 insertions(+), 27 deletions(-) diff --git a/StreamingCommunity/Util/ffmpeg_installer.py b/StreamingCommunity/Util/ffmpeg_installer.py index fbf8dcd..d10f466 100644 --- a/StreamingCommunity/Util/ffmpeg_installer.py +++ b/StreamingCommunity/Util/ffmpeg_installer.py @@ -288,24 +288,35 @@ def check_ffmpeg() -> Tuple[Optional[str], Optional[str], Optional[str]]: using the FFMPEGDownloader class. """ try: - if platform.system().lower() == 'windows': + system_platform = platform.system().lower() + + # Check for Windows platform + if system_platform == 'windows': + + # Using subprocess.call to check the executables and subprocess.check_output to get the path ffmpeg_path = subprocess.check_output(['where', 'ffmpeg'], text=True).strip().split('\n')[0] if subprocess.call(['where', 'ffmpeg'], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) == 0 else None ffprobe_path = subprocess.check_output(['where', 'ffprobe'], text=True).strip().split('\n')[0] if subprocess.call(['where', 'ffprobe'], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) == 0 else None ffplay_path = subprocess.check_output(['where', 'ffplay'], text=True).strip().split('\n')[0] if subprocess.call(['where', 'ffplay'], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) == 0 else None - + + # If all three executables are found, return their paths if all([ffmpeg_path, ffprobe_path, ffplay_path]): return ffmpeg_path, ffprobe_path, ffplay_path + + # Check for Unix-like systems (Linux, macOS) else: ffmpeg_path = shutil.which('ffmpeg') ffprobe_path = shutil.which('ffprobe') ffplay_path = shutil.which('ffplay') - + + # If all three executables are found, return their paths if all([ffmpeg_path, ffprobe_path, ffplay_path]): return ffmpeg_path, ffprobe_path, ffplay_path - + + # If executables were not found, attempt to download FFmpeg downloader = FFMPEGDownloader() return downloader.download() - + except Exception as e: - logging.error(f"Error checking FFmpeg: {e}") + # Log any unexpected errors that may occur during the check or download process + logging.error(f"Error checking or downloading FFmpeg executables: {e}") return None, None, None \ No newline at end of file diff --git a/StreamingCommunity/Util/os.py b/StreamingCommunity/Util/os.py index d67f6e7..61312e5 100644 --- a/StreamingCommunity/Util/os.py +++ b/StreamingCommunity/Util/os.py @@ -353,20 +353,17 @@ class OsSummary: console.print(f"{command[0]} not found", style="bold red") sys.exit(0) - def check_ffmpeg_location(self, command: list): + def check_ffmpeg_location(self, command: list) -> str: """ - Run 'where ffmpeg' command to check FFmpeg's location. - - Returns: - str: Location of FFmpeg executable or None if not found + Check if a specific executable (ffmpeg or ffprobe) is located using the given command. + Returns the path of the executable or None if not found. """ try: - result = subprocess.check_output(command, stderr=subprocess.STDOUT, text=True).strip() - return result + result = subprocess.check_output(command, text=True).strip() + return result.split('\n')[0] if result else None except subprocess.CalledProcessError: - console.print("FFmpeg not found in system PATH", style="bold red") - sys.exit(0) + return None def get_library_version(self, lib_name: str): """ @@ -467,27 +464,24 @@ class OsSummary: console.print(f"[cyan]Python[white]: [bold red]{python_version} ({python_implementation} {arch}) - {os_info} ({glibc_version})[/bold red]") logging.info(f"Python: {python_version} ({python_implementation} {arch}) - {os_info} ({glibc_version})") - # Usa il comando 'where' su Windows - if platform.system() == "Windows": - command = 'where' + # Use the 'where' command on Windows and 'which' command on Unix-like systems + system_platform = platform.system().lower() + command = 'where' if system_platform == 'windows' else 'which' - # Usa il comando 'which' su Unix/Linux - else: - command = 'which' - - # Locate ffmpeg and ffprobe from path enviroment - if self.ffmpeg_path != None and "binary" not in self.ffmpeg_path: + # Locate ffmpeg and ffprobe from the PATH environment + if self.ffmpeg_path is not None and "binary" not in self.ffmpeg_path: self.ffmpeg_path = self.check_ffmpeg_location([command, 'ffmpeg']) - if self.ffprobe_path != None and "binary" not in self.ffprobe_path: + if self.ffprobe_path is not None and "binary" not in self.ffprobe_path: self.ffprobe_path = self.check_ffmpeg_location([command, 'ffprobe']) - # Locate ffmpeg from bin installation + # If ffmpeg or ffprobe is not located, fall back to using the check_ffmpeg function if self.ffmpeg_path is None or self.ffprobe_path is None: self.ffmpeg_path, self.ffprobe_path, self.ffplay_path = check_ffmpeg() + # If still not found, print error and exit if self.ffmpeg_path is None or self.ffprobe_path is None: - console.log("[red]Cant locate ffmpeg or ffprobe") + console.log("[red]Can't locate ffmpeg or ffprobe") sys.exit(0) ffmpeg_version = self.get_executable_version([self.ffprobe_path, '-version'])