Av1an/encoder/vpx.py

69 lines
2.3 KiB
Python
Raw Normal View History

import os
2020-08-21 13:31:09 +00:00
import re
2020-12-25 15:32:45 +00:00
from project import Project
2020-12-25 17:01:51 +00:00
from chunk import Chunk
2020-12-25 15:32:45 +00:00
from av1an.commandtypes import MPCommands, CommandPair, Command
from encoder.encoder import Encoder
from av1an.utils import list_index_of_regex, terminate
class Vpx(Encoder):
def __init__(self):
super().__init__(
encoder_bin='vpxenc',
2020-09-15 17:36:23 +00:00
encoder_help='vpxenc --help',
default_args=['--codec=vp9', '-b', '10', '--profile=2', '--threads=4', '--cpu-used=0', '--end-usage=q',
2020-12-08 23:49:36 +00:00
'--cq-level=30', '--row-mt=1'],
default_passes=2,
default_q_range=(20, 55),
output_extension='ivf'
)
def compose_1_pass(self, a: Project, c: Chunk, output: str) -> MPCommands:
return [
2020-08-19 07:06:42 +00:00
CommandPair(
Encoder.compose_ffmpeg_pipe(a),
['vpxenc', '--passes=1', *a.video_params, '-o', output, '-']
2020-08-19 07:06:42 +00:00
)
]
def compose_2_pass(self, a: Project, c: Chunk, output: str) -> MPCommands:
return [
2020-08-19 07:06:42 +00:00
CommandPair(
Encoder.compose_ffmpeg_pipe(a),
['vpxenc', '--passes=2', '--pass=1', *a.video_params, f'--fpf={c.fpf}', '-o', os.devnull, '-']
),
CommandPair(
Encoder.compose_ffmpeg_pipe(a),
['vpxenc', '--passes=2', '--pass=2', *a.video_params, f'--fpf={c.fpf}', '-o', output, '-']
2020-08-19 07:06:42 +00:00
)
]
def man_q(self, command: Command, q: int) -> Command:
"""Return command with new cq value
:param command: old command
:param q: new cq value
:return: command with new cq value"""
adjusted_command = command.copy()
i = list_index_of_regex(adjusted_command, r"--cq-level=.+")
adjusted_command[i] = f'--cq-level={q}'
return adjusted_command
2020-08-21 13:31:09 +00:00
def match_line(self, line: str):
2020-08-21 13:31:09 +00:00
"""Extract number of encoded frames from line.
:param line: one line of text output from the encoder
:return: match object from re.search matching the number of encoded frames"""
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:
return re.search(r"frame.*?/([^ ]+?) ", line)