File size: 2,060 Bytes
13d3de7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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