2021-03-18 16:53:04 +00:00
|
|
|
import os
|
2021-09-15 14:23:15 +01:00
|
|
|
import sys
|
2021-03-18 16:53:04 +00:00
|
|
|
from sys import platform
|
|
|
|
|
from subprocess import check_output
|
|
|
|
|
import requests
|
|
|
|
|
from zipfile import ZipFile
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
FFMPEG_COMMAND = "ffmpeg -version"
|
2021-09-15 14:23:15 +01:00
|
|
|
FFMPEG_PATHS = [os.path.abspath(os.path.join(getattr(sys, "_MEIPASS", ""))), os.path.abspath("ffmpeg\\bin")]
|
2021-03-18 16:53:04 +00:00
|
|
|
FFMPEG_WINDOWS_URL = "https://www.gyan.dev/ffmpeg/builds/ffmpeg-release-essentials.zip"
|
|
|
|
|
FFMPEG_LINUX_INSTALL = "sudo apt-get install -y ffmpeg"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def is_ffmpeg_installed():
|
2021-04-20 12:23:11 +01:00
|
|
|
"""Checks if FFmpeg is installed
|
|
|
|
|
|
|
|
|
|
Returns
|
|
|
|
|
-------
|
|
|
|
|
bool
|
|
|
|
|
Whether or not ffmpeg is installed
|
|
|
|
|
"""
|
2021-03-18 16:53:04 +00:00
|
|
|
try:
|
|
|
|
|
check_output(FFMPEG_COMMAND.split(" "))
|
|
|
|
|
return True
|
|
|
|
|
except:
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def install_ffmpeg_windows():
|
2021-04-20 12:23:11 +01:00
|
|
|
"""Downloads and extracts the FFmpeg library"""
|
2022-02-08 17:18:42 +00:00
|
|
|
try:
|
|
|
|
|
r = requests.get(FFMPEG_WINDOWS_URL)
|
|
|
|
|
with open("ffmpeg.zip", "wb") as f:
|
|
|
|
|
f.write(r.content)
|
|
|
|
|
|
|
|
|
|
with ZipFile("ffmpeg.zip", "r") as zipf:
|
|
|
|
|
zipf.extractall()
|
|
|
|
|
|
|
|
|
|
ffmpeg_folder = [f for f in os.listdir() if f.startswith("ffmpeg-")][0]
|
|
|
|
|
os.rename(ffmpeg_folder, "ffmpeg")
|
|
|
|
|
os.remove("ffmpeg.zip")
|
|
|
|
|
os.environ["PATH"] += os.pathsep + os.path.abspath("ffmpeg\\bin")
|
|
|
|
|
except requests.exceptions.ConnectionError:
|
|
|
|
|
raise Exception("Unable to download FFmpeg. Please install manually")
|
2021-03-18 16:53:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def install_ffmpeg_linux():
|
2021-04-20 12:23:11 +01:00
|
|
|
"""Runs the linux FFmpeg install command"""
|
2021-03-18 16:53:04 +00:00
|
|
|
check_output(FFMPEG_LINUX_INSTALL.split(" "))
|
|
|
|
|
|
|
|
|
|
|
2021-09-15 14:23:15 +01:00
|
|
|
def try_ffmpeg_paths():
|
|
|
|
|
"""
|
|
|
|
|
Try ffmpeg paths to find existing install
|
|
|
|
|
|
|
|
|
|
Returns
|
|
|
|
|
-------
|
|
|
|
|
str
|
|
|
|
|
Path to existing install (or None if not found)
|
|
|
|
|
"""
|
|
|
|
|
for path in FFMPEG_PATHS:
|
|
|
|
|
if os.path.isdir(path):
|
|
|
|
|
os.environ["PATH"] += os.pathsep + path
|
|
|
|
|
if is_ffmpeg_installed():
|
|
|
|
|
return path
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
2021-03-18 16:53:04 +00:00
|
|
|
def check_ffmpeg():
|
2021-04-20 12:23:11 +01:00
|
|
|
"""Checks if FFmpeg is installed, and if not will install
|
|
|
|
|
|
|
|
|
|
Raises
|
|
|
|
|
-------
|
|
|
|
|
AssertionError
|
|
|
|
|
If ffmpeg could not be installed
|
|
|
|
|
"""
|
2021-03-18 16:53:04 +00:00
|
|
|
if not is_ffmpeg_installed():
|
|
|
|
|
if platform == "win32":
|
2021-09-15 14:23:15 +01:00
|
|
|
existing_install = try_ffmpeg_paths()
|
|
|
|
|
if existing_install:
|
|
|
|
|
print("USING FFMPEG INSTALL", existing_install)
|
|
|
|
|
else:
|
2021-03-18 16:53:04 +00:00
|
|
|
print("INSTALLING FFMPEG")
|
|
|
|
|
install_ffmpeg_windows()
|
|
|
|
|
print("VERIFYING FFMPEG INSTALL")
|
|
|
|
|
assert is_ffmpeg_installed(), "FFMPEG did not install correctly"
|
|
|
|
|
else:
|
|
|
|
|
print("INSTALLING FFMPEG")
|
|
|
|
|
install_ffmpeg_linux()
|
|
|
|
|
print("VERIFYING FFMPEG INSTALL")
|
|
|
|
|
assert is_ffmpeg_installed(), "FFMPEG did not install correctly"
|