forked from Josh5/unmanic.plugin.helpers.ffmpeg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream_mapper.py
503 lines (427 loc) · 19.2 KB
/
stream_mapper.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
unmanic.stream_mapper.py
Written by: Josh.5 <[email protected]>
Date: 30 Jul 2021, (9:41 AM)
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 os
import shutil
from logging import Logger
from .probe import Probe
class StreamMapper(object):
"""
StreamMapper
Manage FFmpeg stream mapping and generating FFmpeg command-line args.
"""
probe: Probe = None
stream_type_idents = {
'video': 'v',
'audio': 'a',
'subtitle': 's',
'data': 'd',
'attachment': 't'
}
processing_stream_type = ''
found_streams_to_encode = False
stream_mapping = []
stream_encoding = []
video_stream_count = 0
audio_stream_count = 0
subtitle_stream_count = 0
data_stream_count = 0
attachment_stream_count = 0
input_file = ''
output_file = ''
generic_options = []
main_options = []
advanced_options = []
format_options = []
def __init__(self, logger: Logger, processing_stream_type: list):
# Ensure ffmpeg is installed
if shutil.which('ffmpeg') is None:
raise Exception("Unable to find executable 'ffmpeg'. Please ensure that FFmpeg is installed correctly.")
self.logger = logger
if processing_stream_type is not None:
if any(pst for pst in processing_stream_type if
pst not in ['video', 'audio', 'subtitle', 'data', 'attachment']):
raise Exception(
"processing_stream_type must be one of ['video','audio','subtitle','data','attachment']")
self.processing_stream_type = processing_stream_type
# Set default Generic options
self.generic_options = [
'-hide_banner',
'-loglevel', 'info',
]
# Set default Main options
self.main_options = []
# Set default Advanced options
self.advanced_options = [
'-strict', '-2',
'-max_muxing_queue_size', '4096',
]
def __copy_stream_mapping(self, codec_type, stream_id):
"""
Create stream mapping to simply copy the stream without encoding.
Apply this to the 'stream_mapping' and 'stream_encoding' attributes.
:param codec_type:
:param stream_id:
:return:
"""
# Map this stream for copy to the destination file
self.stream_mapping += ['-map', '0:{}:{}'.format(codec_type, stream_id)]
# Add a encoding flag copying this stream
self.stream_encoding += ['-c:{}:{}'.format(codec_type, stream_id), 'copy']
def __apply_custom_stream_mapping(self, mapping_dict):
"""
Apply a custom stream mapping.
This method tests that the provided mapping dictionary is valid.
If it is valid, apply it to the 'stream_mapping' and 'stream_encoding' attributes.
:param mapping_dict:
:return:
"""
# Ensure the mapping dictionary provided is correct
if not isinstance(mapping_dict, dict):
raise Exception("processing_stream_type must return a dictionary")
if 'stream_mapping' not in mapping_dict:
raise Exception("processing_stream_type return dictionary must contain 'stream_mapping' key")
if not isinstance(mapping_dict.get('stream_mapping'), list):
raise Exception("processing_stream_type 'stream_mapping' value must be of type 'list'")
if 'stream_encoding' not in mapping_dict:
raise Exception("processing_stream_type return dictionary must contain 'stream_encoding' key")
if not isinstance(mapping_dict.get('stream_encoding'), list):
raise Exception("processing_stream_type 'stream_mapping' value must be of type 'list'")
# Append this custom stream mapping
self.stream_mapping += mapping_dict.get('stream_mapping')
# Append these custom encoding flags
self.stream_encoding += mapping_dict.get('stream_encoding')
def set_probe(self, probe: Probe):
"""Set the ffprobe Probe object"""
self.probe = probe
def test_stream_needs_processing(self, stream_info: dict):
"""
Overwrite this function to test a stream.
Return 'True' if it needs to be process.
Return 'False' if it should just be copied over to the new file.
:param stream_info:
:return: bool
"""
raise NotImplementedError
def custom_stream_mapping(self, stream_info: dict, stream_id: int):
"""
Configure custom mapping for a single stream
This function must return a dictionary containing 2 key values:
{
'stream_mapping': [],
'stream_encoding': [],
}
:param stream_info:
:param stream_id:
:return: dict
"""
raise NotImplementedError
def __set_stream_mapping(self):
"""
Sets a list of stream maps and encoding variables
:return:
"""
# Require a list of probe streams to continue
file_probe_streams = self.probe.get('streams')
if not file_probe_streams:
return False
# What type of streams are we looking for ('video', 'audio', 'subtitle', 'data' or 'attachment')
processing_stream_type = self.processing_stream_type
# Map the streams into two arrays that will be placed together in the correct order.
self.stream_mapping = []
self.stream_encoding = []
# Count streams by type
self.video_stream_count = 0
self.audio_stream_count = 0
self.subtitle_stream_count = 0
self.data_stream_count = 0
self.attachment_stream_count = 0
# Set flag for finding a stream that needs to be processed as False by default.
found_streams_to_process = False
# Loop over all streams found in the file probe
for stream_info in file_probe_streams:
codec_type = stream_info.get('codec_type', '').lower()
# Fore each of these streams:
# If this is a video/image stream?
if codec_type == "video":
# Map the video stream
if "video" in processing_stream_type:
if not self.test_stream_needs_processing(stream_info):
self.__copy_stream_mapping('v', self.video_stream_count)
self.video_stream_count += 1
continue
else:
mapping = self.custom_stream_mapping(stream_info, self.video_stream_count)
if mapping:
found_streams_to_process = True
self.__apply_custom_stream_mapping(mapping)
else:
self.__copy_stream_mapping('v', self.video_stream_count)
self.video_stream_count += 1
continue
else:
self.__copy_stream_mapping('v', self.video_stream_count)
self.video_stream_count += 1
continue
# If this is a audio stream?
elif codec_type == "audio":
# Map the audio stream
if "audio" in processing_stream_type:
if not self.test_stream_needs_processing(stream_info):
self.__copy_stream_mapping('a', self.audio_stream_count)
self.audio_stream_count += 1
continue
else:
mapping = self.custom_stream_mapping(stream_info, self.audio_stream_count)
if mapping:
found_streams_to_process = True
self.__apply_custom_stream_mapping(mapping)
else:
self.__copy_stream_mapping('a', self.audio_stream_count)
self.audio_stream_count += 1
continue
else:
if self.settings.get_setting('mode') == 'advanced':
amaps = self.settings.get_setting('custom_options').split()
self.logger.debug("Advanced Mode Video Settings with custom audio encoding: '%s'", amaps)
if '-c:a' not in amaps:
self.logger.debug("-c:a not detected in custom mappings: '%s'", amaps)
self.__copy_stream_mapping('a', self.audio_stream_count)
else:
self.logger.debug("-c:a detected in custom mappings: '%s'", amaps)
self.stream_mapping += ['-map', '0:{}:{}'.format('a', self.audio_stream_count)]
self.audio_stream_count += 1
else:
self.__copy_stream_mapping('a', self.audio_stream_count)
self.audio_stream_count += 1
continue
# If this is a subtitle stream?
elif codec_type == "subtitle":
# Map the subtitle stream
if "subtitle" in processing_stream_type:
if not self.test_stream_needs_processing(stream_info):
self.__copy_stream_mapping('s', self.subtitle_stream_count)
self.subtitle_stream_count += 1
continue
else:
mapping = self.custom_stream_mapping(stream_info, self.subtitle_stream_count)
if mapping:
found_streams_to_process = True
self.__apply_custom_stream_mapping(mapping)
else:
self.__copy_stream_mapping('s', self.subtitle_stream_count)
self.subtitle_stream_count += 1
continue
else:
if self.settings.get_setting('mode') == 'advanced':
submaps = self.settings.get_setting('custom_options').split()
self.logger.debug("Advanced Mode Video Settings with custom subtitle encoding: '%s'", submaps)
if '-c:s' not in submaps:
self.logger.debug("-c:s not detected in custom mappings: '%s'", submaps)
self.__copy_stream_mapping('s', self.subtitle_stream_count)
else:
self.logger.debug("-c:s detected in custom mappings: '%s'", submaps)
self.stream_mapping += ['-map', '0:{}:{}'.format('s', self.subtitle_stream_count)]
self.subtitle_stream_count += 1
else:
self.__copy_stream_mapping('s', self.subtitle_stream_count)
self.subtitle_stream_count += 1
continue
# If this is a data stream?
elif codec_type == "data":
# Map the data stream
if "data" in processing_stream_type:
if not self.test_stream_needs_processing(stream_info):
self.__copy_stream_mapping('d', self.data_stream_count)
self.data_stream_count += 1
continue
else:
mapping = self.custom_stream_mapping(stream_info, self.data_stream_count)
if mapping:
found_streams_to_process = True
self.__apply_custom_stream_mapping(mapping)
else:
self.__copy_stream_mapping('d', self.data_stream_count)
self.data_stream_count += 1
continue
else:
self.__copy_stream_mapping('d', self.data_stream_count)
self.data_stream_count += 1
continue
# If this is a attachment stream?
elif codec_type == "attachment":
# Map the attachment stream
if "attachment" in processing_stream_type:
if not self.test_stream_needs_processing(stream_info):
self.__copy_stream_mapping('t', self.attachment_stream_count)
self.attachment_stream_count += 1
continue
else:
mapping = self.custom_stream_mapping(stream_info, self.attachment_stream_count)
if mapping:
found_streams_to_process = True
self.__apply_custom_stream_mapping(mapping)
else:
self.__copy_stream_mapping('t', self.attachment_stream_count)
self.attachment_stream_count += 1
continue
else:
self.__copy_stream_mapping('t', self.attachment_stream_count)
self.attachment_stream_count += 1
continue
return found_streams_to_process
def __build_args(self, options: list, *args, **kwargs):
"""
Build a list of FFmpeg options based on the given default options, args and kwargs
:param options:
:param args:
:param kwargs:
:return:
"""
for arg in args:
if arg in options:
options = [value for value in options if value != arg]
options += [arg]
else:
options += [arg]
for kwarg in kwargs:
key = kwarg
value = kwargs.get(kwarg)
if key in options:
key_pos = options.index(key)
val_pos = int(key_pos) + 1
options[key_pos] = key
options[val_pos] = value
else:
options += [key, value]
return
def streams_need_processing(self):
"""
Returns True/False if the streams need to be processed.
If at least one stream needs custom stream mapping (processing), then this will return True.
If the stream mapping will copy all streams to output file untouched, then this will return False.
:return:
"""
return self.__set_stream_mapping()
def container_needs_remuxing(self, container_extension):
"""
Returns True/False if the file container needs to be processed.
:return:
"""
if not self.input_file:
raise Exception("Input file not yet set")
split_file_in = os.path.splitext(self.input_file)
if split_file_in[1].lstrip('.') != container_extension.lstrip('.'):
return True
return False
def set_input_file(self, path):
"""Set the input file for the FFmpeg args"""
self.input_file = os.path.abspath(path)
def set_output_file(self, path):
"""Set the output file for the FFmpeg args"""
self.output_file = os.path.abspath(path)
def set_output_null(self):
"""Set the output container to NULL for the FFmpeg args"""
self.output_file = '-'
if os.name == "nt":
# Windows uses NUL instead
self.output_file = 'NUL'
main_options = {
"-f": 'null',
}
self.__build_args(self.main_options, **main_options)
def set_ffmpeg_generic_options(self, *args, **kwargs):
"""
Set FFmpeg Generic options.
These are the initial options that follow the 'ffmpeg' command.
Ref:
http://ffmpeg.org/ffmpeg-all.html#Generic-options
:param args:
:param kwargs:
:return:
"""
self.__build_args(self.generic_options, *args, **kwargs)
def set_ffmpeg_main_options(self, *args, **kwargs):
"""
Set FFmpeg Main options.
These options follow the generic options.
They include things like the input file(s), metadata mapping, etc.
Ref:
http://ffmpeg.org/ffmpeg-all.html#Main-options
:return:
"""
self.__build_args(self.main_options, *args, **kwargs)
def set_ffmpeg_advanced_options(self, *args, **kwargs):
"""
Set FFmpeg Advanced options.
These options follow the generic options.
They include things like the custom stream mapping, custom metadata mapping,
filters, etc.
Note:
The custom stream mapping is carried out with another method.
This method should not be used for creating custom stream mapping if the
'stream_mapping' and 'stream_encoding' attributes are set.
Ref:
http://ffmpeg.org/ffmpeg-all.html#Advanced-options
:return:
"""
self.__build_args(self.advanced_options, *args, **kwargs)
def get_stream_mapping(self):
"""
Fetch the custom stream mapping generated by this class.
If the mapping args are not yet generated, generate them at this point.
:return:
"""
if not self.stream_mapping:
self.__set_stream_mapping()
return self.stream_mapping
def get_stream_encoding(self):
"""
Fetch the custom stream encoding args generated by this class.
If the encoding args are not yet generated, generate them at this point.
:return:
"""
if not self.stream_encoding:
self.__set_stream_mapping()
return self.stream_encoding
def get_ffmpeg_args(self):
"""
Build the FFmpeg command args and return them as a list.
:return:
"""
args = []
# Add generic options first
args += self.generic_options
# Add other main options
args += self.main_options
# Add the input file
# This class requires at least one input file specified with the input_file attribute
if not self.input_file:
raise Exception("Input file has not been set")
args += ['-i', self.input_file]
# Add advanced options. This includes the stream mapping and the encoding args
args += self.advanced_options
args += self.stream_mapping
args += self.stream_encoding
# Add the output file
# This class requires at least one output file specified with the output_file attribute
if not self.output_file:
raise Exception("Output file has not been set")
elif self.output_file == '-':
args += [self.output_file]
else:
args += ['-y', self.output_file]
return args