Av1an/utils/bar.py

104 lines
3.3 KiB
Python
Raw Normal View History

2020-06-27 19:21:00 +00:00
#!/bin/env python
import re
2020-06-16 23:53:49 +00:00
import subprocess
2020-07-06 15:53:25 +00:00
import sys
2020-06-23 06:12:58 +00:00
from collections import deque
2020-06-20 19:18:03 +00:00
from multiprocessing.managers import BaseManager
2020-07-06 15:53:25 +00:00
from subprocess import PIPE, STDOUT
2020-06-20 19:18:03 +00:00
from tqdm import tqdm
2020-06-27 19:21:00 +00:00
2020-06-21 19:02:36 +00:00
from utils.utils import terminate
2020-06-22 20:11:07 +00:00
2020-07-06 15:53:25 +00:00
2020-06-20 19:18:03 +00:00
def Manager():
m = BaseManager()
2020-06-20 19:18:03 +00:00
m.start()
return m
class Counter():
def __init__(self, total, initial):
self.first_update = True
self.initial = initial
self.left = total - initial
self.tqdm_bar = tqdm(total=self.left, initial=0, dynamic_ncols=True, unit="fr", leave=True, smoothing=0.2)
def update(self, value):
if self.first_update:
self.tqdm_bar.reset(self.left)
self.first_update = False
self.tqdm_bar.update(value)
BaseManager.register('Counter', Counter)
2020-06-16 23:53:49 +00:00
2020-06-17 08:35:45 +00:00
def tqdm_bar(i, encoder, counter, frame_probe_source, passes):
2020-06-22 18:17:27 +00:00
try:
2020-06-23 01:16:24 +00:00
2020-06-23 06:12:58 +00:00
encoder_history = deque(maxlen=20)
2020-06-23 01:16:24 +00:00
2020-06-22 18:17:27 +00:00
f, e = i.split('|')
f = " ffmpeg -y -hide_banner -loglevel error " + f
f, e = f.split(), e.split()
frame = 0
ffmpeg_pipe = subprocess.Popen(f, stdout=PIPE, stderr=STDOUT)
pipe = subprocess.Popen(e, stdin=ffmpeg_pipe.stdout, stdout=PIPE,
stderr=STDOUT,
universal_newlines=True)
2020-07-14 07:25:10 +00:00
pass_1_check = True
skip_1_pass = False
2020-06-22 18:17:27 +00:00
while True:
2020-06-23 06:01:12 +00:00
line = pipe.stdout.readline().strip()
if line:
2020-06-23 06:12:58 +00:00
encoder_history.append(line)
2020-06-22 18:17:27 +00:00
if len(line) == 0 and pipe.poll() is not None:
break
2020-06-21 19:02:36 +00:00
2020-06-22 18:17:27 +00:00
if len(line) == 0:
continue
2020-07-14 07:25:10 +00:00
if encoder in ('aom', 'vpx', 'rav1e','x265'):
2020-06-22 18:17:27 +00:00
match = None
2020-07-14 07:25:10 +00:00
2020-06-22 18:17:27 +00:00
if encoder in ('aom', 'vpx'):
if 'fatal' in line.lower():
print('\n\nERROR IN ENCODING PROCESS\n\n', line)
terminate()
if 'Pass 2/2' in line or 'Pass 1/1' in line:
match = re.search(r"frame.*?\/([^ ]+?) ", line)
elif encoder == 'rav1e':
if 'error' in line.lower():
print('\n\nERROR IN ENCODING PROCESS\n\n', line)
terminate()
match = re.search(r"encoded.*? ([^ ]+?) ", line)
2020-06-16 23:53:49 +00:00
2020-07-14 07:25:10 +00:00
elif encoder in ('x265'):
if not skip_1_pass and pass_1_check:
if 'output file' in line:
if 'nul' in line.lower():
skip_1_pass = True
else:
pass_1_check = False
if not skip_1_pass:
match = re.search(r"^(\d+)", line)
2020-06-22 18:17:27 +00:00
if match:
new = int(match.group(1))
if new > frame:
counter.update(new - frame)
frame = new
2020-06-20 11:12:03 +00:00
2020-07-14 07:25:10 +00:00
2020-06-24 17:30:33 +00:00
if encoder == 'svt_av1':
2020-06-22 18:17:27 +00:00
counter.update(frame_probe_source // passes)
2020-06-23 01:16:24 +00:00
if pipe.returncode != 0 and pipe.returncode != -2: # -2 is Ctrl+C for aom
2020-06-23 06:12:58 +00:00
print(f"\nEncoder encountered an error: {pipe.returncode}")
print('\n'.join(encoder_history))
2020-06-23 01:16:24 +00:00
2020-06-22 18:17:27 +00:00
except Exception as e:
_, _, exc_tb = sys.exc_info()
2020-06-22 20:11:07 +00:00
print(f'Error at encode {e}\nAt line {exc_tb.tb_lineno}')