-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmakeMetadata.py
More file actions
485 lines (456 loc) · 13 KB
/
makeMetadata.py
File metadata and controls
485 lines (456 loc) · 13 KB
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
#!/usr/bin/env python3
'''
these are functions to output metadata files
and structured data (xml/json) about a/v files
'''
import argparse
import ast
import configparser
import datetime
import hashlib
import json
import os
import subprocess
import sys
# nonstandard libraries:
# import xmltodict
# local modules:
try:
import pymmFunctions
except:
from . import pymmFunctions
def get_mediainfo_report(inputPath,destination,_JSON=None,altFileName=None):
# handle an exception for the way
# DPX folders are named in processingVars
if altFileName:
basename = altFileName
else:
basename = pymmFunctions.get_base(inputPath)
# write mediainfo output to a logfile if the destination is a directory ..
if os.path.isdir(destination):
if _JSON:
outputType = "JSON"
else:
outputType = "XML"
outputFilepath = '{}_mediainfo.xml'.format(
os.path.join(destination,basename)
)
mediainfoOutput = '--LogFile={}'.format(outputFilepath)
out = subprocess.run(
['mediainfo',
inputPath,
'--Output={}'.format(outputType),
mediainfoOutput],
stdout=subprocess.PIPE
)
mediainfoJSON = out.stdout.decode('utf-8')
if _JSON:
return mediainfoJSON
else:
return outputFilepath
# ... otherwise pass something like '' as a destination
# and just get the raw mediainfo output
else:
out = subprocess.run(
['mediainfo','--Output=JSON',inputPath],
stdout=subprocess.PIPE
)
mediainfoJSON = out.stdout.decode('utf-8')
# print(mediainfoJSON)
if _JSON:
return mediainfoJSON
else:
print("{} doesn't exist and you didn't say you "
"want the raw mediainfo output.\n"
"What do you want??".format(destination))
return False
def get_mediainfo_pbcore(inputPath):
call = subprocess.Popen(
['mediainfo','--Output=PBCore2',inputPath],
stdout=subprocess.PIPE
)
pbcore = call.communicate()[0]
return pbcore
def get_track_profiles(mediainfoDict):
'''
Get audio and video track profiles to compare for concatenation of files.
Takes an OrderedDict as retrned by get_mediainfo_report.
Define relevant attributes taken from MediaInfo
that we want to compare between files. Are these lists accurate? @fixme
'''
problems = 0
if isinstance(mediainfoDict,str):
mediainfoDict = ast.literal_eval(mediainfoDict)
videoAttribsToKeep = [
'Format','Width','Height','PixelAspectRatio','DisplayAspectRatio',
'FrameRate','Standard','ColorSpace','ChromaSubsampling',
'BitDepth','ScanType','CodecID'
]
audioAttribsToKeep = [
'Format','CodecID','SamplingRate','SamplesPerFrame','BitDepth'
]
# `tracks` should be a list of track dicts
tracks = mediainfoDict['media']['track']
# print(tracks)
videoTrackProfile = None
audioTrackProfile = None
for track in tracks:
# print(track)
if track['@type'] == 'Video':
videoTrackProfile = track
elif track['@type'] == 'Audio':
audioTrackProfile = track
if videoTrackProfile:
# print(videoTrackProfile)
temp = {}
for attr in videoAttribsToKeep:
# videoTrackProfile.pop(attr,None)
if attr in videoTrackProfile:
temp[attr] = videoTrackProfile[attr]
videoTrackProfile = temp
del temp
# print(videoTrackProfile)
else:
problems += 1
print("mediainfo problem: "
"either there is no video track or you got some issues")
if audioTrackProfile:
# print(audioTrackProfile)
temp = {}
for attr in audioAttribsToKeep:
if attr in audioTrackProfile:
temp[attr] = audioTrackProfile[attr]
audioTrackProfile = temp
del temp
# audioTrackProfile.pop(attr,None)
# print(audioTrackProfile)
else:
problems += 1
print("mediainfo problem: "
"either there is no audio track or you got some issues")
if problems == 0:
return json.dumps(videoTrackProfile),json.dumps(audioTrackProfile)
else:
print("there might be problems")
if videoTrackProfile:
return json.dumps(videoTrackProfile),"{}"
elif audioTrackProfile:
return "{}",json.dumps(audioTrackProfile)
else:
return "{}","{}"
def hash_file(inputPath,algorithm='md5',blocksize=65536):
# STOLEN DIRECTLY FROM UCSB BRENDAN COATES: https://github.com/brnco/ucsb-src-microservices/blob/master/hashmove.py
hasher = hashlib.new(algorithm)
with open(inputPath,'rb') as infile:
buff = infile.read(blocksize) # read the file into a buffer cause it's more efficient for big files
while len(buff) > 0: # little loop to keep reading
hasher.update(buff) # here's where the hash is actually generated
buff = infile.read(blocksize) # keep reading
return hasher.hexdigest()
def manifest_path(metadataPath,_uuid,_type):
manifestPath = os.path.join(
metadataPath,
'{}_manifest_{}_{}.txt'.format(
_type,
_uuid,
pymmFunctions.timestamp('8601-filename')
)
)
return manifestPath
def make_hashdeep_manifest(targetDirPath,_uuid,_type):
'''
given a target directory, make a hashdeep manifest.
chdir into target dir, make a manifest with relative paths, and get out.
For the SIP manifest, this currently relies on a bagit-style tree
to contain both the manifest and the package.
proposal: also store the manifest as a blob
(or as text?) in a db entry... yeah.
'''
# set a var for reporting on the SIP structure later
structure = True
if _type == 'hashdeep':
# this is for making a SIP-level manifest for validation
# we want to write the manifest in the top level SIP folder
metadataPath = targetDirPath
# the hashdeep target is the child also named w the UUID
targetDirPath = os.path.join(
targetDirPath,
os.path.basename(targetDirPath)
)
if not os.path.isdir(targetDirPath):
structure = False
elif _type == 'objects':
# targetDirPath should be the SIP objects directory
# we want to write the manifest to the metadata dir
metadataPath = os.path.join(
os.path.dirname(targetDirPath),
'metadata'
)
for path in (targetDirPath,metadataPath):
if not os.path.isdir(path):
structure = False
if structure == False:
print("the expected directory structure is not present.") # @logme
return False
manifestPath = manifest_path(metadataPath,_uuid,_type)
# run hashdeep on the package
command = ['hashdeep', '-rvvl', '-c','md5','-W', manifestPath, '.']
# print(command)
here = os.getcwd()
os.chdir(targetDirPath)
manifest = subprocess.call(command,stdout=subprocess.PIPE)
os.chdir(here)
return manifestPath
def hashdeep_audit(inputPath,manifestPath,_type=None):
'''
Given a target directory and an existing manifest, run a hashdeep audit.
-> chdir into the target, audit the relative paths, and get out.
Updated version creates a bagit-style tree that contains the package,
along with the existing manifest.
same idea as above: read manifest from blob in db and write the audit file
as a new blob.
'''
_uuid = os.path.basename(inputPath)
package = os.path.join(inputPath,_uuid)
if _type == 'SIP':
target = package
elif _type == 'objects':
target = os.path.join(package,'objects')
# turn off multithreading for auditing on LTO!
command = ['hashdeep','-rvval','-j0','-k',manifestPath,'.']
# print(command)
# print(target)
here = os.getcwd()
os.chdir(target)
try:
hashaudit = subprocess.run(command,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
# print(hashaudit)
out = hashaudit.stdout.splitlines()
result = ""
error = False
for line in out:
if line.decode().startswith("hashdeep: Audit"):
outcome = line.decode()
print(outcome)
if outcome == 'hashdeep: Audit failed':
status = False
elif outcome == 'hashdeep: Audit passed':
status = True
else:
status = False
error = True
print("INCONCLUSIVE AUDIT. SIP NOT VERIFIED.")
result = [out,hashaudit.stderr.decode()]
# gather the results for logging
if not error:
for line in out:
result += line.decode()+"\n"
except:
print(
"there was a problem with the hashdeep audit. "
"package NOT verified."
)
status = False
result = "hashdeep error"
os.chdir(here)
return result,status
def make_frame_md5(inputPath,metadataDir):
print('making frame md5')
print(inputPath)
md5File = os.path.basename(inputPath)+"_frame-md5.txt"
frameMd5Filepath = os.path.join(metadataDir,md5File)
av = pymmFunctions.is_av(inputPath)
returnValue = False
if not av:
# FUN FACT: YOU CAN RUN FFMPEG FRAMEMD5 ON A TEXT FILE!!
print("{} IS NOT AN AV FILE SO "
"WHY ARE YOU TRYING TO MAKE "
"A FRAME MD5 REPORT?".format(inputPath))
elif av == 'VIDEO':
frameMd5Command = [
'ffmpeg',
'-i',inputPath,
'-f','framemd5',
frameMd5Filepath
]
output = subprocess.Popen(
frameMd5Command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
try:
out,err = output.communicate()
if err:
# this output is captured in stderr for some reason
print("FRAME MD5 CHA CHA CHA")
# print(err.decode('utf-8'))
returnValue = frameMd5Filepath
except:
print(out.decode())
elif av == 'AUDIO':
sampleRate = pymmFunctions.get_audio_sample_rate(inputPath)
frameMd5Command = [
'ffmpeg',
'-i',inputPath,
'-af','asetnsamples=n={}'.format(sampleRate),
'-f','framemd5',
'-vn',
frameMd5Filepath
]
output = subprocess.run(frameMd5Command,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
# print(output.returncode)
try:
if output.returncode == 0:
# print(output)
print("FRAME MD5 CHA CHA CHA")
returnValue = frameMd5Filepath
except:
print(output.stderr.decode())
elif av == 'DPX':
pass
'''
OK: FOOD FOR THOUGHT:
IT TAKES EFFING FOREVER TO CALCULATE FRAMEMD5 VALUES FOR A DPX
SEQUENCE. SLIGHTLY LONGER THAN THE HASHDEEP MANIFEST THAT WILL
BE CREATED LATER. SO... SKIP FRAMEMD5 FOR DPX? SINCE WE ARE ALREADY
CALCULATING A HASH MANIFEST LATER ON?
MAYBE LATER GET A FUNCTION TO PARSE A HASH MANIFEST FOR THE FOLDER AND
TURN IT INTO A
ACTUALLY ON THE SOUPED UP LINUX SERVER THIS IS REALLY FAST.
SO MAYBE RUN A BENCH MARK AND IF THE SYSTEM CAN HANDLE IT RUN THIS FUNCTION
'''
# filePattern,startNumber,file0 = pymmFunctions.parse_sequence_folder(inputPath)
# frameMd5Command = [
# 'ffmpeg',
# '-start_number',startNumber,
# '-i',filePattern,
# '-f','framemd5',
# frameMd5Filepath
# ]
# print(' '.join(frameMd5Command))
# output = subprocess.Popen(
# frameMd5Command,
# stdout=subprocess.PIPE,
# stderr=subprocess.PIPE
# )
# try:
# out,err = output.communicate()
# if err:
# # this output is captured in stderr for some reason
# print("FRAME MD5 CHA CHA CHA")
# # print(err.decode('utf-8'))
# returnValue = frameMd5Filepath
# except:
# print(out.decode())
return returnValue
def get_duration(inputPath):
print('getting input file duration via general track 0')
command = [
'mediainfo',
'--output=JSON',
inputPath
]
mediainfoJSON = subprocess.run(
command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
out = mediainfoJSON.stdout
fileJson = json.loads(out.decode())
try:
duration = fileJson['media']['track'][0]['Duration']
except:
print("Error getting duration via mediainfo.")
return duration
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
'-i','--inputPath',
help='path of input file',
required=True
)
parser.add_argument(
'-m','--mediainfo',
action='store_true',
help='generate a mediainfo sidecar file'
)
parser.add_argument(
'-f','--frame_md5',
action='store_true',
help='make frame md5 report'
)
parser.add_argument(
'-p','--pbcore',
action='store_true',
help='make mediainfo pbcore report'
)
parser.add_argument(
'-j','--getJSON',
action='store_true',
help='get JSON output as applicable'
)
parser.add_argument(
'-d','--destination',
help='set destination for output metadata files'
)
parser.add_argument(
'-v','--getValue',
help='declare a valid MediaInfo raw tag to get it.'\
'REQUIRES you to declare a stream type!'
)
parser.add_argument(
'-t','--valueType',
help='declare a valid stream type from which to grab a value.',
choices=['General','Audio','Video']
)
args = parser.parse_args()
inputPath = args.inputPath
destination = args.destination
frame_md5 = args.frame_md5
_pbcore = args.pbcore
mediainfo_report = args.mediainfo
getJSON = args.getJSON
value = args.getValue
valueType = args.valueType
if not inputPath:
print("\n\nHEY THERE, YOU NEED TO SET AN INPUT FILE "
"TO RUN THIS SCRIPT ON.\rNOW EXITING")
sys.exit()
if not destination:
# print('''
# YOU DIDN'T TELL ME WHERE TO PUT THE OUTPUT OF THIS SCRIPT,
# SO WE'LL PUT ANY SIDECAR FILES IN THE
# SAME DIRECTORY AS YOUR INPUT FILE.
# ''')
destination = os.path.dirname(os.path.abspath(inputPath))
if mediainfo_report:
get_mediainfo_report(inputPath,destination,getJSON)
if frame_md5:
frameMd5Filepath = make_frame_md5(inputPath,destination)
# print(frameMd5Filepath)
if _pbcore:
xml = get_mediainfo_pbcore(inputPath)
with open(
os.path.join(
destination,
os.path.basename(inputPath)+"_pbcore.xml"
),
'wb'
) as xmlFile:
xmlFile.write(xml)
if value:
if not valueType:
print("YOU NEED TO DECLARE A STREAM TYPE:"\
"Audio, General, or Video"
)
sys.exit()
else:
theValue = pymmFunctions.get_mediainfo_value(
inputPath,
valueType,
value
)
print(theValue)
return theValue
if __name__ == '__main__':
main()