-
Notifications
You must be signed in to change notification settings - Fork 1
/
formats.py
177 lines (141 loc) · 5.76 KB
/
formats.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
from collections import namedtuple
from functools import partial
from PyQt5.QtWidgets import QDialog, QApplication, QFormLayout, QLabel, QLineEdit, QPushButton, QHBoxLayout
from more_itertools import consume
from more_itertools.recipes import grouper
from dialog import Dialog
from utils import color_text, get_stylesheet
encoding = namedtuple('Encoding', ['ext', 'f', 'commands'])
passes = namedtuple('commandset', ['all', 'first', 'second'])
format_spec = {}
webm_params = passes([
'-c:v', 'libvpx-vp9',
'-tile-columns', '1',
'-tile-rows', '0',
'-threads', '12',
'-row-mt', '1',
'-aq-mode', '2',
'-arnr-maxframes', '15',
'-arnr-strength', '3',
'-static-thresh', '0',
'-frame-parallel', '0',
'-tune-content', 'screen',
'-auto-alt-ref', '6',
'-lag-in-frames', '25',
'-g', '8000',
'-pix_fmt', 'yuv420p10le'
], [
'-cpu-used', '1'
], [
'-cpu-used', '1'
])
format_spec['VP9'] = encoding('webm', 'webm', webm_params)
av1_params = passes([
'-c:v', 'libaom-av1',
'-tiles', '0x0',
'-threads', '12',
'-pix_fmt', 'yuv420p10le',
'-row-mt', '1',
'-auto-alt-ref', '1',
'-lag-in-frames', '25',
'-strict', 'experimental',
'-static-thresh', '0',
'-frame-parallel', '0',
'-g', '99999',
'-aq-mode', '2',
'-tune-content', 'screen',
'-arnr-maxframes', '15',
'-arnr-strength', '6',
], [
'-cpu-used', '8',
], [
'-cpu-used', '6',
])
# Orignially had mkv as filetype
format_spec['AV1'] = encoding('webm', 'webm', av1_params)
class Tweaker(QDialog):
def __init__(self, spec: str):
super(Tweaker, self).__init__()
self.enc = format_spec[spec]
self.setWindowTitle(spec)
self.ok_button = QPushButton('Ok', self)
# self.ok_button.setFixedSize(self.ok_button.sizeHint())
self.ok_button.clicked.connect(self.accept)
self.cancel_button = QPushButton('Cancel', self)
# self.cancel_button.setFixedSize(self.cancel_button.sizeHint())
self.cancel_button.clicked.connect(self.reject)
self.setStyleSheet(get_stylesheet())
self.all_pass = self.enc.commands.all
self.first_pass = self.enc.commands.first
self.second_pass = self.enc.commands.second
self.all_pass_pairs = {k: v for k, v in grouper(self.all_pass, 2)}
self.first_pass_pairs = {k: v for k, v in grouper(self.first_pass, 2)}
self.second_pass_pairs = {k: v for k, v in grouper(self.second_pass, 2)}
self.create_form()
def extend(self, items: dict):
self._extra_dict = items.copy()
form = QFormLayout()
form.addRow(QLabel(color_text('Extra Options', color='limegreen')), None)
for key, value in items.items():
key_field = QLabel(key)
value_field = QLineEdit(str(value))
value_field.textChanged.connect(partial(self.update_pair, self._extra_dict, key))
form.addRow(key_field, value_field)
self.hbox.addLayout(form)
def get_extended_results(self):
print(self._extra_dict)
return self._extra_dict
def add_row(self, pass_dict: dict):
idx = self.form.indexOf(self.sender())
dia = Dialog(self, 'Pick a parameter!', 'Select the name of the option to add!')
if dia.exec() != QDialog.Accepted:
return
key = dia.answer()
if not key.startswith('-'):
key = '-' + key
if key in pass_dict:
return
key_field = QLabel(key)
key_field.mouseDoubleClickEvent = lambda _, item=key_field: self.remove_item(_, item)
value = QLineEdit('')
value.textChanged.connect(partial(self.update_pair, pass_dict, key))
self.form.insertRow(idx // 2 + 1, key_field, value)
# TODO: Duplicate detection
def update_pair(self, pair_map: dict, key: str, value: str):
if not key.startswith('-'):
key = '-' + key
pair_map.update({key: value})
def remove_item(self, _, item, mapping):
del mapping[item.text()]
self.form.removeRow(item)
self.adjustSize()
def create_form(self):
self.hbox = QHBoxLayout()
self.form = form = QFormLayout()
for pass_name, pass_dict in zip(('All passes', 'First Pass', 'Second Pass'),
(self.all_pass_pairs, self.first_pass_pairs, self.second_pass_pairs)):
add_btn = QPushButton('Add')
add_btn.clicked.connect(partial(self.add_row, pass_dict))
form.addRow(QLabel(color_text(pass_name, color='limegreen')), add_btn)
for key, value in pass_dict.items():
key_field = QLabel(key)
key_field.mouseDoubleClickEvent = lambda _, item=key_field, mapping=pass_dict: self.remove_item(_, item,
mapping)
value_field = QLineEdit(value)
value_field.textChanged.connect(partial(self.update_pair, pass_dict, key))
form.addRow(key_field, value_field)
form.addRow(self.ok_button, self.cancel_button)
self.hbox.addLayout(form)
self.setLayout(self.hbox)
def get_encoding(self):
for pass_list, pass_pair in zip([self.all_pass, self.first_pass, self.second_pass],
[self.all_pass_pairs, self.first_pass_pairs, self.second_pass_pairs]):
pass_list.clear()
consume(pass_list.extend([k, v]) for k, v in pass_pair.items())
p = passes(self.all_pass, self.first_pass, self.second_pass)
return encoding(self.enc.ext, self.enc.f, p)
if __name__ == '__main__':
app = QApplication([])
t = Tweaker('VP9')
if t.exec() == QDialog.Accepted:
print(t.get_encoding())