2020-06-27 19:21:00 +00:00
|
|
|
#!/bin/env python
|
2020-06-22 16:32:40 +00:00
|
|
|
|
|
|
|
import os
|
|
|
|
import shutil
|
2020-07-06 15:53:25 +00:00
|
|
|
from distutils.spawn import find_executable
|
2020-06-22 16:32:40 +00:00
|
|
|
from pathlib import Path
|
2020-07-06 15:53:25 +00:00
|
|
|
|
2020-06-27 19:21:00 +00:00
|
|
|
from psutil import virtual_memory
|
|
|
|
|
2020-06-22 16:32:40 +00:00
|
|
|
from .utils import terminate
|
|
|
|
|
|
|
|
|
|
|
|
def determine_resources(encoder, workers):
|
|
|
|
"""Returns number of workers that machine can handle with selected encoder."""
|
|
|
|
|
|
|
|
# If set by user, skip
|
|
|
|
if workers != 0:
|
|
|
|
return workers
|
|
|
|
|
|
|
|
cpu = os.cpu_count()
|
|
|
|
ram = round(virtual_memory().total / 2 ** 30)
|
|
|
|
|
|
|
|
if encoder in ('aom', 'rav1e', 'vpx'):
|
2020-06-30 23:58:27 +00:00
|
|
|
workers = round(min(cpu / 2, ram / 1.5))
|
2020-06-22 16:32:40 +00:00
|
|
|
|
2020-07-14 07:25:10 +00:00
|
|
|
elif encoder in ('svt_av1', 'x265'):
|
2020-06-30 23:58:27 +00:00
|
|
|
workers = round(min(cpu, ram)) // 5
|
2020-06-22 16:32:40 +00:00
|
|
|
|
|
|
|
# fix if workers round up to 0
|
|
|
|
if workers == 0:
|
2020-06-30 23:58:27 +00:00
|
|
|
workers = 1
|
2020-06-22 16:32:40 +00:00
|
|
|
|
2020-06-30 23:58:27 +00:00
|
|
|
return workers
|
2020-06-22 16:32:40 +00:00
|
|
|
|
2020-07-05 16:45:10 +00:00
|
|
|
|
2020-06-22 16:32:40 +00:00
|
|
|
def check_executables(encoder):
|
2020-07-14 07:25:10 +00:00
|
|
|
encoders = {'svt_av1': 'SvtAv1EncApp', 'rav1e': 'rav1e', 'aom': 'aomenc', 'vpx': 'vpxenc','x265': 'x265'}
|
2020-06-22 16:32:40 +00:00
|
|
|
if not find_executable('ffmpeg'):
|
|
|
|
print('No ffmpeg')
|
|
|
|
terminate()
|
|
|
|
|
|
|
|
# Check if encoder executable is reachable
|
|
|
|
if encoder in encoders:
|
|
|
|
enc = encoders.get(encoder)
|
|
|
|
|
|
|
|
if not find_executable(enc):
|
|
|
|
print(f'Encoder {enc} not found')
|
|
|
|
terminate()
|
|
|
|
else:
|
2020-07-14 07:25:10 +00:00
|
|
|
print(f'Not valid encoder {encoder}\nValid encoders: "aom rav1e", "svt_av1", "vpx", "x265" ')
|
2020-06-22 16:32:40 +00:00
|
|
|
terminate()
|
|
|
|
|
|
|
|
|
|
|
|
def setup(temp: Path, resume):
|
|
|
|
"""Creating temporally folders when needed."""
|
|
|
|
# Make temporal directories, and remove them if already presented
|
|
|
|
if not resume:
|
|
|
|
if temp.is_dir():
|
|
|
|
shutil.rmtree(temp)
|
|
|
|
|
|
|
|
(temp / 'split').mkdir(parents=True, exist_ok=True)
|
|
|
|
(temp / 'encode').mkdir(exist_ok=True)
|
|
|
|
|
2020-06-22 20:11:07 +00:00
|
|
|
|
2020-07-14 07:25:10 +00:00
|
|
|
def outputs_filenames(inp: Path, out:Path, encoder):
|
|
|
|
if encoder == 'x265':
|
|
|
|
suffix = '.mp4'
|
|
|
|
else:
|
|
|
|
suffix = '.mkv'
|
|
|
|
|
2020-06-22 16:32:40 +00:00
|
|
|
if out:
|
2020-07-14 07:25:10 +00:00
|
|
|
return out.with_suffix(suffix)
|
2020-06-22 16:32:40 +00:00
|
|
|
else:
|
2020-07-14 07:25:10 +00:00
|
|
|
return Path(f'{inp.stem}_av1{suffix}')
|