forked from kieranjol/IFIscripts
/
makepbcore.py
executable file
·566 lines (549 loc) · 18.7 KB
/
makepbcore.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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
#!/usr/bin/env python
'''
Describe AV objects using PBCore in CSV form.
Ideas for improvement:
Have the PBCore get metadata function return the value, and
just append it to the element list in the one call.
Also, have a consistent method for extracting metadata,
I'd suggest just parsing a single raw -f mediainfo, which should
alleviate the need to use --Inform and the PBCore output.
Also - better handling of multiple audio tracks.
The audio_format and the PBCore calls are all just analyzing the first track.
Get rid of this technical debt!
'''
import sys
import os
import subprocess
import argparse
from lxml import etree
import ififuncs
def get_metadata(xpath_path, root, pbcore_namespace):
'''
Extracts values from PBCore2 XML MediaInfo outputs.
'''
value = root.xpath(
xpath_path,
namespaces={'ns':pbcore_namespace}
)
if value == []:
value = 'n/a'
elif len(value) > 1:
mixed_values = ''
value_list = []
for i in value:
# Checks if multiple audio tracks have different values.
if i.getparent().find(
'ns:essenceTrackType',
namespaces={'ns':pbcore_namespace}
).text == 'Audio':
value_list.append(i.text)
# Checks if values in the list are the same(1) or different (2)
if len(set(value_list)) is 1:
value = value[0].text
else:
# Return the mixed values with pipe delimiter.
for x in value_list:
mixed_values += x + '|'
if mixed_values[-1] == '|':
mixed_values = mixed_values[:-1]
value = mixed_values
else:
value = value[0].text
return value
def get_attributes(root, pbcore_namespace):
'''
Extracts values from PBCore2 XML MediaInfo outputs.
'''
value = root.xpath(
"ns:essenceTrackEncoding",
namespaces={'ns':pbcore_namespace}
)[0].attrib
return value # a dict
def parse_args(args_):
'''
Parse command line arguments.
'''
parser = argparse.ArgumentParser(
description='Describes AV objects using a combination of the PBCore 2 metadata standard and the mandatory fields from the IFI technical database.'
'This script takes a folder as input. Either a single file or multiple objects will be described.'
'This will produce a single PBCore CSV record per package, even if multiple objects are within a package.'
' Written by Kieran O\'Leary.'
)
parser.add_argument(
'input', help='Input directory'
)
parser.add_argument(
'-user',
help='Declare who you are. If this is not set, you will be prompted.'
)
parser.add_argument(
'-accession',
help='Enter the Accession number for the representation.'
)
parser.add_argument(
'-reference',
help='Enter the Filmographic reference number for the representation.'
)
parser.add_argument(
'-p', action='store_true',
help='Adds the PBCore CSV to the metadata folder'
)
parsed_args = parser.parse_args(args_)
return parsed_args
def get_accession_number(source):
'''
Checks if the package has been accessioned.
Returns the accession number if this is the case, otherwise the script exits.
'''
basename = os.path.basename(source)
if len(basename) == 7:
if basename[:3] == 'aaa':
return basename
else:
print('looks like your package has not been accessioned? Exiting!')
sys.exit()
def get_reference_number(source):
'''
Checks if the reference number is in the folder path.
This check is not sustainable, will have to be made more flexible!
'''
basename = os.path.basename(os.path.dirname(source))
if len(basename) == 7:
if basename[:3] == 'af1':
return basename
else:
basename = ififuncs.get_reference_number()
return basename
def make_csv(csv_filename):
'''
Writes a CSV with IFI database headings.
'''
ififuncs.create_csv(csv_filename, [
'Reference Number',
'Donor',
'Edited By',
'Date Created',
'Date Last Modified',
'Film Or Tape',
'Date Of Donation',
'Accession Number',
'Habitat',
'Type Of Deposit',
'Depositor Reference',
'Master Viewing',
'Language Version',
'Condition Rating',
'Companion Elements',
'EditedNew',
'FIO',
'CollectionTitle',
'Created By',
'instantiationIdentif',
'instantiationDate_modified',
'instantiationDimensi',
'instantiationStandar',
'instantiationLocatio',
'instantMediaty',
'instantFileSize',
'instantFileSize_gigs',
'instantTimeStart',
'instantDataRate',
'instantTracks',
'instantColors',
'instantLanguage',
'instantAltMo',
'essenceTrackEncodvid',
'essenceFrameRate',
'essenceTrackSampling',
'essenceBitDepth_vid',
'essenceFrameSize',
'essenceAspectRatio',
'essenceTrackEncod_au',
'essenceBitDepth_au',
'instantiationDuratio',
'instantiationChanCon',
'PixelAspectRatio',
'FrameCount',
'ColorSpace',
'ChromaSubsampling',
'ScanType',
'Interlacement',
'Compression_Mode',
'colour_primaries',
'transfer_characteris',
'matrix_coefficients',
'pix_fmt',
'audio_fmt',
'audio_codecid',
'video_codecid',
'video_codec_version',
'video_codec_profile'
])
def main(args_):
# if multiple file are present, this script will treat them as a single
# instantiation/representation and get aggregate metadata about the whole
# package. For now, this will be a clumsy implementation - the first file
# will provide most metadata. Things like duration/bitrate/filesize
# will be calculated as a whole.
# Although another way would be that every call is looped, and if
# this could catch files that should not be in the package, eg. a 4:2:2
# file in a 4:2:0 package..
# yup - do it that way!
args = parse_args(args_)
all_files = ififuncs.recursive_file_list(args.input)
silence = True
if args.user:
user = args.user
else:
user = ififuncs.get_user()
for dirs in os.listdir(args.input):
if ififuncs.validate_uuid4(dirs) is None:
instantiationIdentif = dirs
Accession_Number = get_accession_number(args.input)
if args.reference:
Reference_Number = args.reference.upper()
else:
Reference_Number = get_reference_number(args.input)
if args.p:
for root, _, filenames in os.walk(args.input):
if os.path.basename(root) == 'metadata':
metadata_dir = root
elif os.path.basename(root) == 'logs':
logs_dir = root
csv_filename = os.path.join(metadata_dir, Accession_Number + '_pbcore.csv')
sipcreator_log = os.path.join(
logs_dir, instantiationIdentif + '_sip_log.log'
)
ififuncs.generate_log(
sipcreator_log,
'EVENT = makepbcore.py started'
)
ififuncs.generate_log(
sipcreator_log,
'eventDetail=makepbcore.py %s' % ififuncs.get_script_version('makepbcore.py')
)
ififuncs.generate_log(
sipcreator_log,
'Command line arguments: %s' % args
)
ififuncs.generate_log(
sipcreator_log,
'EVENT = agentName=%s' % user
)
else:
csv_filename = 'blaa.csv'
print ' - Metadata will be stored in %s' % csv_filename
for filenames in os.listdir(args.input):
if '_manifest.md5' in filenames:
md5_manifest = os.path.join(args.input, filenames)
elif'manifest-sha512.txt' in filenames:
sha512_manifest = os.path.join(args.input, filenames)
make_csv(csv_filename)
ms = 0
FrameCount = 0
instantFileSize = 0
instantFileSize_gigs = 0
scan_types = []
matrix_list = []
transfer_list = []
colour_primaries_list = []
color_spaces = []
chroma = []
frame_sizes = []
par_list = []
container_list = []
fps_list = []
sample_rate_list = []
track_count_list = []
interlace_list = []
compression_list = []
pix_fmt_list = []
audio_fmt_list = []
audio_codecid_list = []
audio_codec_list = []
au_bitdepth_list = []
video_codecid_list = []
video_codec_version_list = []
video_codec_profile_list = []
for source in all_files:
metadata = subprocess.check_output(['mediainfo', '--Output=PBCore2', source])
root = etree.fromstring(metadata)
print(' - Analysing %s') % source
pbcore_namespace = root.xpath('namespace-uri(.)')
track_type = root.xpath('//ns:essenceTrackType', namespaces={'ns':pbcore_namespace})
if len(track_type) > 0:
for track in track_type:
if track.text == 'Video':
essenceTrackEncodvid = get_metadata(
"ns:essenceTrackEncoding",
track.getparent(), pbcore_namespace
)
vcodec_attributes = get_attributes(track.getparent(), pbcore_namespace)
elif track.text == 'Audio':
silence = False
essenceTrackEncod_au = get_metadata(
"//ns:essenceTrackEncoding",
track.getparent(), pbcore_namespace
)
audio_codec_list.append(essenceTrackEncod_au)
acodec_attributes = get_attributes(track.getparent(), pbcore_namespace)
audio_codecid = acodec_attributes['ref']
essenceTrackSampling = ififuncs.get_mediainfo(
'samplerate',
'--inform=Audio;%SamplingRate_String%', source
)
essenceBitDepth_au = get_metadata(
"//ns:essenceTrackBitDepth",
root, pbcore_namespace
)
audio_codecid_list.append(audio_codecid)
au_bitdepth_list.append(essenceBitDepth_au)
ScanType = get_metadata(
"//ns:essenceTrackAnnotation[@annotationType='ScanType']",
root, pbcore_namespace
)
scan_types.append(ScanType)
matrix_coefficients = get_metadata(
"//ns:essenceTrackAnnotation[@annotationType='matrix_coefficients']",
root, pbcore_namespace
)
matrix_list.append(matrix_coefficients)
transfer_characteris = get_metadata(
"//ns:essenceTrackAnnotation[@annotationType='transfer_characteristics']",
root, pbcore_namespace
)
transfer_list.append(transfer_characteris)
colour_primaries = get_metadata(
"//ns:essenceTrackAnnotation[@annotationType='color_primaries']",
root, pbcore_namespace
)
colour_primaries_list.append(colour_primaries)
FrameCount += int(get_metadata(
"//ns:essenceTrackAnnotation[@annotationType='FrameCount']",
root, pbcore_namespace
))
instantFileSize += int(get_metadata(
"//ns:instantiationFileSize",
root, pbcore_namespace
))
instantDataRate = round(float(ififuncs.get_mediainfo(
'OverallBitRate', '--inform=General;%OverallBitRate%', source
)) / 1000 / 1000, 2)
instantTracks = ififuncs.get_number_of_tracks(source)
track_count_list.append(instantTracks)
ms += ififuncs.get_milliseconds(source)
ColorSpace = get_metadata(
"//ns:essenceTrackAnnotation[@annotationType='ColorSpace']",
root, pbcore_namespace
)
color_spaces.append(ColorSpace)
ChromaSubsampling = get_metadata(
"//ns:essenceTrackAnnotation[@annotationType='ChromaSubsampling']",
root, pbcore_namespace
)
chroma.append(ChromaSubsampling)
instantMediaty = get_metadata(
"//ns:instantiationMediaType",
root, pbcore_namespace
)
essenceFrameSize = get_metadata(
"//ns:essenceTrackFrameSize",
root, pbcore_namespace
)
frame_sizes.append(essenceFrameSize)
PixelAspectRatio = get_metadata(
"//ns:essenceTrackAnnotation[@annotationType='PixelAspectRatio']",
root, pbcore_namespace
)
par_list.append(PixelAspectRatio)
instantiationStandar = get_metadata(
"//ns:instantiationAnnotation[@annotationType='Format']",
root, pbcore_namespace
)
container_list.append(instantiationStandar)
essenceFrameRate = get_metadata(
"//ns:essenceTrackFrameRate",
root, pbcore_namespace
)
fps_list.append(essenceFrameRate)
essenceAspectRatio = ififuncs.get_mediainfo(
'DAR', '--inform=Video;%DisplayAspectRatio_String%', source
)
sample_rate_list.append(essenceTrackSampling)
Interlacement = get_metadata(
"//ns:instantiationAnnotation[@annotationType='Interlacement']",
root, pbcore_namespace
)
interlace_list.append(Interlacement)
Compression_Mode = get_metadata(
"//ns:instantiationAnnotation[@annotationType='Compression_Mode']",
root, pbcore_namespace
)
compression_list.append(Compression_Mode)
instantiationDate_modified = get_metadata(
"//ns:instantiationDate[@dateType='file modification']",
root, pbcore_namespace
)
pix_fmt = ififuncs.get_ffmpeg_fmt(source, 'video')
pix_fmt_list.append(pix_fmt)
audio_fmt = ififuncs.get_ffmpeg_fmt(source, 'audio')
audio_fmt_list.append(audio_fmt)
if silence:
audio_codecid = 'n/a'
essenceBitDepth_au = 'n/a'
essenceTrackEncod_au = 'n/a'
video_codecid = vcodec_attributes['ref']
video_codecid_list.append(video_codecid)
try:
video_codec_version = vcodec_attributes['version']
except KeyError:
video_codec_version = 'n/a'
try:
video_codec_profile = vcodec_attributes['annotation'][8:]
except KeyError:
video_codec_profile = 'n/a'
video_codec_version_list.append(video_codec_version)
video_codec_profile_list.append(video_codec_profile)
metadata_error = ''
metadata_list = [
scan_types,
matrix_list,
transfer_list,
colour_primaries_list,
color_spaces,
chroma,
frame_sizes,
par_list,
container_list,
fps_list,
sample_rate_list,
track_count_list,
interlace_list,
compression_list,
pix_fmt_list,
audio_fmt_list,
audio_codecid_list,
audio_codec_list,
au_bitdepth_list,
video_codecid_list,
video_codec_version_list,
video_codec_profile_list
]
for i in metadata_list:
if len(set(i)) > 1:
metadata_error += 'WARNING - Your metadata values are not the same for all files: %s\n' % set(i)
print metadata_error
if args.p:
ififuncs.generate_log(
sipcreator_log,
'EVENT = Metadata mismatch - Your metadata values are not the same for all files: %s' % set(i)
)
tc = ififuncs.convert_millis(ms)
instantiationDuratio = ififuncs.convert_timecode(25, tc)
Donor = ''
Edited_By = user
Date_Created = ''
Date_Last_Modified = ''
Film_Or_Tape = 'Digital File'
Date_Of_Donation = ''
Habitat = ''
Type_Of_Deposit = ''
Depositor_Reference = ''
Master_Viewing = 'Preservation Master'
Language_Version = ''
Condition_Rating = ''
Companion_Elements = ''
EditedNew = user
FIO = 'In'
CollectionTitle = ''
Created_By = user
instantiationDimensi = ''
instantiationLocatio = ''
instantTimeStart = ''
instantFileSize_gigs = round(
float(instantFileSize) / 1024 / 1024 / 1024, 3
)
instantColors = ''
instantLanguage = ''
instantAltMo = 'n/a'
essenceBitDepth_vid = ififuncs.get_mediainfo(
'duration', '--inform=Video;%BitDepth%', source
)
instantiationChanCon = ''
ififuncs.append_csv(csv_filename, [
Reference_Number,
Donor,
Edited_By,
Date_Created,
Date_Last_Modified,
Film_Or_Tape,
Date_Of_Donation,
Accession_Number,
Habitat,
Type_Of_Deposit,
Depositor_Reference,
Master_Viewing,
Language_Version,
Condition_Rating,
Companion_Elements,
EditedNew,
FIO,
CollectionTitle,
Created_By,
instantiationIdentif,
instantiationDate_modified,
instantiationDimensi,
instantiationStandar,
instantiationLocatio,
instantMediaty,
instantFileSize,
instantFileSize_gigs,
instantTimeStart,
instantDataRate,
instantTracks,
instantColors,
instantLanguage,
instantAltMo,
essenceTrackEncodvid,
essenceFrameRate,
essenceTrackSampling,
essenceBitDepth_vid,
essenceFrameSize,
essenceAspectRatio,
essenceTrackEncod_au,
essenceBitDepth_au,
instantiationDuratio,
instantiationChanCon,
PixelAspectRatio,
FrameCount,
ColorSpace,
ChromaSubsampling,
ScanType,
Interlacement,
Compression_Mode,
colour_primaries,
transfer_characteris,
matrix_coefficients,
pix_fmt,
audio_fmt,
audio_codecid,
video_codecid,
video_codec_version,
video_codec_profile
])
if args.p:
ififuncs.generate_log(
sipcreator_log,
'EVENT = Metadata extraction - eventDetail=Technical record creation using PBCore, eventOutcome=%s, agentName=makepbcore' % (csv_filename))
ififuncs.generate_log(
sipcreator_log,
'EVENT = makepbcore.py finished')
ififuncs.checksum_replace(md5_manifest, sipcreator_log, 'md5')
ififuncs.checksum_replace(sha512_manifest, sipcreator_log, 'sha512')
ififuncs.manifest_update(md5_manifest, csv_filename)
print ' - Updating %s with %s' % (md5_manifest, csv_filename)
ififuncs.sha512_update(sha512_manifest, csv_filename)
print ' - Updating %s with %s' % (sha512_manifest, csv_filename)
print metadata_error
if __name__ == '__main__':
main(sys.argv[1:])