diff --git a/videohelpersuite/utils.py b/videohelpersuite/utils.py index f036d55..d3e710c 100644 --- a/videohelpersuite/utils.py +++ b/videohelpersuite/utils.py @@ -6,15 +6,48 @@ import subprocess from .logger import logger -ffmpeg_path = shutil.which("ffmpeg") -if "VHS_USE_IMAGEIO_FFMPEG" in os.environ or ffmpeg_path is None: +def ffmpeg_suitability(path): + try: + version = subprocess.run([path, "-version"], check=True, + capture_output=True).stdout.decode("utf-8") + except: + return 0 + score = 0 + #rough layout of the importance of various features + simple_criterion = [("libvpx", 20),("264",10), ("265",3), + ("svtav1",5),("libopus", 1)] + for criterion in simple_criterion: + if version.find(criterion[0]) >= 0: + score += criterion[1] + #obtain rough compile year from copyright information + copyright_index = version.find('2000-2') + if copyright_index >= 0: + copyright_year = version[copyright_index+6:copyright_index+9] + if copyright_year.isnumeric(): + score += int(copyright_year) + return score + +if "VHS_FORCE_FFMPEG_PATH" in os.environ: + ffmpeg_path = os.env["VHS_FORCE_FFMPEG_PATH"] +else: + ffmpeg_paths = [] try: from imageio_ffmpeg import get_ffmpeg_exe - ffmpeg_path = get_ffmpeg_exe() + imageio_ffmpeg_path = get_ffmpeg_exe() + ffmpeg_paths.append(imageio_ffmpeg_path) except: + if "VHS_USE_IMAGEIO_FFMPEG" in os.environ: + raise logger.warn("Failed to import imageio_ffmpeg") -if ffmpeg_path is None: - logger.warning("ffmpeg could not be found. Outputs that require it have been disabled") + if "VHS_USE_IMAGEIO_FFMPEG" in os.environ: + ffmpeg_path = imageio_ffmpeg_path + else: + system_ffmpeg = shutil.which("ffmpeg") + if system_ffmpeg is not None: + ffmpeg_paths.append(system_ffmpeg) + if len(ffmpeg_paths) == 0: + logger.error("No valid ffmpeg found.") + ffmpeg_path = max(ffmpeg_paths, key=ffmpeg_suitability) def get_sorted_dir_files_from_directory(directory: str, skip_first_images: int=0, select_every_nth: int=1, extensions: Iterable=None): directory = directory.strip()