-
Notifications
You must be signed in to change notification settings - Fork 1
/
f47_bluefish_ffv1_tbc_fix.py
executable file
·509 lines (424 loc) · 16.7 KB
/
f47_bluefish_ffv1_tbc_fix.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
504
505
506
507
508
509
#!/usr/bin/env python3
'''
*** THIS SCRIPT MUST RUN WITH SHELL SCRIPT LAUNCH TO PASS FILES TO SYS.ARGV[1] AND DRIVE PARALLEL ***
Script that takes BlueFish MKV tbc 1/1000 and encodes to MKV tbc 1/25:
1. Shell script searches in paths for files that end in '.mkv' and passes on one at a time to Python
2. Receives single path as sys.argv[1], checks metadata of file acquiring field order, colour data etc
and updates DAR from 1.26 to 1.29.
3. Populates FFmpeg subprocess command based on format decision from retrieved data
4. Transcodes new file into QNAP_08 path with inherited source name
5. Runs framemd5 checks against the FFV1 matroska and duplicate, checks if they're identical
If identical:
i. verifies new MKV passes mediaconch policy
ii. If yes, deletes source MKV file and updates log with success
If no, deletes duplicate MKV file and updates log with mediaconch failure
If not identical:
i. Duplicate is deleted from new QNAP08 path and log is updated with framemd5 mismatch data
ii. Source file moved to 'problem' subfolder for human intervention
Python 3.7+
2022
'''
import os
import sys
import json
import time
import shutil
import logging
import datetime
import subprocess
# Global paths from environment vars
SOURCE = os.environ['BLUEFISH_MKV']
COMPLETED = os.path.join(SOURCE, 'completed')
MKV_POLICY = os.environ['MKV_POLICY']
DEST = os.environ['MKV_PROCESSING']
TBC_LOG = os.environ['TBC_LOG']
LOG = os.environ['SCRIPT_LOG']
FRAMEMD5_PATH = os.environ['BLUEFISH_TEMP']
CONTROL_JSON = os.path.join(LOG, 'downtime_control.json')
# Setup logging
logger = logging.getLogger('QNAP_08_bluefish_ffv1_tbc_fix.py')
hdlr = logging.FileHandler(os.path.join(LOG, 'QNAP_08_bluefish_ffv1_tbc_fix.log'))
formatter = logging.Formatter('%(asctime)s\t%(levelname)s\t%(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.INFO)
def check_control():
'''
Check control json for downtime requests
'''
with open(CONTROL_JSON) as control:
j = json.load(control)
if not j['ofcom_transcode']:
logger.info('Script run prevented by downtime_control.json. Script exiting.')
sys.exit('Script run prevented by downtime_control.json. Script exiting.')
def get_colour(fullpath):
'''
Retrieves colour information via mediainfo and returns in correct FFmpeg format
'''
mediainfo_cmd1 = [
'mediainfo',
'--Language=raw',
'--Output=Video;%colour_primaries%',
fullpath
]
colour_prim = subprocess.check_output(mediainfo_cmd1)
colour_prim = str(colour_prim)
mediainfo_cmd3 = [
'mediainfo',
'--Language=raw',
'--Output=Video;%matrix_coefficients%',
fullpath
]
col_matrix = subprocess.check_output(mediainfo_cmd3)
col_matrix = str(col_matrix)
if 'BT.709' in colour_prim:
color_primaries = 'bt709'
elif 'BT.601' in colour_prim and 'NTSC' in colour_prim:
color_primaries = 'smpte170m'
elif 'BT.601' in colour_prim and 'PAL' in colour_prim:
color_primaries = 'bt470bg'
else:
color_primaries = ''
if 'BT.709' in col_matrix:
colormatrix = 'bt709'
elif 'BT.601' in col_matrix:
colormatrix = 'smpte170m'
elif 'BT.470' in col_matrix:
colormatrix = 'bt470bg'
else:
colormatrix = ''
return (color_primaries, colormatrix)
def get_interl(fullpath):
'''
Retrieves interlacing data and returns in correct FFmpeg format
'''
mediainfo_cmd = [
'mediainfo',
'--Language=raw',
'--Output=Video;%ScanOrder%',
fullpath
]
interl_setting = subprocess.check_output(mediainfo_cmd)
interl_setting = str(interl_setting)
if 'TFF' in interl_setting:
setfield = 'tff'
elif 'BFF' in interl_setting:
setfield = 'bff'
elif 'PROG' in interl_setting:
setfield = 'prog'
else:
setfield = ''
return setfield
def get_fps(fullpath):
'''
Retrieve fps for source file
'''
mediainfo_cmd = [
'mediainfo',
'--Language=raw',
'--Output=Video;%FrameRate%',
fullpath
]
fps = subprocess.check_output(mediainfo_cmd)
fps = fps.decode('utf-8')
if '.' in fps:
fps = fps.split('.')[0]
return fps
def get_dar(fullpath):
'''
Retrieves metadata DAR info and returns as string
'''
cmd = [
'mediainfo',
'--Language=raw', '--Full',
'--Inform="Video;%DisplayAspectRatio%"',
fullpath
]
cmd[3] = cmd[3].replace('"', '')
dar_setting = subprocess.check_output(cmd)
dar_setting = dar_setting.decode('utf-8')
dar = str(dar_setting).rstrip('\n')
return dar
def adjust_dar_metadata(filepath):
'''
Use MKVToolNix MKVPropEdit to
adjust the metadata for PAR output
check output correct
'''
dar = get_dar(filepath)
cmd = [
'mkvpropedit', filepath,
'--edit', 'track:v1',
'--set', 'display-width=295',
'--set', 'display-height=228'
]
confirmed = subprocess.run(cmd, shell=False, check=True, universal_newlines=True, stdout=subprocess.PIPE, text=True)
confirmed = str(confirmed.stdout)
print(confirmed)
if 'The changes are written to the file.' not in str(confirmed):
print(f"DAR conversion failed: {confirmed}")
return False
new_dar = get_dar(filepath)
if '1.29' in new_dar:
print(f"DAR converted from {dar} to {new_dar}")
return True
def create_ffmpeg_command(fullpath, outpath, data=None):
'''
Subprocess command build, with variations
added based on metadata extraction
'''
if data is None:
data = []
# Build subprocess call from data list
ffmpeg_program_call = [
"ffmpeg"
]
input_video_file = [
"-i", fullpath,
"-nostdin"
]
map_command = [
"-map", "0"
]
video_settings = [
"-c:v", "ffv1",
"-level", "3",
"-g", "1",
"-slicecrc", "1",
"-slices", "24"
]
colour_build = [
"-color_primaries", f"{data[4]}",
"-color_trc", f"{data[3]}",
"-colorspace", f"{data[2]}"
]
interlace = [
"-vf", f"setfield={data[5]},fps=fps={data[1]}"
]
audio_settings = [
"-c:a", "copy"
]
out_settings = [
"-n", outpath
]
return ffmpeg_program_call + input_video_file + map_command + video_settings + colour_build + \
interlace + audio_settings + out_settings
def conformance_check(filepath):
'''
Checks mediaconch policy against new MKV file
'''
mediaconch_cmd = [
'mediaconch', '--force',
'-p', MKV_POLICY,
filepath
]
try:
success = subprocess.check_output(mediaconch_cmd)
success = success.decode('utf-8')
except Exception:
success = ""
print(f"Mediaconch policy retrieval failure for {filepath}")
if success.startswith('N/A'):
return f"FAIL! {success}"
elif success.startswith('pass!'):
return "PASS!"
elif success.startswith('fail!'):
return f"FAIL! {success}"
else:
return f"FAIL! {success}"
def make_framemd5(mkv_path1, mkv_path2):
'''
Creates MKV and MOV framemd5 and returns path locations for generated files
Uses lutyuv trim due to non-compliant yuv data capture at source (fault of capture cards)
This losslessly passed to matroska, but in transcoding back to V210 mov yuv regions
0-4 and 1019-1023 become lossy failing framemd5 comparison. lutyuv command courtesy Dave Rice.
UPDATE FOR BLUEFISH MKV (WITH AUDIO COMPARISONS, MAY NEED UPDATING)
'''
filename = os.path.split(mkv_path1)[1]
output_mkv1 = os.path.join(FRAMEMD5_PATH, f"{filename}.bluefish.mkv.framemd5")
output_mkv2 = os.path.join(FRAMEMD5_PATH, f"{filename}.corrected.mkv.framemd5")
framemd5_mkv = [
"ffmpeg", "-nostdin", "-y",
"-i", mkv_path1, "-an",
"-vf", "lutyuv=y=if(gt(val\,1019)\,1019\,if(lt(val\,4)\,4\,val)):u=if(gt(val\,1019)\,1019\,if(lt(val\,4)\,4\,val)):v=if(gt(val\,1019)\,1019\,if(lt(val\,4)\,4\,val))",
"-f", "framemd5",
output_mkv1
]
try:
subprocess.call(framemd5_mkv)
except Exception:
logger.exception("Framemd5 command failure: %s", mkv_path1)
framemd5_mkv2 = [
"ffmpeg", "-nostdin", "-y",
"-i", mkv_path2, "-an",
"-vf", "lutyuv=y=if(gt(val\,1019)\,1019\,if(lt(val\,4)\,4\,val)):u=if(gt(val\,1019)\,1019\,if(lt(val\,4)\,4\,val)):v=if(gt(val\,1019)\,1019\,if(lt(val\,4)\,4\,val))",
"-f", "framemd5",
output_mkv2
]
try:
subprocess.call(framemd5_mkv2)
except Exception:
logger.exception("Framemd5 command failure: %s", mkv_path2)
return (output_mkv1, output_mkv2)
def diff_check(md5_mkv1, md5_mkv2):
'''
Opens and compares two MD4 paths with trim applied to
prevent differing TBN data upsetting results
'''
mkv1_manifest = []
mkv2_manifest = []
with open(md5_mkv1, 'r') as file1:
mkv1 = file1.read().splitlines()
for line in mkv1[8:]:
mkv1_manifest.append(line.split(',')[-1])
with open(md5_mkv2, 'r') as file2:
mkv2 = file2.read().splitlines()
for line in mkv2[8:]:
mkv2_manifest.append(line.split(',')[-1])
if len(mkv1_manifest) == len(mkv2_manifest):
length_check = len(mkv1_manifest) - 3
else:
if len(mkv1_manifest) < len(mkv2_manifest):
length_check = len(mkv1_manifest) - 3
else:
length_check = len(mkv2_manifest) - 3
difference = set(mkv1_manifest[:length_check]).difference(set(mkv2_manifest[:length_check]))
if not difference:
return 'MATCH'
else:
print(f"Files do not match - mismatches:\n{difference}")
return 'FAIL'
def fail_log(fullpath, message):
'''
Creates fail log if not in existence
Appends new message to log
'''
message = str(message)
dt = datetime.datetime.now()
dtime = dt.strftime("%Y-%m-%d %H:%M:%S")
if not os.path.isfile(TBC_LOG):
with open(TBC_LOG, 'x') as log_data:
log_data.close()
with open(TBC_LOG, 'a+') as log_data:
log_data.write(f"==== {fullpath}: {dtime} \n")
log_data.write(message)
log_data.write("\n")
log_data.close()
def main():
'''
Receives sys.argv[1] path to FFV1 mkv from shell start script via GNU parallel
Extracts metadata of file, passes to FFmpeg subprocess command, transcodes V210
Makes framemd5 comparison, and passes V210 mov through mediaconch policy
If all pass, cleans up files moving to success folder and deletes FFV1 mkv.
'''
logger_list = []
if len(sys.argv) < 2:
logger.warning("SCRIPT EXITING: Error with shell script input:\n %s", sys.argv)
sys.exit(f'Error with shell script input {sys.argv}')
else:
logger.info("================== START BlueFish MKV TBC correction START ==================")
# check_control()
fullpath = sys.argv[1]
file = os.path.split(fullpath)[1]
outpath = os.path.join(DEST, file)
if os.path.exists(fullpath):
# Build and execute FFmpeg subprocess call
logger_list.append(f"******** {fullpath} being processed ********")
ffmpeg_data = []
# Update CID with DAR warning
dar = get_dar(fullpath)
print(f"************ {dar} ************")
if '1.26' in dar:
logger_list.append(f'{file}\tFile has 1.26 DAR. Converting to 1.29 DAR')
logger_list.append(f'{file}\tFile found with 1.26 DAR. Converting to 1.29 DAR')
confirmed = adjust_dar_metadata(fullpath)
if not confirmed:
logger_list.append(f'WARNING: {file}\tCould not adjust DAR metadata.')
else:
logger_list.append(f'{file}\tFile DAR header metadata changed to 1.29')
# Extract MKV metadata to list and pass to subprocess blocks
setfield = get_interl(fullpath)
colour_data = get_colour(fullpath)
color_primaries = colour_data[0]
color_trc = 'bt709'
colormatrix = colour_data[1]
fps = get_fps(fullpath)
codec = 'ffv1'
ffmpeg_data = [codec, fps, colormatrix, color_trc, color_primaries, setfield]
ffmpeg_call = create_ffmpeg_command(fullpath, outpath, ffmpeg_data)
ffmpeg_call_neat = (" ".join(ffmpeg_call), "\n")
logger_list.append(f"FFmpeg call: {ffmpeg_call_neat}")
tic = time.perf_counter()
try:
subprocess.call(ffmpeg_call)
except Exception:
logger_list.append(f"WARNING: FFmpeg command failed: {ffmpeg_call}")
toc = time.perf_counter()
encode_time = (toc - tic) // 60
seconds_time = (toc - tic)
logger_list.append(f"*** Encoding time for {file} was {encode_time} minutes // or in seconds {seconds_time}")
# Check framemd5's match for MKV and MOV
tic2 = time.perf_counter()
md5_mkv1, md5_mkv2 = make_framemd5(fullpath, outpath)
toc2 = time.perf_counter()
md5_time = (toc2 - tic2) // 60
md5_seconds = (toc2 - tic2)
logger_list.append(f"*** MD5 creation time for files: {md5_time} minutes or {md5_seconds} seconds")
result = diff_check(md5_mkv1, md5_mkv2)
if 'MATCH' in result:
logger_list.append("Framemd5 check passed for source and copy MKV files")
# Run conformance check
result = conformance_check(outpath)
if "PASS!" in result:
logger_list.append(f"PASS! {outpath} passed the policy checker and it's Matroska can be deleted")
try:
# Delete FFV1 mkv after successful transcode to MKV
shutil.move(fullpath, COMPLETED)
fname = os.path.split(fullpath)[-1]
completed_pth = os.path.join(COMPLETED, fname)
logger_list.append("*** FILE BEING MOVED TO COMPLETED PATH FOR AUTOMATED DELETION: %s", fname)
except Exception:
logger_list.append(f"WARNING: Deletion failure: {fullpath}")
else:
logger_list.append(f"WARNING: {outpath} failed the policy checker. Leaving Matroska for second encoding attempt")
fail_log(fullpath, f"Failed Mediaconch conformance check:\n{result}")
try:
logger_list.append(f"PAUSED -- Deleting {outpath} file as failed mediaconch policy")
os.remove(outpath)
except Exception:
logger_list.append(f"WARNING: Unable to delete {outpath}")
# Collate and output all logs at once for concurrent runs
for line in logger_list:
if 'WARNING' in str(line):
logger.warning("%s", line)
else:
logger.info("%s", line)
else:
logger_list.append(f"--- {outpath} ---")
fail_log(fullpath, "Failed framemd5 manifests, appending 'failed_' for review.")
fail_log(fullpath, f"Deleting: {outpath}")
logger_list.append("FRAMEMD5 FILES DO NOT MATCH")
md5_mkv1_split = os.path.split(md5_mkv1)
rename_md5_mkv1 = os.path.join(FRAMEMD5_PATH, f'failed_{md5_mkv1_split[1]}')
md5_mkv2_split = os.path.split(md5_mkv2)
rename_md5_mkv2 = os.path.join(FRAMEMD5_PATH, f'failed_{md5_mkv2_split[1]}')
# Move framemd5 files from qnap02 to qnap04 (new block)
logger_list.append(f"MOVING: {md5_mkv2} TO {rename_md5_mkv2}")
os.rename(md5_mkv2, rename_md5_mkv2)
os.rename(md5_mkv1, rename_md5_mkv1)
try:
logger_list.append(f"Deleting {outpath} file as failed transcoding checks")
os.remove(outpath)
except Exception:
logger_list.append(f"WARNING: Unable to delete {outpath}")
# Collate and output all logs at once for concurrent runs
for line in logger_list:
if 'WARNING' in str(line):
logger.warning("%s", line)
else:
logger.info("%s", line)
else:
logger.info("SKIPPING: Filename doesn't exist: %s", fullpath)
logger.info("================== END BlueFish MKV TBC correction END =============\n")
if __name__ == "__main__":
main()