import os import stat import shutil import subprocess import imageio_ffmpeg from logging_config import logger def is_ffmpeg_in_path() -> bool: try: subprocess.run( ["ffmpeg", "-version"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True ) return True except (subprocess.CalledProcessError, FileNotFoundError): return False def ensure_ffmpeg_in_path(): if is_ffmpeg_in_path(): logger.info("FFmpeg is already available in PATH.") return try: ffmpeg_path_original = imageio_ffmpeg.get_ffmpeg_exe() ffmpeg_dir = os.path.dirname(ffmpeg_path_original) binary_name = os.path.basename(ffmpeg_path_original) logger.info(f"imageio-ffmpeg reported path: {ffmpeg_path_original}") logger.info(f"Directory contents: {os.listdir(ffmpeg_dir)}") logger.info(f"Binary name: {binary_name}") expected_binary_name = "ffmpeg" copied_path = os.path.join(ffmpeg_dir, expected_binary_name) if not os.path.exists(copied_path): logger.info(f"Copying {binary_name} to {expected_binary_name} in {ffmpeg_dir}.") shutil.copy2(ffmpeg_path_original, copied_path) st = os.stat(copied_path) os.chmod(copied_path, st.st_mode | stat.S_IEXEC) else: logger.info(f"'{copied_path}' already exists; skipping copy.") # Add directory to PATH os.environ["PATH"] = ffmpeg_dir + os.pathsep + os.environ["PATH"] logger.info(f"PATH updated to include: {ffmpeg_dir}") if is_ffmpeg_in_path(): logger.info("FFmpeg is now accessible in PATH.") else: logger.warning("FFmpeg is still not found in PATH after attempting to add it.") raise RuntimeError("Failed to make FFmpeg accessible in PATH.") except Exception as e: logger.error(f"Failed to ensure FFmpeg is in PATH: {str(e)}") raise RuntimeError("Failed to ensure FFmpeg is in PATH.") from e