More code re-organizing

This commit is contained in:
Brett Williams
2023-10-21 22:45:30 -05:00
parent 7a52cce40a
commit 9027cd7202
26 changed files with 33 additions and 48 deletions

View File

@@ -0,0 +1,96 @@
import re
from src.engines.core.base_engine import *
class FFMPEG(BaseRenderEngine):
binary_names = {'linux': 'ffmpeg', 'windows': 'ffmpeg.exe', 'macos': 'ffmpeg'}
def version(self):
version = None
try:
ver_out = subprocess.check_output([self.renderer_path(), '-version'],
timeout=SUBPROCESS_TIMEOUT).decode('utf-8')
match = re.match(".*version\s*(\S+)\s*Copyright", ver_out)
if match:
version = match.groups()[0]
except Exception as e:
logger.error("Failed to get FFMPEG version: {}".format(e))
return version
def get_encoders(self):
raw_stdout = subprocess.check_output([self.renderer_path(), '-encoders'], stderr=subprocess.DEVNULL,
timeout=SUBPROCESS_TIMEOUT).decode('utf-8')
pattern = '(?P<type>[VASFXBD.]{6})\s+(?P<name>\S{2,})\s+(?P<description>.*)'
encoders = [m.groupdict() for m in re.finditer(pattern, raw_stdout)]
return encoders
def get_formats(self):
encoders = [x['name'] for x in self.get_all_formats() if 'E' in x['type']]
return encoders
def get_all_formats(self):
try:
formats_raw = subprocess.check_output([self.renderer_path(), '-formats'], stderr=subprocess.DEVNULL,
timeout=SUBPROCESS_TIMEOUT).decode('utf-8')
pattern = '(?P<type>[DE]{1,2})\s+(?P<id>\S{2,})\s+(?P<name>.*)\r'
all_formats = [m.groupdict() for m in re.finditer(pattern, formats_raw)]
return all_formats
except Exception as e:
logger.error(f"Error getting all formats: {e}")
return []
def extension_for_format(self, ffmpeg_format):
# Extract the common extension using regex
muxer_flag = 'muxer' if 'E' in ffmpeg_format['type'] else 'demuxer'
format_detail_raw = subprocess.check_output(
[self.renderer_path(), '-hide_banner', '-h', f"{muxer_flag}={ffmpeg_format['id']}"]).decode('utf-8')
pattern = r"Common extensions: (\w+)"
common_extensions = re.findall(pattern, format_detail_raw)
found_extensions = []
if common_extensions:
found_extensions = common_extensions[0].split(',')
return found_extensions
def get_output_formats(self):
return [x for x in self.get_all_formats() if 'E' in x['type'].upper()]
def get_frame_count(self, path_to_file):
raw_stdout = subprocess.check_output([self.renderer_path(), '-i', path_to_file, '-map', '0:v:0', '-c', 'copy',
'-f', 'null', '-'], stderr=subprocess.STDOUT,
timeout=SUBPROCESS_TIMEOUT).decode('utf-8')
match = re.findall(r'frame=\s*(\d+)', raw_stdout)
if match:
frame_number = int(match[-1])
return frame_number
def get_arguments(self):
help_text = subprocess.check_output([self.renderer_path(), '-h', 'long'], stderr=subprocess.STDOUT).decode('utf-8')
lines = help_text.splitlines()
options = {}
current_category = None
for line in lines:
line = line.strip()
if line.endswith(":") and "options" in line.lower():
current_category = line.split('options')[0].strip()
options[current_category] = {}
elif line: # Ignore blank lines
split_line = line.split(' ', 2)
flag = split_line[0]
argument = split_line[1] if len(split_line) > 1 else ''
description = split_line[2] if len(split_line) > 2 else ''
if current_category:
name = flag.strip('-')
options[current_category][name] = {
'flag': flag, 'description': description.strip(), 'takes_argument': bool(argument)
}
return options
if __name__ == "__main__":
print(FFMPEG().get_all_formats())