-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.py
295 lines (261 loc) · 11.1 KB
/
plugin.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
plugins.__init__.py
Written by: Josh.5 <[email protected]>
Date: 02 May 2022, (5:19 PM)
Copyright:
Copyright (C) 2021 Josh Sunnex
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General
Public License as published by the Free Software Foundation, version 3.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License along with this program.
If not, see <https://www.gnu.org/licenses/>.
"""
import logging
from unmanic.libs.unplugins.settings import PluginSettings
from ignore_video_file_over_resolution.lib.ffmpeg import Probe
# Configure plugin logger
logger = logging.getLogger("Unmanic.Plugin.ignore_video_file_over_resolution")
default_resolutions = {
'480p_sdtv': {
'width': 854,
'height': 480,
'label': "480p (SDTV)",
},
'576p_sdtv': {
'width': 1024,
'height': 576,
'label': "576p (SDTV)",
},
'720p_hdtv': {
'width': 1280,
'height': 720,
'label': "720p (HDTV)",
},
'1080p_hdtv': {
'width': 1920,
'height': 1080,
'label': "1080p (HDTV)",
},
'dci_2k_hdtv': {
'width': 2048,
'height': 1080,
'label': "DCI 2K (HDTV)",
},
'1440p': {
'width': 2560,
'height': 1440,
'label': "1440p (WQHD)",
},
'4k_uhd': {
'width': 3840,
'height': 2160,
'label': "4K (UHD)",
},
'dci_4k': {
'width': 4096,
'height': 2160,
'label': "DCI 4K",
},
'8k_uhd': {
'width': 8192,
'height': 4608,
'label': "8k (UHD)",
},
}
class Settings(PluginSettings):
settings = {
"target_resolution": '720p_hdtv',
"custom_resolutions": False,
'480p_sdtv_width': default_resolutions.get('480p_sdtv', {}).get('width'),
'480p_sdtv_height': default_resolutions.get('480p_sdtv', {}).get('height'),
'576p_sdtv_width': default_resolutions.get('576p_sdtv', {}).get('width'),
'576p_sdtv_height': default_resolutions.get('576p_sdtv', {}).get('height'),
'720p_hdtv_width': default_resolutions.get('720p_hdtv', {}).get('width'),
'720p_hdtv_height': default_resolutions.get('720p_hdtv', {}).get('height'),
'1080p_hdtv_width': default_resolutions.get('1080p_hdtv', {}).get('width'),
'1080p_hdtv_height': default_resolutions.get('1080p_hdtv', {}).get('height'),
'dci_2k_hdtv_width': default_resolutions.get('dci_2k_hdtv', {}).get('width'),
'dci_2k_hdtv_height': default_resolutions.get('dci_2k_hdtv', {}).get('height'),
'1440p_width': default_resolutions.get('1440p', {}).get('width'),
'1440p_height': default_resolutions.get('1440p', {}).get('height'),
'4k_uhd_width': default_resolutions.get('4k_uhd', {}).get('width'),
'4k_uhd_height': default_resolutions.get('4k_uhd', {}).get('height'),
'dci_4k_width': default_resolutions.get('dci_4k', {}).get('width'),
'dci_4k_height': default_resolutions.get('dci_4k', {}).get('height'),
'8k_uhd_width': default_resolutions.get('8k_uhd', {}).get('width'),
'8k_uhd_height': default_resolutions.get('8k_uhd', {}).get('height'),
}
def __init__(self, *args, **kwargs):
super(Settings, self).__init__(*args, **kwargs)
self.form_settings = {
"target_resolution": self.__set_target_resolution_form_settings(),
"custom_resolutions": {
"label": "Customise resolution values",
},
}
for key in self.settings:
if '_width' not in key and '_height' not in key:
continue
res_key = key[:-len('_width')]
axis = 'width'
if '_height' in key:
res_key = key[:-len('_height')]
axis = 'height'
title_prefix = default_resolutions.get(res_key, {}).get('label')
current_val = default_resolutions.get(res_key, {}).get(axis)
min_val = int(int(current_val) / 2)
max_val = int(int(current_val) * 2)
self.form_settings[key] = self.__set_custom_resolution_form_settings(title_prefix, axis,
min_val=min_val, max_val=max_val)
def __set_custom_resolution_form_settings(self, title_prefix, axis, min_val=0, max_val=999999, step=1):
values = {
"label": "{} - {}".format(title_prefix, axis.capitalize()),
"input_type": "slider",
"slider_options": {
"min": min_val,
"max": max_val,
"step": step,
"suffix": "px"
},
}
if not self.get_setting('custom_resolutions'):
values["display"] = 'hidden'
return values
def __set_target_resolution_form_settings(self):
def generate_label_resolution(key):
return "{} - {}x{}".format(default_resolutions.get(key, {}).get('label'),
default_resolutions.get(key, {}).get('width'),
default_resolutions.get(key, {}).get('height'))
values = {
"label": "Resolution",
"input_type": "select",
"select_options": [
{
'value': '480p_sdtv',
'label': generate_label_resolution('480p_sdtv'),
},
{
'value': '576p_sdtv',
'label': generate_label_resolution('576p_sdtv'),
},
{
'value': '720p_hdtv',
'label': generate_label_resolution('720p_hdtv'),
},
{
'value': '1080p_hdtv',
'label': generate_label_resolution('1080p_hdtv'),
},
{
'value': 'dci_2k_hdtv',
'label': generate_label_resolution('dci_2k_hdtv'),
},
{
'value': '1440p',
'label': generate_label_resolution('1440p'),
},
{
'value': '4k_uhd',
'label': generate_label_resolution('4k_uhd'),
},
{
'value': 'dci_4k',
'label': generate_label_resolution('dci_4k'),
},
{
'value': '8k_uhd',
'label': generate_label_resolution('8k_uhd'),
},
],
}
return values
def get_test_resolution(settings):
target_resolution = settings.get_setting('target_resolution')
# Set the target resolution
custom_resolutions = settings.get_setting('custom_resolutions')
test_resolution = {
'width': default_resolutions.get(target_resolution, {}).get('width'),
'height': default_resolutions.get(target_resolution, {}).get('height'),
}
if custom_resolutions:
test_resolution = {
'width': settings.get_setting('{}_width'.format(target_resolution)),
'height': settings.get_setting('{}_height'.format(target_resolution)),
}
return test_resolution
def get_video_stream_data(streams):
width = 0
height = 0
video_stream_index = 0
for stream in streams:
if stream.get('codec_type') == 'video':
width = stream.get('width', stream.get('coded_width', 0))
height = stream.get('height', stream.get('coded_height', 0))
video_stream_index = stream.get('index')
break
return width, height, video_stream_index
def on_library_management_file_test(data):
"""
Runner function - enables additional actions during the library management file tests.
The 'data' object argument includes:
library_id - The library that the current task is associated with
path - String containing the full path to the file being tested.
issues - List of currently found issues for not processing the file.
add_file_to_pending_tasks - Boolean, is the file currently marked to be added to the queue for processing.
priority_score - Integer, an additional score that can be added to set the position of the new task in the task queue.
shared_info - Dictionary, information provided by previous plugin runners. This can be appended to for subsequent runners.
:param data:
:return:
"""
# Get the path to the file
abspath = data.get('path')
# Get file probe
probe = Probe(logger, allowed_mimetypes=['video'])
if 'ffprobe' in data.get('shared_info', {}):
if not probe.set_probe(data.get('shared_info', {}).get('ffprobe')):
# Failed to set ffprobe from shared info.
# Probably due to it being for an incompatible mimetype declared above
return
elif not probe.file(abspath):
# File probe failed, skip the rest of this test
return
# Set file probe to shared infor for subsequent file test runners
if 'shared_info' in data:
data['shared_info'] = {}
data['shared_info']['ffprobe'] = probe.get_probe()
# Configure settings object (maintain compatibility with v1 plugins)
if data.get('library_id'):
settings = Settings(library_id=data.get('library_id'))
else:
settings = Settings()
# Get video width and height
vid_width, vid_height, video_stream_index = get_video_stream_data(probe.get('streams', []))
# Get the test resolution
test_resolution = get_test_resolution(settings)
limit_label = default_resolutions.get(settings.get_setting('target_resolution'), {}).get('label')
limit_width = default_resolutions.get(settings.get_setting('target_resolution'), {}).get('width')
limit_height = default_resolutions.get(settings.get_setting('target_resolution'), {}).get('height')
if int(vid_width) > int(test_resolution['width']) or int(vid_height) > int(test_resolution['height']):
# Ignore this file
data['add_file_to_pending_tasks'] = False
logger.debug(
"File '%s' should be ignored - resolution %sx%s is over the set limit of %s %sx%s.",
abspath,
vid_width,
vid_height,
limit_label,
limit_width,
limit_height)
return
logger.debug(
"File '%s' will not be ignored - resolution %sx%s is under the set limit of %s %sx%s.",
abspath,
vid_width,
vid_height,
limit_label,
limit_width,
limit_height)