mediainfo-picker 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mediainfo_picker/__init__.py +5 -0
- mediainfo_picker/__main__.py +5 -0
- mediainfo_picker/app.py +463 -0
- mediainfo_picker/config.py +48 -0
- mediainfo_picker/data/icon.svg +5 -0
- mediainfo_picker/data/mediainfo-csv/Audio.csv +285 -0
- mediainfo_picker/data/mediainfo-csv/General.csv +359 -0
- mediainfo_picker/data/mediainfo-csv/Image.csv +177 -0
- mediainfo_picker/data/mediainfo-csv/LICENSE-MediaInfoLib +25 -0
- mediainfo_picker/data/mediainfo-csv/Menu.csv +101 -0
- mediainfo_picker/data/mediainfo-csv/Other.csv +193 -0
- mediainfo_picker/data/mediainfo-csv/TAG +1 -0
- mediainfo_picker/data/mediainfo-csv/Text.csv +305 -0
- mediainfo_picker/data/mediainfo-csv/Video.csv +398 -0
- mediainfo_picker/descriptions.py +47 -0
- mediainfo_picker/engine.py +246 -0
- mediainfo_picker-0.2.0.dist-info/METADATA +125 -0
- mediainfo_picker-0.2.0.dist-info/RECORD +22 -0
- mediainfo_picker-0.2.0.dist-info/WHEEL +4 -0
- mediainfo_picker-0.2.0.dist-info/entry_points.txt +2 -0
- mediainfo_picker-0.2.0.dist-info/licenses/LICENSE +21 -0
- mediainfo_picker-0.2.0.dist-info/licenses/src/mediainfo_picker/data/mediainfo-csv/LICENSE-MediaInfoLib +25 -0
mediainfo_picker/app.py
ADDED
|
@@ -0,0 +1,463 @@
|
|
|
1
|
+
"""The window: pick a file, choose Default / Full / Selected fields, apply, copy."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import sys
|
|
6
|
+
from collections.abc import Iterator
|
|
7
|
+
from datetime import datetime
|
|
8
|
+
from importlib.resources import files
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
|
|
11
|
+
from PySide6.QtCore import QByteArray, Qt
|
|
12
|
+
from PySide6.QtGui import QCloseEvent, QFont, QFontDatabase, QGuiApplication, QIcon
|
|
13
|
+
from PySide6.QtWidgets import (
|
|
14
|
+
QAbstractButton,
|
|
15
|
+
QApplication,
|
|
16
|
+
QButtonGroup,
|
|
17
|
+
QFileDialog,
|
|
18
|
+
QHBoxLayout,
|
|
19
|
+
QLabel,
|
|
20
|
+
QLineEdit,
|
|
21
|
+
QMainWindow,
|
|
22
|
+
QMessageBox,
|
|
23
|
+
QPlainTextEdit,
|
|
24
|
+
QPushButton,
|
|
25
|
+
QRadioButton,
|
|
26
|
+
QSplitter,
|
|
27
|
+
QTreeWidget,
|
|
28
|
+
QTreeWidgetItem,
|
|
29
|
+
QVBoxLayout,
|
|
30
|
+
QWidget,
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
from . import __version__, config
|
|
34
|
+
from .descriptions import data_tag, describe
|
|
35
|
+
from .engine import Field, MediaInfo, MediaInfoError, Mode, plan
|
|
36
|
+
|
|
37
|
+
APP_NAME = "MediaInfo Picker"
|
|
38
|
+
ACCENT = "#17707F" # the banner and the icon (data/icon.svg) share this colour
|
|
39
|
+
PARAM = Qt.ItemDataRole.UserRole # an item's "Kind_Name"
|
|
40
|
+
DEFAULT = Qt.ItemDataRole.UserRole + 1 # whether mediainfo shows it by default
|
|
41
|
+
CHECKABLE = Qt.ItemFlag.ItemIsUserCheckable
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def _check(on: bool) -> Qt.CheckState:
|
|
45
|
+
return Qt.CheckState.Checked if on else Qt.CheckState.Unchecked
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def app_icon() -> QIcon:
|
|
49
|
+
return QIcon(str(files("mediainfo_picker") / "data" / "icon.svg"))
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def banner() -> QWidget:
|
|
53
|
+
"""The strip across the top: the icon and the app's name on the accent colour."""
|
|
54
|
+
strip = QWidget()
|
|
55
|
+
strip.setObjectName("banner")
|
|
56
|
+
strip.setAttribute(Qt.WidgetAttribute.WA_StyledBackground, True)
|
|
57
|
+
strip.setStyleSheet(f"#banner {{ background: {ACCENT}; }} #banner QLabel {{ color: white; }}")
|
|
58
|
+
icon = QLabel()
|
|
59
|
+
icon.setPixmap(app_icon().pixmap(22, 22))
|
|
60
|
+
title = QLabel(APP_NAME)
|
|
61
|
+
font = QFont(title.font())
|
|
62
|
+
font.setPointSizeF(font.pointSizeF() * 1.3)
|
|
63
|
+
font.setBold(True)
|
|
64
|
+
title.setFont(font)
|
|
65
|
+
row = QHBoxLayout(strip)
|
|
66
|
+
row.setContentsMargins(10, 6, 10, 6)
|
|
67
|
+
row.addWidget(icon)
|
|
68
|
+
row.addWidget(title)
|
|
69
|
+
row.addStretch(1)
|
|
70
|
+
return strip
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
class Window(QMainWindow):
|
|
74
|
+
def __init__(self, mi: MediaInfo | None, error: str = "") -> None:
|
|
75
|
+
super().__init__()
|
|
76
|
+
self.mi = mi
|
|
77
|
+
self.settings = config.load()
|
|
78
|
+
self.fields: list[Field] = []
|
|
79
|
+
self.known: set[str] = mi.known_params() if mi else set()
|
|
80
|
+
self.path = ""
|
|
81
|
+
self.selected: set[str] = set(self.settings.selected)
|
|
82
|
+
self._updating = False
|
|
83
|
+
self.setWindowTitle(APP_NAME)
|
|
84
|
+
self.setWindowIcon(app_icon())
|
|
85
|
+
mono = QFontDatabase.systemFont(QFontDatabase.SystemFont.FixedFont)
|
|
86
|
+
|
|
87
|
+
# File row
|
|
88
|
+
self.file_edit = QLineEdit(readOnly=True, placeholderText="Choose a media file…")
|
|
89
|
+
browse = QPushButton("Browse…")
|
|
90
|
+
browse.clicked.connect(self.browse)
|
|
91
|
+
top = QHBoxLayout()
|
|
92
|
+
top.addWidget(QLabel("File:"))
|
|
93
|
+
top.addWidget(self.file_edit, 1)
|
|
94
|
+
top.addWidget(browse)
|
|
95
|
+
|
|
96
|
+
# Left: mode, search, palette
|
|
97
|
+
self.mode_group = QButtonGroup(self)
|
|
98
|
+
self.radios: dict[Mode, QRadioButton] = {}
|
|
99
|
+
modes = QHBoxLayout()
|
|
100
|
+
for mode, text in ((Mode.DEFAULT, "Default"), (Mode.FULL, "Full"), (Mode.SELECTED, "Selected fields")):
|
|
101
|
+
rb = QRadioButton(text)
|
|
102
|
+
self.mode_group.addButton(rb)
|
|
103
|
+
self.radios[mode] = rb
|
|
104
|
+
modes.addWidget(rb)
|
|
105
|
+
modes.addStretch(1)
|
|
106
|
+
self.search = QLineEdit(placeholderText="Search fields…", clearButtonEnabled=True)
|
|
107
|
+
self.search.textChanged.connect(self.apply_filter)
|
|
108
|
+
self._expanded_before_search: set[str] | None = None
|
|
109
|
+
self.tree = QTreeWidget()
|
|
110
|
+
self.tree.setHeaderHidden(True)
|
|
111
|
+
self.tree.itemChanged.connect(self.item_changed)
|
|
112
|
+
expand = QPushButton("Expand all")
|
|
113
|
+
expand.clicked.connect(lambda: self.set_expanded(True))
|
|
114
|
+
collapse = QPushButton("Collapse all")
|
|
115
|
+
collapse.clicked.connect(lambda: self.set_expanded(False))
|
|
116
|
+
self.defaults_btn = QPushButton("Defaults")
|
|
117
|
+
self.defaults_btn.setToolTip("Tick exactly the fields mediainfo shows by default")
|
|
118
|
+
self.defaults_btn.clicked.connect(self.select_defaults)
|
|
119
|
+
self.clear_btn = QPushButton("Clear all")
|
|
120
|
+
self.clear_btn.setToolTip("Untick every field")
|
|
121
|
+
self.clear_btn.clicked.connect(self.clear_selection)
|
|
122
|
+
tools = QHBoxLayout()
|
|
123
|
+
for b in (expand, collapse):
|
|
124
|
+
tools.addWidget(b)
|
|
125
|
+
tools.addStretch(1)
|
|
126
|
+
for b in (self.defaults_btn, self.clear_btn):
|
|
127
|
+
tools.addWidget(b)
|
|
128
|
+
self.count = QLabel()
|
|
129
|
+
left = QWidget()
|
|
130
|
+
lv = QVBoxLayout(left)
|
|
131
|
+
lv.setContentsMargins(0, 0, 0, 0)
|
|
132
|
+
lv.addLayout(modes)
|
|
133
|
+
lv.addWidget(self.search)
|
|
134
|
+
lv.addLayout(tools)
|
|
135
|
+
lv.addWidget(self.tree, 1)
|
|
136
|
+
lv.addWidget(self.count)
|
|
137
|
+
|
|
138
|
+
# Right: output
|
|
139
|
+
self.output = QPlainTextEdit(readOnly=True, lineWrapMode=QPlainTextEdit.LineWrapMode.NoWrap)
|
|
140
|
+
self.output.setFont(mono)
|
|
141
|
+
self.splitter = QSplitter()
|
|
142
|
+
self.splitter.addWidget(left)
|
|
143
|
+
self.splitter.addWidget(self.output)
|
|
144
|
+
self.splitter.setChildrenCollapsible(False) # the left panel never shrinks below its buttons
|
|
145
|
+
self.splitter.setStretchFactor(1, 2)
|
|
146
|
+
|
|
147
|
+
# Bottom: command and buttons
|
|
148
|
+
self.command = QLineEdit(readOnly=True, placeholderText="The equivalent mediainfo command appears here")
|
|
149
|
+
self.command.setFont(mono)
|
|
150
|
+
self.command.setToolTip(
|
|
151
|
+
"The mediainfo command that prints the output above, for a Unix-style shell "
|
|
152
|
+
"(bash, zsh; on Windows, Git Bash or WSL)"
|
|
153
|
+
)
|
|
154
|
+
self.apply_btn = QPushButton("Apply", default=True)
|
|
155
|
+
self.apply_btn.clicked.connect(self.apply)
|
|
156
|
+
copy_out = QPushButton("Copy output")
|
|
157
|
+
copy_out.clicked.connect(lambda: self.copy(self.output.toPlainText(), "Output"))
|
|
158
|
+
copy_cmd = QPushButton("Copy command")
|
|
159
|
+
copy_cmd.setToolTip(self.command.toolTip())
|
|
160
|
+
copy_cmd.clicked.connect(lambda: self.copy(self.command.text(), "Command"))
|
|
161
|
+
brow = QHBoxLayout()
|
|
162
|
+
brow.addStretch(1)
|
|
163
|
+
for b in (self.apply_btn, copy_out, copy_cmd):
|
|
164
|
+
brow.addWidget(b)
|
|
165
|
+
|
|
166
|
+
# Log: what happened and anything worth knowing about the last run. The status bar is kept
|
|
167
|
+
# for the versions and for errors.
|
|
168
|
+
self.log_view = QPlainTextEdit(readOnly=True, maximumBlockCount=500)
|
|
169
|
+
self.log_view.setPlaceholderText("Messages appear here")
|
|
170
|
+
lines = self.log_view.fontMetrics().lineSpacing()
|
|
171
|
+
self.log_view.setFixedHeight(lines * 5 + 12)
|
|
172
|
+
|
|
173
|
+
central = QWidget()
|
|
174
|
+
v = QVBoxLayout(central)
|
|
175
|
+
v.addLayout(top)
|
|
176
|
+
v.addWidget(self.splitter, 1)
|
|
177
|
+
v.addWidget(self.command)
|
|
178
|
+
v.addLayout(brow)
|
|
179
|
+
v.addWidget(self.log_view)
|
|
180
|
+
outer = QWidget()
|
|
181
|
+
ov = QVBoxLayout(outer)
|
|
182
|
+
ov.setContentsMargins(0, 0, 0, 0)
|
|
183
|
+
ov.setSpacing(0)
|
|
184
|
+
ov.addWidget(banner())
|
|
185
|
+
ov.addWidget(central, 1)
|
|
186
|
+
self.setCentralWidget(outer)
|
|
187
|
+
|
|
188
|
+
# Status bar: the versions in use, always, and errors
|
|
189
|
+
self.version_label = QLabel()
|
|
190
|
+
self.statusBar().addPermanentWidget(self.version_label)
|
|
191
|
+
if mi:
|
|
192
|
+
text = f"MediaInfo {mi.version()} · Descriptions {data_tag()} · {APP_NAME} v{__version__}"
|
|
193
|
+
self.version_label.setText(text)
|
|
194
|
+
else:
|
|
195
|
+
self.version_label.setText(f"MediaInfo not found · {APP_NAME} v{__version__}")
|
|
196
|
+
self.statusBar().showMessage(error)
|
|
197
|
+
self.apply_btn.setEnabled(False)
|
|
198
|
+
browse.setEnabled(False)
|
|
199
|
+
|
|
200
|
+
self.radios[Mode(self.settings.mode)].setChecked(True)
|
|
201
|
+
self.mode_group.buttonToggled.connect(self.mode_changed)
|
|
202
|
+
if self.settings.geometry:
|
|
203
|
+
self.restoreGeometry(QByteArray.fromHex(self.settings.geometry.encode()))
|
|
204
|
+
else:
|
|
205
|
+
self.resize(1200, 760)
|
|
206
|
+
if self.settings.splitter:
|
|
207
|
+
self.splitter.restoreState(QByteArray.fromHex(self.settings.splitter.encode()))
|
|
208
|
+
else:
|
|
209
|
+
self.splitter.setSizes([480, max(self.width() - 480, 400)])
|
|
210
|
+
self.update_palette_state()
|
|
211
|
+
|
|
212
|
+
# --- the tree -------------------------------------------------------------------------------
|
|
213
|
+
|
|
214
|
+
def sections(self) -> Iterator[QTreeWidgetItem]:
|
|
215
|
+
for i in range(self.tree.topLevelItemCount()):
|
|
216
|
+
item = self.tree.topLevelItem(i)
|
|
217
|
+
if item is None:
|
|
218
|
+
raise RuntimeError(f"tree has no top-level item {i}")
|
|
219
|
+
yield item
|
|
220
|
+
|
|
221
|
+
def items(self) -> Iterator[QTreeWidgetItem]:
|
|
222
|
+
for top in self.sections():
|
|
223
|
+
for j in range(top.childCount()):
|
|
224
|
+
child = top.child(j)
|
|
225
|
+
if child is None:
|
|
226
|
+
raise RuntimeError(f"section {top.text(0)!r} has no child {j}")
|
|
227
|
+
yield child
|
|
228
|
+
|
|
229
|
+
# --- mode and palette -----------------------------------------------------------------------
|
|
230
|
+
|
|
231
|
+
@property
|
|
232
|
+
def mode(self) -> Mode:
|
|
233
|
+
return next(m for m, rb in self.radios.items() if rb.isChecked())
|
|
234
|
+
|
|
235
|
+
def mode_changed(self, _button: QAbstractButton, checked: bool) -> None:
|
|
236
|
+
if not checked:
|
|
237
|
+
return
|
|
238
|
+
if self.mode is Mode.SELECTED and not self.settings.selected_initialised and self.fields:
|
|
239
|
+
self.selected = {f.param for f in self.fields if f.default}
|
|
240
|
+
self.settings.selected_initialised = True
|
|
241
|
+
self.refresh_ticks()
|
|
242
|
+
self.update_palette_state()
|
|
243
|
+
|
|
244
|
+
def update_palette_state(self) -> None:
|
|
245
|
+
"""Default: ticks shown, read-only. Full: greyed out. Selected fields: editable."""
|
|
246
|
+
self.tree.setEnabled(self.mode is not Mode.FULL)
|
|
247
|
+
editable = self.mode is Mode.SELECTED
|
|
248
|
+
self.defaults_btn.setEnabled(editable)
|
|
249
|
+
self.clear_btn.setEnabled(editable)
|
|
250
|
+
self._updating = True
|
|
251
|
+
for item in [*self.items(), *self.sections()]:
|
|
252
|
+
flags = item.flags()
|
|
253
|
+
item.setFlags(flags | CHECKABLE if editable else flags & ~CHECKABLE)
|
|
254
|
+
self._updating = False
|
|
255
|
+
self.update_count()
|
|
256
|
+
|
|
257
|
+
def ticked(self, param: str, default: bool) -> bool:
|
|
258
|
+
if self.mode is Mode.SELECTED:
|
|
259
|
+
return param in self.selected
|
|
260
|
+
return self.mode is Mode.FULL or default
|
|
261
|
+
|
|
262
|
+
def build_tree(self) -> None:
|
|
263
|
+
# A new file keeps the sections you had open (before any search opened the rest).
|
|
264
|
+
expanded = self._expanded_before_search
|
|
265
|
+
if expanded is None:
|
|
266
|
+
expanded = self.expanded_sections()
|
|
267
|
+
self._updating = True
|
|
268
|
+
self.tree.clear()
|
|
269
|
+
tops: dict[str, QTreeWidgetItem] = {}
|
|
270
|
+
for f in self.fields:
|
|
271
|
+
top = tops.get(f.section)
|
|
272
|
+
if top is None:
|
|
273
|
+
top = tops[f.section] = QTreeWidgetItem(self.tree, [f.section])
|
|
274
|
+
top.setFlags(top.flags() | Qt.ItemFlag.ItemIsAutoTristate | CHECKABLE)
|
|
275
|
+
label = f.label if f.label == f.name else f"{f.label} ({f.name})"
|
|
276
|
+
item = QTreeWidgetItem(top, [label])
|
|
277
|
+
item.setData(0, PARAM, f.param)
|
|
278
|
+
item.setData(0, DEFAULT, f.default)
|
|
279
|
+
tip = describe(f.param)
|
|
280
|
+
item.setToolTip(0, f"{f.param}\n{f.value}" + (f"\n\n{tip}" if tip else ""))
|
|
281
|
+
item.setFlags(item.flags() | CHECKABLE)
|
|
282
|
+
item.setCheckState(0, _check(self.ticked(f.param, f.default)))
|
|
283
|
+
for top in self.sections():
|
|
284
|
+
top.setExpanded(top.text(0) in expanded)
|
|
285
|
+
self._updating = False
|
|
286
|
+
self._expanded_before_search = None
|
|
287
|
+
self.apply_filter(self.search.text())
|
|
288
|
+
|
|
289
|
+
def expanded_sections(self) -> set[str]:
|
|
290
|
+
return {top.text(0) for top in self.sections() if top.isExpanded()}
|
|
291
|
+
|
|
292
|
+
def set_expanded(self, on: bool) -> None:
|
|
293
|
+
for top in self.sections():
|
|
294
|
+
if not top.isHidden():
|
|
295
|
+
top.setExpanded(on)
|
|
296
|
+
|
|
297
|
+
def set_selection(self, params: set[str]) -> None:
|
|
298
|
+
"""Replace the ticks for this file's fields; saved fields this file lacks are kept."""
|
|
299
|
+
here = {f.param for f in self.fields}
|
|
300
|
+
self.selected = (self.selected - here) | (params & here)
|
|
301
|
+
self.refresh_ticks()
|
|
302
|
+
|
|
303
|
+
def select_defaults(self) -> None:
|
|
304
|
+
self.set_selection({f.param for f in self.fields if f.default})
|
|
305
|
+
|
|
306
|
+
def clear_selection(self) -> None:
|
|
307
|
+
self.set_selection(set())
|
|
308
|
+
|
|
309
|
+
def refresh_ticks(self) -> None:
|
|
310
|
+
self._updating = True
|
|
311
|
+
for item in self.items():
|
|
312
|
+
item.setCheckState(0, _check(self.ticked(item.data(0, PARAM), item.data(0, DEFAULT))))
|
|
313
|
+
self._updating = False
|
|
314
|
+
self.update_count()
|
|
315
|
+
|
|
316
|
+
def item_changed(self, item: QTreeWidgetItem, _col: int) -> None:
|
|
317
|
+
if self._updating or self.mode is not Mode.SELECTED:
|
|
318
|
+
return
|
|
319
|
+
if item.parent() is None: # a section's tristate box; its children change and report themselves
|
|
320
|
+
return
|
|
321
|
+
param = item.data(0, PARAM)
|
|
322
|
+
if item.checkState(0) == Qt.CheckState.Checked:
|
|
323
|
+
self.selected.add(param)
|
|
324
|
+
else:
|
|
325
|
+
self.selected.discard(param)
|
|
326
|
+
# The same param in another section of that kind (Audio #1 and #2) follows: mediainfo hides
|
|
327
|
+
# and shows by kind, not by stream.
|
|
328
|
+
self._updating = True
|
|
329
|
+
for other in self.items():
|
|
330
|
+
if other is not item and other.data(0, PARAM) == param:
|
|
331
|
+
other.setCheckState(0, item.checkState(0))
|
|
332
|
+
self._updating = False
|
|
333
|
+
self.update_count()
|
|
334
|
+
|
|
335
|
+
def apply_filter(self, text: str) -> None:
|
|
336
|
+
needle = text.strip().lower()
|
|
337
|
+
# Searching opens every section with a hit; clearing the search puts back what was open.
|
|
338
|
+
restore = None
|
|
339
|
+
if needle and self._expanded_before_search is None:
|
|
340
|
+
self._expanded_before_search = self.expanded_sections()
|
|
341
|
+
elif not needle and self._expanded_before_search is not None:
|
|
342
|
+
restore, self._expanded_before_search = self._expanded_before_search, None
|
|
343
|
+
for top in self.sections():
|
|
344
|
+
visible = 0
|
|
345
|
+
for j in range(top.childCount()):
|
|
346
|
+
child = top.child(j)
|
|
347
|
+
if child is None:
|
|
348
|
+
continue
|
|
349
|
+
hit = not needle or needle in child.text(0).lower()
|
|
350
|
+
child.setHidden(not hit)
|
|
351
|
+
visible += hit
|
|
352
|
+
top.setHidden(visible == 0)
|
|
353
|
+
if needle:
|
|
354
|
+
top.setExpanded(visible > 0)
|
|
355
|
+
elif restore is not None:
|
|
356
|
+
top.setExpanded(top.text(0) in restore)
|
|
357
|
+
self.update_count()
|
|
358
|
+
|
|
359
|
+
def update_count(self) -> None:
|
|
360
|
+
params = {f.param for f in self.fields}
|
|
361
|
+
if self.mode is Mode.FULL:
|
|
362
|
+
n = len(params)
|
|
363
|
+
elif self.mode is Mode.DEFAULT:
|
|
364
|
+
n = len({f.param for f in self.fields if f.default})
|
|
365
|
+
else:
|
|
366
|
+
n = len(self.selected & params)
|
|
367
|
+
self.count.setText(f"{n} of {len(params)} fields")
|
|
368
|
+
|
|
369
|
+
# --- file, apply, copy ----------------------------------------------------------------------
|
|
370
|
+
|
|
371
|
+
def browse(self) -> None:
|
|
372
|
+
start = self.settings.last_dir or str(Path.home())
|
|
373
|
+
path, _ = QFileDialog.getOpenFileName(self, "Choose a media file", start)
|
|
374
|
+
if path:
|
|
375
|
+
self.open_file(path)
|
|
376
|
+
|
|
377
|
+
def open_file(self, path: str) -> None:
|
|
378
|
+
if self.mi is None:
|
|
379
|
+
return
|
|
380
|
+
QGuiApplication.setOverrideCursor(Qt.CursorShape.WaitCursor)
|
|
381
|
+
try:
|
|
382
|
+
self.fields = self.mi.read_fields(path)
|
|
383
|
+
except MediaInfoError as exc:
|
|
384
|
+
QMessageBox.warning(self, "mediainfo", str(exc))
|
|
385
|
+
return
|
|
386
|
+
finally:
|
|
387
|
+
QGuiApplication.restoreOverrideCursor()
|
|
388
|
+
self.path = path
|
|
389
|
+
self.file_edit.setText(path)
|
|
390
|
+
self.log(f"Opened {path}")
|
|
391
|
+
self.settings.last_dir = str(Path(path).parent)
|
|
392
|
+
if self.mode is Mode.SELECTED and not self.settings.selected_initialised:
|
|
393
|
+
self.selected = {f.param for f in self.fields if f.default}
|
|
394
|
+
self.settings.selected_initialised = True
|
|
395
|
+
self.build_tree()
|
|
396
|
+
self.update_palette_state()
|
|
397
|
+
self.apply()
|
|
398
|
+
|
|
399
|
+
def apply(self) -> None:
|
|
400
|
+
if self.mi is None:
|
|
401
|
+
return
|
|
402
|
+
if not self.path:
|
|
403
|
+
self.log("Choose a file first.")
|
|
404
|
+
return
|
|
405
|
+
p = plan(self.mode, self.fields, self.selected, self.known)
|
|
406
|
+
QGuiApplication.setOverrideCursor(Qt.CursorShape.WaitCursor)
|
|
407
|
+
try:
|
|
408
|
+
result = self.mi.render(self.path, p)
|
|
409
|
+
except MediaInfoError as exc:
|
|
410
|
+
self.output.setPlainText(f"mediainfo failed:\n\n{exc}")
|
|
411
|
+
self.statusBar().showMessage(f"mediainfo failed: {exc}")
|
|
412
|
+
return
|
|
413
|
+
finally:
|
|
414
|
+
QGuiApplication.restoreOverrideCursor()
|
|
415
|
+
self.statusBar().clearMessage()
|
|
416
|
+
self.output.setPlainText(result.text)
|
|
417
|
+
self.command.setText(result.command)
|
|
418
|
+
self.command.setCursorPosition(0)
|
|
419
|
+
self.log(f"Applied {self.radios[self.mode].text()}: {self.count.text()}.")
|
|
420
|
+
if result.removed_labels:
|
|
421
|
+
self.log("Removed from the text (mediainfo cannot hide them): " + ", ".join(result.removed_labels))
|
|
422
|
+
if p.unshowable:
|
|
423
|
+
self.log("Cannot be shown outside Full: " + ", ".join(p.unshowable))
|
|
424
|
+
|
|
425
|
+
def log(self, message: str) -> None:
|
|
426
|
+
self.log_view.appendPlainText(f"{datetime.now():%H:%M:%S} {message}")
|
|
427
|
+
|
|
428
|
+
def copy(self, text: str, what: str) -> None:
|
|
429
|
+
clipboard = QGuiApplication.clipboard()
|
|
430
|
+
if text and clipboard is not None:
|
|
431
|
+
clipboard.setText(text)
|
|
432
|
+
self.log(f"{what} copied to the clipboard.")
|
|
433
|
+
|
|
434
|
+
def closeEvent(self, event: QCloseEvent) -> None:
|
|
435
|
+
s = self.settings
|
|
436
|
+
s.mode = self.mode.value
|
|
437
|
+
s.selected = sorted(self.selected)
|
|
438
|
+
s.geometry = bytes(self.saveGeometry().toHex().data()).decode()
|
|
439
|
+
s.splitter = bytes(self.splitter.saveState().toHex().data()).decode()
|
|
440
|
+
try:
|
|
441
|
+
config.save(s)
|
|
442
|
+
except OSError as exc:
|
|
443
|
+
QMessageBox.warning(self, "Settings", f"Could not save settings: {exc}")
|
|
444
|
+
super().closeEvent(event)
|
|
445
|
+
|
|
446
|
+
|
|
447
|
+
def main() -> int:
|
|
448
|
+
app = QApplication(sys.argv)
|
|
449
|
+
app.setApplicationName("mediainfo-picker") # also names the settings folder: keep it
|
|
450
|
+
app.setWindowIcon(app_icon())
|
|
451
|
+
try:
|
|
452
|
+
mi, err = MediaInfo(), ""
|
|
453
|
+
except MediaInfoError as exc:
|
|
454
|
+
mi, err = None, str(exc)
|
|
455
|
+
w = Window(mi, err)
|
|
456
|
+
w.show()
|
|
457
|
+
if mi and len(sys.argv) > 1:
|
|
458
|
+
w.open_file(sys.argv[1])
|
|
459
|
+
return app.exec()
|
|
460
|
+
|
|
461
|
+
|
|
462
|
+
if __name__ == "__main__":
|
|
463
|
+
sys.exit(main())
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"""The user's choices, kept between runs in one small JSON file.
|
|
2
|
+
|
|
3
|
+
Stored in the platform's config location (on Linux `~/.config/mediainfo-picker/settings.json`).
|
|
4
|
+
Fields are saved as "Kind_Name" params, so a selection made on one file applies to any other; a
|
|
5
|
+
saved field the current file lacks is kept, not dropped, and used again when a file has it.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import json
|
|
11
|
+
from dataclasses import asdict, dataclass, field
|
|
12
|
+
from pathlib import Path
|
|
13
|
+
|
|
14
|
+
from PySide6.QtCore import QStandardPaths
|
|
15
|
+
|
|
16
|
+
FILE = "settings.json"
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@dataclass
|
|
20
|
+
class Settings:
|
|
21
|
+
mode: str = "default"
|
|
22
|
+
selected: list[str] = field(default_factory=list)
|
|
23
|
+
selected_initialised: bool = False # becomes True the first time Selected fields is used
|
|
24
|
+
last_dir: str = ""
|
|
25
|
+
geometry: str = "" # QMainWindow.saveGeometry(), hex
|
|
26
|
+
splitter: str = "" # QSplitter.saveState(), hex
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def config_path() -> Path:
|
|
30
|
+
base = QStandardPaths.writableLocation(QStandardPaths.StandardLocation.AppConfigLocation)
|
|
31
|
+
return Path(base) / FILE
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def load() -> Settings:
|
|
35
|
+
try:
|
|
36
|
+
data = json.loads(config_path().read_text())
|
|
37
|
+
except (OSError, ValueError):
|
|
38
|
+
return Settings()
|
|
39
|
+
known = Settings.__dataclass_fields__
|
|
40
|
+
return Settings(**{k: v for k, v in data.items() if k in known})
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def save(s: Settings) -> None:
|
|
44
|
+
p = config_path()
|
|
45
|
+
p.parent.mkdir(parents=True, exist_ok=True)
|
|
46
|
+
tmp = p.with_suffix(".tmp")
|
|
47
|
+
tmp.write_text(json.dumps(asdict(s), indent=2) + "\n")
|
|
48
|
+
tmp.replace(p)
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64" width="64" height="64">
|
|
2
|
+
<rect width="64" height="64" rx="14" fill="#17707F"/>
|
|
3
|
+
<path d="M17 33.5 L27.5 44 L47 22" fill="none" stroke="#FFFFFF" stroke-width="7"
|
|
4
|
+
stroke-linecap="round" stroke-linejoin="round"/>
|
|
5
|
+
</svg>
|