File size: 1,611 Bytes
4f48868
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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():
    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 now: {os.environ['PATH']}")

    if is_ffmpeg_in_path():
        logger.info("FFmpeg is accessible on PATH.")
    else:
        logger.warning("Tried appending the directory to PATH, but 'ffmpeg' is still not found.")