-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplugin.py
339 lines (269 loc) · 12.5 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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Written by: Josh.5 <[email protected]>
Date: 9 March 2021, (1:09 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
import os
from configparser import NoSectionError, NoOptionError
from unmanic.libs.unplugins.settings import PluginSettings
from unmanic.libs.directoryinfo import UnmanicDirectoryInfo
from video_trim.lib.ffmpeg import StreamMapper, Probe, Parser
# Configure plugin logger
logger = logging.getLogger("Unmanic.Plugin.video_trim")
class Settings(PluginSettings):
settings = {
"start_seconds": 0,
"end_seconds": 0,
'ignore_previously_processed': True,
}
form_settings = {
"start_seconds": {
"label": "Seconds to trim off the start of the files",
},
"end_seconds": {
"label": "Seconds to trim off the end of the files",
},
"ignore_previously_processed": {
"label": "Ignore all files previously trimmed with this plugin.",
},
}
class PluginStreamMapper(StreamMapper):
def __init__(self):
super(PluginStreamMapper, self).__init__(logger, ['video', 'audio'])
self.settings = None
def set_settings(self, settings):
self.settings = settings
def test_stream_needs_processing(self, stream_info: dict):
# No streams need to be modified with custom mapping. Copy all.
return False
def custom_stream_mapping(self, stream_info: dict, stream_id: int):
# Will not be called as above function returns False every time.
return {
'stream_mapping': [],
'stream_encoding': [],
}
def __gen_start_args(self, duration):
start_seconds = self.settings.get_setting('start_seconds')
advanced_options = {}
if start_seconds and float(start_seconds) > 0:
# Ensure the start trim is less than the duration of the file
if float(start_seconds) > float(duration):
# The configured value is larger than the duration of the file.
# Skip this file for now...
return advanced_options
# Build the start trim args
advanced_options = {
"-ss": str(self.settings.get_setting('start_seconds')),
}
self.set_ffmpeg_advanced_options(**advanced_options)
return advanced_options
def __gen_end_args(self, duration):
# Reduce duration by X seconds less the start_seconds
end_seconds = self.settings.get_setting('end_seconds')
start_seconds = self.settings.get_setting('start_seconds')
advanced_options = {}
if end_seconds and float(end_seconds) > 0:
# Ensure the end trim is less than the duration of the file, less the start trim
if float(end_seconds) > float(duration):
# The configured value is larger than the duration of the file.
# Skip this file for now...
return advanced_options
# Build the start trim args
advanced_options = {
"-to": str(float(duration) - float(end_seconds)),
}
self.set_ffmpeg_advanced_options(**advanced_options)
return advanced_options
def gen_trim_args(self):
"""
Generate a list of args for using a VAAPI decoder
:return:
"""
file_probe_format = self.probe.get('format', {})
duration = file_probe_format.get('duration')
if not duration:
# Without duration, we cannot set the start or end cut points
return ''
# generate the args
start_args = self.__gen_start_args(duration)
end_args = self.__gen_end_args(duration)
# Create an args string to be used to mark against a file
args_string = ''
for key in start_args:
args_string += "{} {}".format(key, start_args.get(key))
logger.debug("args_string: '{}'".format(args_string))
for key in end_args:
args_string += "{} {}".format(key, end_args.get(key))
logger.debug("args_string: '{}'".format(args_string))
return args_string
@staticmethod
def file_already_trimmed(settings, path):
directory_info = UnmanicDirectoryInfo(os.path.dirname(path))
try:
previous_trim = directory_info.get('video_trim', os.path.basename(path))
except NoSectionError as e:
previous_trim = ''
except NoOptionError as e:
previous_trim = ''
except Exception as e:
logger.debug("Unknown exception {}.".format(e))
previous_trim = ''
if previous_trim:
logger.debug("File was previously trimmed with {}.".format(previous_trim))
# This stream already has been normalised
if settings.get_setting('ignore_previously_processed'):
logger.debug("Plugin configured to ignore previously normalised streams")
return True
# Default to...
return False
def on_library_management_file_test(data):
"""
Runner function - enables additional actions during the library management file tests.
The 'data' object argument includes:
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.
:param data:
:return:
"""
# Get the path to the file
abspath = data.get('path')
# Get file probe
probe = Probe(logger, allowed_mimetypes=['video', 'audio'])
if not probe.file(abspath):
# File probe failed, skip the rest of this test
return data
# 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 stream mapper
mapper = PluginStreamMapper()
mapper.set_settings(settings)
mapper.set_probe(probe)
if not mapper.file_already_trimmed(settings, abspath):
# Mark this file to be added to the pending tasks
data['add_file_to_pending_tasks'] = True
logger.debug("File '{}' should be added to task list. File has not been previously trimmed.".format(abspath))
else:
logger.debug("File '{}' has been previously trimmed.".format(abspath))
return data
def on_worker_process(data):
"""
Runner function - enables additional configured processing jobs during the worker stages of a task.
The 'data' object argument includes:
exec_command - A command that Unmanic should execute. Can be empty.
command_progress_parser - A function that Unmanic can use to parse the STDOUT of the command to collect progress stats. Can be empty.
file_in - The source file to be processed by the command.
file_out - The destination that the command should output (may be the same as the file_in if necessary).
original_file_path - The absolute path to the original file.
repeat - Boolean, should this runner be executed again once completed with the same variables.
DEPRECIATED 'data' object args passed for legacy Unmanic versions:
exec_ffmpeg - Boolean, should Unmanic run FFMPEG with the data returned from this plugin.
ffmpeg_args - A list of Unmanic's default FFMPEG args.
:param data:
:return:
"""
# Default to no FFMPEG command required. This prevents the FFMPEG command from running if it is not required
data['exec_command'] = []
data['repeat'] = False
# Get the path to the file
abspath = data.get('file_in')
# Get file probe
probe = Probe(logger, allowed_mimetypes=['video', 'audio'])
if not probe.file(abspath):
# File probe failed, skip the rest of this test
return data
# Configure settings object (maintain compatibility with v1 plugins)
if data.get('library_id'):
settings = Settings(library_id=data.get('library_id'))
else:
settings = Settings()
# Fetch duration from file probe...
file_probe_format = probe.get('format', {})
duration = file_probe_format.get('duration')
if not duration:
# Without duration, we cannot set the start or end cut points
return data
# Get stream mapper
mapper = PluginStreamMapper()
mapper.set_settings(settings)
mapper.set_probe(probe)
if not mapper.file_already_trimmed(settings, abspath):
# Set the input file
mapper.set_input_file(abspath)
# Do not remux the file. Keep the file out in the same container
mapper.set_output_file(data.get('file_out'))
# Set the trim args
mapper.gen_trim_args()
# Set stream mapping and encoding args
mapper.get_stream_mapping()
mapper.get_stream_encoding()
# Get generated ffmpeg args
ffmpeg_args = mapper.get_ffmpeg_args()
# Apply ffmpeg args to command
data['exec_command'] = ['ffmpeg']
data['exec_command'] += ffmpeg_args
logger.debug("ffmpeg.args: '{}'".format(ffmpeg_args))
# Set the parser
parser = Parser(logger)
parser.set_probe(probe)
data['command_progress_parser'] = parser.parse_progress
return data
def on_postprocessor_task_results(data):
"""
Runner function - provides a means for additional postprocessor functions based on the task success.
The 'data' object argument includes:
task_processing_success - Boolean, did all task processes complete successfully.
file_move_processes_success - Boolean, did all postprocessor movement tasks complete successfully.
destination_files - List containing all file paths created by postprocessor file movements.
source_data - Dictionary containing data pertaining to the original source file.
:param data:
:return:
"""
# We only care that the task completed successfully.
# If a worker processing task was unsuccessful, dont mark the file as being normalised
# TODO: Figure out a way to know if a file was normalised but another plugin was the
# cause of the task processing failure flag
if not data.get('task_processing_success'):
return data
# Configure settings object (maintain compatibility with v1 plugins)
if data.get('library_id'):
settings = Settings(library_id=data.get('library_id'))
else:
settings = Settings()
# Loop over the destination_files list and update the directory info file for each one
for destination_file in data.get('destination_files'):
# Get the original file's absolute path
if not os.path.exists(destination_file):
logger.error("Destination file does not exist '{}'.".format(destination_file))
continue
# Get file probe
probe = Probe(logger, allowed_mimetypes=['video', 'audio'])
if not probe.file(destination_file):
# File probe failed, skip the rest of this test
logger.error("Destination file could not be probed! Is it corrupted? '{}'.".format(destination_file))
continue
# Get stream mapper
mapper = PluginStreamMapper()
mapper.set_settings(settings)
mapper.set_probe(probe)
# Get trim args for the source file
trim_args = mapper.gen_trim_args()
directory_info = UnmanicDirectoryInfo(os.path.dirname(destination_file))
directory_info.set('video_trim', os.path.basename(destination_file), trim_args)
directory_info.save()
logger.debug("Video Trim info written for '{}'.".format(destination_file))
return data