YoutubeTranscriptTool / ffmpeg_setup.py
maguid28's picture
Implemented transcription fallback using Whisper
4f48868
raw
history blame
1.61 kB
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.")