-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtranscodeEngine.py
executable file
·1588 lines (1435 loc) · 93.2 KB
/
transcodeEngine.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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/local/bin/python3
# -*- coding: utf-8 -*-
#Current Version: 1.7.0
#Version History
# 0.1.0 - 20171113
# Got it mostly working. current known issues:
# Can't handle non BlackMagic mediainfo files (but will fail gracefully and still make checksums)
# Doesn't output Rsync info as it's happening
# No logging
# 0.2.0 - 20171114
# Will ask user for valid input if the user input doesn't match expected
# Puts out Rsync Output
# Throws mediainfo parsing errors by the field, rather than by the file
# No logging, no mediainfo fixing
# 0.3.0 - 20171205
# Skips hidden files
# Creates sidecar checksum files upon request
# 0.4.0 - 20171211
# Changed --Output=XML to --Output=OLDXML to keep up to date with latest version of Mediainfo
# Fixed bug where choosing No Checksum would crash it
# 0.5.0 - 20180111
# Updated controlled vocabs for Digital Object Elements. removed .mov from filename
# 0.6.0 - 20180123
# Changed input args to be the -i method
# updated so 0 derivatives can be chosen so just the other methods are used.
# Fixed all of the indent/spaces nightmares happening throughout the script
# 0.7.0 - 20180409
# Fixed 4 channel issue. 4 channel MOV can now be properly processed
# 0.8.0 - 20180625
# Added Autio support
# 0.9.0 - 20180629
# Added bext and ID3 Support
# Fixed presraid move so it happens after transcoding
# 0.9.1 - 20180703
# Fixed bugs introduced in verion 0.9.1, added more comments
# 1.0.0 - 20181012
# Added MKV support!
# 1.0.1 - 20181024
# Patched to fix itunes duration error for mp3 files! added "-write_xing 0" to audio ffmpeg string
# 1.0.2 - Fixed typos in user interface (Derivatitves to Derivatives)
# 1.1.0 - 20190321
# -Added support for DV
# 1.1.1 - 20190321
# -Added support for NYU Deliverables
# 1.1.2 - 20190419
# -Added support for NYU Metadata names (stripping barcodes in metadata)
# 1.1.3 - 20200401
# -Fixed metadata harvesting for DV files
# 1.1.4 - 20200608
# -Added support for DV wrapped in .mov
# -no longer supports .dv files
# 1.2.0 - 202000608
# -Moving towards python 3 entirely
# -Adding simple_salesforce support for auto-updating salesfroce records with CSV metadata
# -rearranged order of operations a bit so that syncing to presraid happens last
# -added -nsf option to skip salesfroce sync (--NoSalesForce)
# -scripts now checks file size of destination files agianst source files after rsync and updates salesforce field "Loaded to PresRADID" upon success!
# 1.2.1 - 20200612
# -Added shebang #!/usr/local/bin/python3 so that we don't need to pt python3 before the script
# 1.2.2 - 20200625
# -Changed mp4 string to include -crf 18 instead of -b:v 3500000 to get better quality out of nasty videos.
# -inserted setdar=3/4 string into mp4 string to force apsect ratio
# 1.2.3 - 20201013
# -Hardcoded mezzanine audio sample rate to 48kHz
# 1.2.4 - 20210410
# -Fixed minor bug that kept coding history from working properly for second Minidisc deck
# 1.2.5 - 20210429
# -Added Tascam DA-20 to arsenal of coding history decks
# 1.3 - 20210528
# -HUGE UPDATE
# -Script now enters metadata directly from salesforce, but allowd for human entry if the salesforce fields are empty
# -Still to do: properly handle face02 metadata
# 1.3.1 - 20210602
# -Fixed bugs and feature issues for audio metadata embedding
# -script now enters IDIT, ICRD, ICRD, ISFT, ITCH for consistency
# -NYU access file extension changed to "_s.mp4"
# 1.3.2 - 20210825
# -Script now uses dvrescue automatically on dv in .mov files that it gets, no longer running QCTools on these files
# -Fixed bugs causing script not to work on a single file
# 1.3.3 - 20210901
# -Fixed bug that caused audio not to work
# -Fixed bugs that required full path after one wrong answer for pres raid path
# 1.3.4 - 20210903
# -removed vsync 0 flag which was cuasing bugs on latest versions of ffmpeg
# 1.4.0 - 20220114
# -finally added aspect ratio support. Uses media info metadata to pass aspect ratio to transcode string.
# -This has been tested and works with a mix of 4:3 and 16:9 contend for MP4, ProRes, and MKV derivatives
# 1.5.0 - 20220209
# -Hoping that v 1.5 can work out any lingering audio issues
# -Added PNG generation support to catch glitches and noise errors
# 1.5.1 - 20220613
# - For now, just testing github on new laptop
# 1.6.0 - 20220613
# - Added M4A support
# - Still need additional metadata field support from salesforce for CA-R metadata (mp4 metadata too)
# 1.6.1 - 20220615
# - Added MP4 and M4A metadata embedding
# - removed line that handles renaming file to NYU spec if 720x540 frame size is selected for MP4
# 1.6.2 - 20220616
# - MAJOR UPDATE: Users can now specify that the created FFV1/MKV file be the Preservation file.
# This means that the qctools and metadata will come from the MKV instead, which will also be loaded to the presRAID
# 1.6.3 - 20220617
# - Fixed a dumb mistake where the MKVMaster variable wasn't initialized
# - Fixed an error where empty metadata fields were causing a NoneType error when i tried to strip them of quotes
# - Updated ffmpeg metadata embedd string generation to leave out any fields that are empty (even though ffmpeg handles this gracefully)
# 1.6.4 - 20220622
# - Made minor changes to meet the CA-R Spec
# - MP4 filename must have .HD in it
# - Checksum format needs to be checksum *filename, added a check for this
# - Checksum for MP4 needs to be generated. This is done by checking if there is _prsv.mkv in the master name, since only CA-R wants that
# 1.6.5 - 20220819
# - Made updates to handle CA-R Audio files
# 1.6.6 - 20220815
# - Added better error handling for the following:
# - Fails gracefully now when input directory doesn't have any valid Files
# - Fails gracefully when encountering a broken file
# 1.6.7 - 20220822
# - Fixed metadata date handling when there is no creation date
# 1.6.7 - 20220919
# - Fixed metadata date handling for single and double quotes in user entry or salesforce fields
# 1.6.8 - 20220929
# - Added "tamwag" to text in filename that will trigger BAVC barcode removal from metadata.
# - at some point this will need to be connected to a switch in salesforce
# 1.7.0 - 2023019
# - Possibly my last update on this script as a BAVC employee!
# - Add better catches when SFInit fails. This way the script won't die if the network blinks out
# - Also added better error messages when SF connection fails
#
# STILL NEEDS
# Logging
# User Verification
#
# REQUIREMENTS
# ffmpeg
# QCLI
# bwfmetaedit
# id3v2
# sox
###REQUIRED LIBRARIES####
###simple_salesforce
# import modules used here -- sys is a very standard one
import os, sys
import datetime
import re # used for parsing checksum out of sidecar Files
import config # used for getting saleforce login and api key
import csv # used for creating the csv
import json # used for uploading csv info into salesfroce
import hashlib # used for creating the md5 checksum
import subprocess # used for running ffmpeg, qcli, and rsync
import shlex # used for properly splitting the ffmpeg/rsync strings
import argparse # used for parsing input arguments
from simple_salesforce import Salesforce
# Gather our code in a main() function
def main():
media_info_list = []
####init the stuff from the cli########
parser = argparse.ArgumentParser(description="Harvests Mediainfo of input file or files in input directory")
parser.add_argument('-i','--input',dest='i', help="the path to the input directory or files")
parser.add_argument('-o','--output',dest='o', help="the output file path (optional)")
parser.add_argument('-c','--csvname',dest='c', help="the name of the csv file (optional)")
parser.add_argument('-mkv','--Matroska',dest='mkv',action ='store_true',default=False, help="Allows input file type to be mkv rather than default mov")
parser.add_argument('-dv','--DV',dest='dv',action ='store_true',default=False, help="Allows input file type to be dv rather than default mov. Processes as 720x480 rather than 720x486")
parser.add_argument('-nsf', '--NoSalesForce',dest='nsf',action='store_true',default=False,help="Turns off 'No SalesForce' flag, which will avoid syncing the CSV to SF automatically. By defualt this script will sync CSV files to SalesForce")
args = parser.parse_args()
#handling the input args. This is kind of a mess in this version
if args.i is None:
print(bcolors.FAIL + "Please enter an input path!" + bcolors.ENDC)
quit()
if args.o is None:
print(bcolors.OKBLUE + "No output path defined, using default path" + bcolors.ENDC)
out_path = ""
else:
out_path = args.o
print(bcolors.OKBLUE + "User output path defined as " + out_path + bcolors.ENDC)
# This part can tell if we're processing a file or directory. Handles it accordingly
inPath = args.i
inType = fileOrDir(inPath)
#processDict contains the info the script needs to run each subprocess, inlcuding the options that the user selected, and the paths to the files
processDict = {}
processDict = createProcessDict(processDict)
#initialize master file extension in processDict
if args.mkv is True:
processDict['masterExtension'] = ".mkv"
elif args.dv is True:
processDict['masterExtension'] = ".mov"
else:
processDict['masterExtension'] = ".mov"
# Initialize CSV output info
if args.c is None:
csv_name = "mediainfo.csv"
elif ".csv" in args.c:
csv_name = args.c
else:
csv_name = args.c + ".csv"
# If we are processing a single file
if inType == "F":
print(bcolors.OKBLUE + "\nProcessing Input File: " + os.path.basename(inPath) + "\n\n" + bcolors.ENDC)
#Set the paths of the CSV files
if out_path == "":
csv_path = os.path.dirname(inPath) + "/" + csv_name
else:
csv_path = out_path
print(bcolors.OKBLUE + "Output CSV file path is: " + csv_path + "\n\n" + bcolors.ENDC)
#Process as Audio
if inPath.endswith(".wav"):
#Harvest mediainfo metadata, there is a subprocess in this script that inserts the BWAV metadata
mediaInfoDict = createMediaInfoDict(inPath, inType, processDict)
if mediaInfoDict == None:
print(bcolors.FAIL + "\nFatal Error. Please check the input file and try again!" + bcolors.ENDC)
quit()
#Transcode the File
processVideo(inPath, processDict, "WAV", "")
#Insert ID3 metadata into MP3. only do this for MP3, not for M4A
for derivCount in range (0, (processDict.get('numDerivs'))):
if processDict['derivDetails'][derivCount]['derivType'] == 5:
insertID3(mediaInfoDict['audioMetaDict'], inPath.replace(".wav","_access.mp3"))
if processDict['derivDetails'][derivCount]['derivType'] == 6:
if "_prsv.wav" in inPath:
access_path = inPath.replace("_prsv.wav","_access.m4a")
insertMetaM4A(mediaInfoDict['audioMetaDict'], access_path)
if "CaliforniaRevealed" in access_path or "CA-R" in access_path:
access_temp_path = access_path.replace("_prsv.wav","_access.m4a")
#this is a dumb hardcoded thing where any files with _prsv.wav with m4a derivs will get CA-R Style checksums for the M4a
insertMetaM4A(mediaInfoDict['audioMetaDict'], access_path)
accessHash = hashfile(access_path, "md5", blocksize=65536)
sidecarPath = access_path + ".md5"
f = open(sidecarPath,'w')
f.write(accessHash)
f.write(" *" + os.path.basename(access_temp_path).replace("BAVC" + (mediaInfoDict["Name"] + "_"),""))
f.close()
else:
insertMetaM4A(mediaInfoDict['audioMetaDict'], inPath.replace(".wav","_access.m4a"))
#remove audio metadata from CSV Metadata Dict, necessary to keep the output CSV clean
del mediaInfoDict['audioMetaDict']
media_info_list.append(mediaInfoDict) # Turns the dicts into lists
else:
#Harvest mediainfo metadata
mediaInfoDict = createMediaInfoDict(inPath, inType, processDict)
if mediaInfoDict == None:
print(bcolors.FAIL + "\nFatal Error. Please check the input file and try again!" + bcolors.ENDC)
quit()
#create video metadata dict from the mediainfo dict
videoMetaDict = mediaInfoDict['audioMetaDict']
del mediaInfoDict['audioMetaDict'] #need to delete the extra embedded metadata or the CSV can't be created properly
media_info_list.append(mediaInfoDict) # Turns the dicts into lists
# Quick little method to see if we're going to crop the file. This should eventually be its own function that does tons of pre-ffmpeg processing :->
frameSize = media_info_list[0]['essenceTrackFrameSize__c']
if "486" in frameSize:
processDict['crop'] = 1
else:
processDict['crop'] = 2
# FFmpeg and QCTools the file
if processDict['numDerivs'] == 0:
print(bcolors.OKBLUE + "User Select Zero Derivatives, Skipping Transcode of " + os.path.basename(inPath) + "\n\n" + bcolors.ENDC)
if processDict['createQCT'] == 1:
if "dv" in media_info_list[0]["essenceTrackEncodingVideo__c"] or "DV" in media_info_list[0]["essenceTrackEncodingVideo__c"]:
runCommand("dvrescue '" + inPath + "' -x '" + inPath + ".dvrescue.xml' -c '" + inPath + ".dvrescue.scc' -s '" + inPath + ".dvrescue.vtt'")
else:
runCommand("qcli -i '" + inPath + "'")
else:
processVideo(inPath, processDict, media_info_list[0]["essenceTrackEncodingVideo__c"], media_info_list[0]["essenceTrackAspectRatio__c"])
#Insert MP4 Metadata
for derivCount in range (0, (processDict.get('numDerivs'))):
if processDict['derivDetails'][derivCount]['derivType'] == 1:
if "_prsv.mkv" in inPath: #just a quick switch to make sure that CA-R files are renamed to remove "_prsv" and have ".HD" in them
insertMetaM4A(videoMetaDict, inPath.replace("_prsv.mkv","_access.HD.mp4"))
elif "_prsv.mov" in inPath: #just a quick switch to make sure that CA-R files are renamed to remove "_prsv" and have ".HD" in them
insertMetaM4A(videoMetaDict, inPath.replace("_prsv.mov","_access.HD.mp4"))
else:
insertMetaM4A(videoMetaDict, inPath.replace(".mov","_access.mp4"))
#This is where we reprocess the metadata harvesting for the MKV master
if processDict['MKVMaster'] == 1:
processDict['MKVMaster'] = 0
mediaInfoDict = createMediaInfoDict(inPath.replace(".mov",".mkv"), inType, processDict)
if mediaInfoDict == None:
print(bcolors.FAIL + "\nFatal Error. Please check the input file and try again!" + bcolors.ENDC)
quit()
del mediaInfoDict['audioMetaDict'] #need to delete the extra embedded metadata or the CSV can't be created properly
media_info_list.pop() #we need to remove the MOV entry from the list
media_info_list.append(mediaInfoDict) # then we add the MKV info back to the list
processDict['MKVMaster'] = 1
# Make the mediainfo CSV
print(bcolors.OKGREEN + "DONE! Creating CSV File " + "\n" + bcolors.ENDC)
createCSV(media_info_list, csv_path) # this processes a list with a single dict in it (this is the case that only one file was given as the input)
updateSalesForceCSV(csv_path, args.nsf, mediaInfoDict["Name"]) # syncs CSV file to salesforce
# Rsync the File
inPathList = []
inPathList.append(inPath)
if processDict['MKVMaster'] == 1: #add MKV file to rsync list as well
inPathList.append(inPath.replace(".mov",".mkv"))
moveToBackup(inPathList, processDict, args.nsf)
print(bcolors.OKBLUE + "DONE!" + "\n\n" + bcolors.ENDC)
quit()
# If we are processing an entire directory file
elif inType == "D":
print(bcolors.OKBLUE + "Processing Input Directory: " + os.path.dirname(inPath) + "\n\n" + bcolors.ENDC)
#Set path of output csv file
if out_path == "":
csv_path = inPath + "/" + csv_name
else:
csv_path = out_path
print(bcolors.OKBLUE + "Output CSV file path is: " + csv_path + "\n\n" + bcolors.ENDC)
# Need this part to get the number of Mov files.
movCount = 0
for root, directories, filenames in os.walk(inPath):
for filename in filenames:
tempFilePath = os.path.join(root,filename)
if tempFilePath.endswith(processDict['masterExtension']) and not tempFilePath.endswith('_mezzanine.mov') and not filename.startswith('.'):
movCount = movCount + 1
#If no MOV/MKV files are found, we're going to run as audio, so count the number of WAV files
if movCount == 0:
wavCount = 0
for root, directories, filenames in os.walk(inPath):
for filename in filenames:
tempFilePath = os.path.join(root,filename)
if tempFilePath.endswith('.wav') and not tempFilePath.endswith('_mezzanine.wav') and not filename.startswith('.'):
wavCount = wavCount + 1
#now we walk through the directory to process each file
for root, directories, filenames in os.walk(inPath):
fileNum = 0
inPathList = []
for filename in filenames:
#Process the file
tempFilePath = os.path.join(root,filename)
#Process as Audio
if tempFilePath.endswith('.wav') and not tempFilePath.endswith('_mezzanine.wav') and not filename.startswith('.'):
#Harvest mediainfo metadata, there is a subprocess in this script that inserts the BWAV metadata
mediaInfoDict = createMediaInfoDict(tempFilePath, inType, processDict)
if mediaInfoDict == None:
print(bcolors.FAIL + "\nFatal Error. Please check the input file and try again!" + bcolors.ENDC)
continue
#Transcode the file
processVideo(tempFilePath, processDict, "WAV", "")
#Insert ID3 metadata into MP3. only do this for MP3, not for M4A
for derivCount in range (0, (processDict.get('numDerivs'))):
if processDict['derivDetails'][derivCount]['derivType'] == 5:
insertID3(mediaInfoDict['audioMetaDict'], tempFilePath.replace(".wav","_access.mp3"))
if processDict['derivDetails'][derivCount]['derivType'] == 6:
if "_prsv.wav" in tempFilePath:
if "CaliforniaRevealed" in tempFilePath or "CA-R" in tempFilePath:
access_temp_path = tempFilePath.replace("_prsv.wav","_access.m4a")
#this is a dumb hardcoded thing where any files with _prsv.wav with m4a derivs will get CA-R Style checksums for the M4a
insertMetaM4A(mediaInfoDict['audioMetaDict'], access_temp_path)
accessHash = hashfile(access_temp_path, "md5", blocksize=65536)
sidecarPath = access_temp_path + ".md5"
f = open(sidecarPath,'w')
f.write(accessHash)
f.write(" *" + os.path.basename(access_temp_path).replace("BAVC" + (mediaInfoDict["Name"] + "_"),""))
f.close()
else:
insertMetaM4A(mediaInfoDict['audioMetaDict'], tempFilePath.replace(".wav","_access.m4a"))
#remove audio metadata from CSV Metadata Dict
del mediaInfoDict['audioMetaDict']
media_info_list.append(mediaInfoDict) # Turns the dicts into lists
# Add to the list of paths to be rsynced
inPathList.append(tempFilePath)
fileNum += 1
print(bcolors.OKBLUE + "Done!\n" + bcolors.ENDC)
#create CSV
print(bcolors.OKGREEN + "DONE! Creating CSV File " + "\n\n" + bcolors.ENDC)
createCSV(media_info_list,csv_path) # this instances process the big list of dicts
#process as video
elif tempFilePath.endswith(processDict['masterExtension']) and not tempFilePath.endswith('_mezzanine.mov') and not filename.startswith('.'):
#Harvest mediainfo metadata
mediaInfoDict = createMediaInfoDict(tempFilePath, inType, processDict)
if mediaInfoDict == None:
print(bcolors.FAIL + "\nFatal Error. Please check the input file and try again!" + bcolors.ENDC)
continue
#create video metadata dict from the mediainfo dict
videoMetaDict = mediaInfoDict['audioMetaDict']
del mediaInfoDict['audioMetaDict'] #idk why i was deleting this before but i'm still doing it now for good measure
media_info_list.append(mediaInfoDict) # Turns the dicts into lists
# Quick little method to see if we're going to crop the file. This should eventually be its own function that does tons of pre-ffmpeg processing :->
frameSize = media_info_list[0]['essenceTrackFrameSize__c']
if "486" in frameSize:
processDict['crop'] = 1
else:
processDict['crop'] = 2
# FFmpeg and QCTools the file
if processDict['numDerivs'] == 0:
print(bcolors.OKBLUE + "User Select Zero Derivatives, Skipping Transcode of " + filename + "\n\n" + bcolors.ENDC)
if processDict['createQCT'] == 1:
if "dv" in media_info_list[fileNum]["essenceTrackEncodingVideo__c"] or "DV" in media_info_list[fileNum]["essenceTrackEncodingVideo__c"]:
runCommand("dvrescue '" + tempFilePath + "' -x '" + tempFilePath + ".dvrescue.xml' -c '" + tempFilePath + ".dvrescue.scc' -s '" + tempFilePath + ".dvrescue.vtt'")
else:
runCommand("qcli -i '" + tempFilePath + "'")
else:
processVideo(tempFilePath, processDict, media_info_list[fileNum]["essenceTrackEncodingVideo__c"], media_info_list[fileNum]["essenceTrackAspectRatio__c"])
for derivCount in range (0, (processDict.get('numDerivs'))):
if processDict['derivDetails'][derivCount]['derivType'] == 1:
if "_prsv.mov" in tempFilePath:
insertMetaM4A(videoMetaDict, tempFilePath.replace("_prsv.mov","_access.HD.mp4"))
elif "_prsv.mkv" in tempFilePath:
insertMetaM4A(videoMetaDict, tempFilePath.replace("_prsv.mkv","_access.HD.mp4"))
else:
insertMetaM4A(videoMetaDict, tempFilePath.replace(".mov","_access.mp4"))
#This is where we reprocess the metadata harvesting for the MKV master
if processDict['MKVMaster'] == 1:
processDict['MKVMaster'] = 0
mediaInfoDict = createMediaInfoDict(tempFilePath.replace(".mov",".mkv"), inType, processDict)
if mediaInfoDict == None:
print(bcolors.FAIL + "\nFatal Error. Please check the input file and try again!" + bcolors.ENDC)
continue
del mediaInfoDict['audioMetaDict'] #need to delete the extra embedded metadata or the CSV can't be created properly
media_info_list.pop() #we need to remove the MOV entry from the list
media_info_list.append(mediaInfoDict) # then we add the MKV info back to the list
processDict['MKVMaster'] = 1
# Add to the list of paths to be rsynced
inPathList.append(tempFilePath)
fileNum += 1
if processDict['MKVMaster'] == 1: #Add the MKV to the list of files to be rsync'd
inPathList.append(tempFilePath.replace(".mov",".mkv"))
#Progress bar fun
#numFiles = movCount
#percentDone = float(float(fileIndex)/float(numFiles)*100.0)
#sys.stdout.write('\r')
#sys.stdout.write("[%-20s] %d%% %s \n" % ('='*int(percentDone/5.0), percentDone, filename))
#sys.stdout.flush()
if inPathList == []:
print(bcolors.FAIL + "\nNo valid input files were found in this folder. Please check the files or directory and try again\n" + bcolors.ENDC)
quit()
print(bcolors.OKBLUE + "Done!\n" + bcolors.ENDC)
print(bcolors.OKGREEN + "DONE! Creating CSV File " + "\n\n" + bcolors.ENDC)
createCSV(media_info_list,csv_path) # this instances process the big list of dicts
updateSalesForceCSV(csv_path, args.nsf, mediaInfoDict["Name"]) # syncs CSV file to salesforce
# Rsync the Files that were trasncoded
moveToBackup(inPathList, processDict, args.nsf)
quit()
# Determine whether input is a file or a Directory, returns F or D respectively, quits otherwise
def fileOrDir(inPath):
if os.path.isdir(inPath):
return "D"
elif os.path.isfile(inPath):
return "F"
else:
print("I couldn't determine or find the input type!")
quit()
#Process a single file
def createMediaInfoDict(filePath, inType, processDict):
media_info_text = getMediaInfo(filePath)
media_info_dict = parseMediaInfo(filePath, media_info_text, processDict['hashType'], processDict['sidecar'], processDict['masterExtension'], processDict['MKVMaster'], processDict['hashFormat'])
return media_info_dict
#gets the Mediainfo text
def getMediaInfo(filePath):
print(bcolors.OKGREEN + "Running Mediainfo and Checksums (If Selected)\n\n" + bcolors.ENDC)
cmd = [ '/usr/local/bin/mediainfo', '-f', '--Output=OLDXML', filePath ]
media_info = subprocess.Popen( cmd, stdout=subprocess.PIPE,encoding='utf8').communicate()[0]
return media_info
#process mediainfo object into a dict
def parseMediaInfo(filePath, media_info_text, hashType, sidecar, masterExtension, MKVMaster, hashFormat):
# The following line initializes the dict.
if MKVMaster == 0:
masterExtension = ".mkv" #MKVMaster will only be zero when it's time to reprocess the MKV after its initial creation.
file_dict = {"Name" : "", "instantiationIdentifierDigital__c" : "", "essenceTrackDuration__c" : "", "instantiationFileSize__c" : "", "instantiationDigital__c" : "", "essenceTrackEncodingVideo__c" : "", "essenceTrackBitDepthVideo__c" : "", "essenceTrackCompressionMode__c" : "", "essenceTrackScanType__c" : "", "essenceTrackFrameRate__c" : "", "essenceTrackFrameSize__c" : "", "essenceTrackAspectRatio__c" : "", "instantiationDataRateVideo__c" : "", "instantiationDigitalColorMatrix__c" : "", "instantiationDigitalColorSpace__c" : "", "instantiationDigitalChromaSubsampling__c" : "", "instantiationDataRateAudio__c" : "", "essenceTrackBitDepthAudio__c" : "", "essenceTrackSamplingRate__c" : "", "essenceTrackEncodingAudio__c" : "", "instantiationChannelConfigDigitalLayout__c" : "", "instantiationChannelConfigurationDigital__c" : "", "messageDigest" : "", "messageDigestAlgorithm" : "", "audioMetaDict" : {}}
file_dict["instantiationIdentifierDigital__c"] = os.path.basename(filePath).split(".")[0]
barcodeTemp = file_dict["instantiationIdentifierDigital__c"]
try:
barcodeTemp = str(barcodeTemp).split("_")[0]
file_dict["Name"] = barcodeTemp.split("BAVC")[1]
if "WDA_" in file_dict["instantiationIdentifierDigital__c"]:
print(bcolors.OKGREEN + "Renaming File for Disney Specs" + bcolors.ENDC)
file_dict["instantiationIdentifierDigital__c"] = file_dict["instantiationIdentifierDigital__c"].replace("BAVC" + file_dict["Name"] + "_","")
elif "nyuarchives" in file_dict["instantiationIdentifierDigital__c"]:
print(bcolors.OKGREEN + "Renaming File for NYU Specs" + bcolors.ENDC)
file_dict["instantiationIdentifierDigital__c"] = file_dict["instantiationIdentifierDigital__c"].replace("BAVC" + file_dict["Name"] + "_","")
elif "tamwag" in file_dict["instantiationIdentifierDigital__c"]:
print(bcolors.OKGREEN + "Renaming File for NYU Specs" + bcolors.ENDC)
file_dict["instantiationIdentifierDigital__c"] = file_dict["instantiationIdentifierDigital__c"].replace("BAVC" + file_dict["Name"] + "_","")
elif "_prsv" in file_dict["instantiationIdentifierDigital__c"]:
print(bcolors.OKGREEN + "Renaming File for CA-R Specs" + bcolors.ENDC)
file_dict["instantiationIdentifierDigital__c"] = file_dict["instantiationIdentifierDigital__c"].replace("BAVC" + file_dict["Name"] + "_","")
except:
print(bcolors.FAIL + "Error parsing filename, No Barcode given for this file!\n\n")
try:
mi_General_Text = (media_info_text.split("<track type=\"General\">"))[1].split("</track>")[0]
if masterExtension in filePath:
mi_Video_Text = (media_info_text.split("<track type=\"Video\">"))[1].split("</track>")[0]
try:
mi_Audio_Text = (media_info_text.split("<track type=\"Audio\">"))[1].split("</track>")[0]
except:
mi_Audio_Text = (media_info_text.split("<track type=\"Audio\" typeorder=\"1\">"))[1].split("</track>")[0]
except:
print(bcolors.FAIL + "MEDIAINFO ERROR: Could not parse tracks for " + file_dict["instantiationIdentifierDigital__c"] + "\n\n" + bcolors.ENDC)
# General Stuff
try:
file_dict["essenceTrackDuration__c"] = (mi_General_Text.split("<Duration>"))[6].split("</Duration>")[0]
except:
print(bcolors.FAIL + "MEDIAINFO ERROR: Could not parse Duration for " + file_dict["instantiationIdentifierDigital__c"] + "\n\n" + bcolors.ENDC)
try:
fileFormatTemp = (mi_General_Text.split("<Format>"))[1].split("</Format>")[0]
if fileFormatTemp == "MPEG-4":
file_dict["instantiationDigital__c"] = "MOV"
try:
file_dict = getVideoMetadata(file_dict, filePath, file_dict["Name"])
except Exception as e:
print(bcolors.FAIL + "METADATA ERROR: Could not properly parse video metadata for " + file_dict["instantiationIdentifierDigital__c"] + "\n\n" + bcolors.ENDC)
print(sys.exc_info())
elif fileFormatTemp == "Matroska":
file_dict["instantiationDigital__c"] = "MKV"
try:
file_dict = getVideoMetadata(file_dict, filePath, file_dict["Name"])
except Exception as e:
print(bcolors.FAIL + "METADATA ERROR: Could not properly parse video metadata for " + file_dict["instantiationIdentifierDigital__c"] + "\n\n" + bcolors.ENDC)
print(sys.exc_info())
elif fileFormatTemp == "DV":
file_dict["instantiationDigital__c"] = "DV"
try:
file_dict = getVideoMetadata(file_dict, filePath, file_dict["Name"])
except Exception as e:
print(bcolors.FAIL + "METADATA ERROR: Could not properly parse video metadata for " + file_dict["instantiationIdentifierDigital__c"] + "\n\n" + bcolors.ENDC)
print(sys.exc_info())
elif fileFormatTemp == "Wave":
file_dict["instantiationDigital__c"] = "WAV"
try:
file_dict = getAudioMetadata(file_dict, filePath, file_dict["Name"])
except Exception as e:
print(bcolors.FAIL + "METADATA ERROR: Could not properly parse audio metadata for " + file_dict["instantiationIdentifierDigital__c"] + "\n\n" + bcolors.ENDC)
print(sys.exc_info())
#This is where we insert the BWAV metadata. tag value pairs are added the medainfo dict (so we don't need to add more dicts) then rmeoved later on in the script
try:
insertBWAV(file_dict, filePath)
except Exception as e:
print(bcolors.FAIL + "METADATA ERROR: Could not properly embed bwav metadata for " + file_dict["instantiationIdentifierDigital__c"] + "\n\n" + bcolors.ENDC)
print(sys.exc_info())
try:
createSpectro(filePath)
except Exception as e:
print(bcolors.FAIL + "METADATA ERROR: Could not make spectrogram for file " + file_dict["instantiationIdentifierDigital__c"] + "\n\n" + bcolors.ENDC)
print(sys.exc_info())
else:
print(bcolors.FAIL + "METADATA ERROR: Unable to parse digital file format for file " + file_dict["instantiationIdentifierDigital__c"] + "\n\n" + bcolors.ENDC)
return None
except:
print(bcolors.FAIL + "MEDIAINFO ERROR: Could not File Format for " + file_dict["instantiationIdentifierDigital__c"] + "\n\n" + bcolors.ENDC)
try:
file_dict["instantiationFileSize__c"] = (mi_General_Text.split("<File_size>"))[6].split("</File_size>")[0]
except:
print(bcolors.FAIL + "MEDIAINFO ERROR: Could not parse File Size for " + file_dict["instantiationIdentifierDigital__c"] + "\n\n" + bcolors.ENDC)
# Video Stuff
if masterExtension in filePath or MKVMaster == 0: #mkv only equals zero when this subprocess is being re-run on the master mkv
try:
try:
file_dict["essenceTrackEncodingVideo__c"] = (mi_Video_Text.split("<Codec_ID>"))[1].split("</Codec_ID>")[0]
except:
file_dict["essenceTrackEncodingVideo__c"] = (mi_Video_Text.split("<Codec>"))[1].split("</Codec>")[0]
if file_dict["essenceTrackEncodingVideo__c"] == "v210":
file_dict["essenceTrackEncodingVideo__c"] = "Uncompressed 10-bit (v210)"
elif file_dict["essenceTrackEncodingVideo__c"] == "apch":
file_dict["essenceTrackEncodingVideo__c"] = "Apple ProRes 422 HQ"
elif file_dict["essenceTrackEncodingVideo__c"] == "apcn":
file_dict["essenceTrackEncodingVideo__c"] = "Apple ProRes 422"
elif file_dict["essenceTrackEncodingVideo__c"] == "apcs":
file_dict["essenceTrackEncodingVideo__c"] = "Apple ProRes 422 LT"
elif file_dict["essenceTrackEncodingVideo__c"] == "apco":
file_dict["essenceTrackEncodingVideo__c"] = "Apple ProRes 422 Proxy"
elif file_dict["essenceTrackEncodingVideo__c"] == "ap4h":
file_dict["essenceTrackEncodingVideo__c"] = "Apple ProRes 4444"
elif file_dict["essenceTrackEncodingVideo__c"] == "dv":
file_dict["essenceTrackEncodingVideo__c"] = "DV"
elif file_dict["essenceTrackEncodingVideo__c"] == "dvc ":
file_dict["essenceTrackEncodingVideo__c"] = "DV"
elif file_dict["essenceTrackEncodingVideo__c"] == "dvc":
file_dict["essenceTrackEncodingVideo__c"] = "DV"
elif file_dict["essenceTrackEncodingVideo__c"] == "DV":
file_dict["essenceTrackEncodingVideo__c"] = "DV"
elif "FFV1" in file_dict["essenceTrackEncodingVideo__c"]:
file_dict["essenceTrackEncodingVideo__c"] = "FFV1"
except:
try:
file_dict["essenceTrackEncodingVideo__c"] = "DV"
except:
print(bcolors.FAIL + "MEDIAINFO ERROR: Could not parse Video Track Encoding for " + file_dict["instantiationIdentifierDigital__c"] + "\n\n" + bcolors.ENDC)
try:
file_dict["essenceTrackBitDepthVideo__c"] = (mi_Video_Text.split("<Bit_depth>"))[2].split("</Bit_depth>")[0].split(" ")[0]
except:
print(bcolors.FAIL + "MEDIAINFO ERROR: Could not parse Video Bit Depth for " + file_dict["instantiationIdentifierDigital__c"] + "\n\n" + bcolors.ENDC)
try:
file_dict["essenceTrackCompressionMode__c"] = (mi_Video_Text.split("<Compression_mode>"))[1].split("</Compression_mode>")[0]
except:
if "ProRes" in file_dict["essenceTrackEncodingVideo__c"]:
file_dict["essenceTrackCompressionMode__c"] = "Lossy"
else:
print(bcolors.FAIL + "MEDIAINFO ERROR: Could not parse Compression Mode for " + file_dict["instantiationIdentifierDigital__c"] + "\n\n" + bcolors.ENDC)
try:
file_dict["essenceTrackScanType__c"] = (mi_Video_Text.split("<Scan_type>"))[1].split("</Scan_type>")[0]
except:
print(bcolors.FAIL + "MEDIAINFO ERROR: Could not parse Scan Type for " + file_dict["instantiationIdentifierDigital__c"] + "\n\n" + bcolors.ENDC)
try:
file_dict["essenceTrackFrameRate__c"] = (mi_Video_Text.split("<Frame_rate>"))[1].split("</Frame_rate>")[0]
if file_dict["essenceTrackFrameRate__c"] == "29.970":
file_dict["essenceTrackFrameRate__c"] = "29.97"
except:
print(bcolors.FAIL + "MEDIAINFO ERROR: Could not parse Frame Rate for " + file_dict["instantiationIdentifierDigital__c"] + "\n\n" + bcolors.ENDC)
try:
frame_width = (mi_Video_Text.split("<Width>"))[1].split("</Width>")[0]
except:
print(bcolors.FAIL + "MEDIAINFO ERROR: Could not parse Frame Width for " + file_dict["instantiationIdentifierDigital__c"] + "\n\n" + bcolors.ENDC)
try:
frame_height = (mi_Video_Text.split("<Height>"))[1].split("</Height>")[0]
except:
print(bcolors.FAIL + "MEDIAINFO ERROR: Could not parse Frame Height for " + file_dict["instantiationIdentifierDigital__c"] + "\n\n" + bcolors.ENDC)
try:
file_dict["essenceTrackFrameSize__c"] = frame_width + " x " + frame_height
except:
print(bcolors.FAIL + "MEDIAINFO ERROR: Could not create frame size using height and width of " + file_dict["instantiationIdentifierDigital__c"] + "\n\n" + bcolors.ENDC)
try:
file_dict["essenceTrackAspectRatio__c"] = (mi_Video_Text.split("<Display_aspect_ratio>"))[2].split("</Display_aspect_ratio>")[0]
except:
print(bcolors.FAIL + "MEDIAINFO ERROR: Could not parse Display Aspect Ratio for " + file_dict["instantiationIdentifierDigital__c"] + "\n\n" + bcolors.ENDC)
try:
file_dict["instantiationDataRateVideo__c"] = (mi_Video_Text.split("<Bit_rate>"))[2].split("</Bit_rate>")[0]
file_dict["instantiationDataRateVideo__c"] = file_dict["instantiationDataRateVideo__c"].replace("/","p")
except:
#this catches the overall bitrate of FFV1 files. It's a bit of a fudge, but gets the point across
try:
file_dict["instantiationDataRateVideo__c"] = (mi_General_Text.split("<Overall_bit_rate>"))[2].split("</Overall_bit_rate>")[0]
file_dict["instantiationDataRateVideo__c"] = file_dict["instantiationDataRateVideo__c"].replace("/","p")
except:
print(bcolors.FAIL + "MEDIAINFO ERROR: Could not parse Video Data Rate for " + file_dict["instantiationIdentifierDigital__c"] + "\n\n" + bcolors.ENDC)
try:
if file_dict["essenceTrackEncodingVideo__c"] == "DV":
file_dict["instantiationDigitalColorMatrix__c"] = "n/a"
elif "dvc" in file_dict["essenceTrackEncodingVideo__c"]: #for some reason i had to do this instead of ==. couldn't figure out why, but go with what works I guess!
file_dict["instantiationDigitalColorMatrix__c"] = "n/a"
else:
file_dict["instantiationDigitalColorMatrix__c"] = (mi_Video_Text.split("<Color_primaries>"))[1].split("</Color_primaries>")[0].split(" ")[0]
except:
print(bcolors.FAIL + "MEDIAINFO ERROR: Could not parse Digital Color Matrix for " + file_dict["instantiationIdentifierDigital__c"] + "\n\n" + bcolors.ENDC)
try:
if file_dict["essenceTrackEncodingVideo__c"] == "DV" or file_dict["essenceTrackEncodingVideo__c"] == "dvc":
file_dict["instantiationDigitalColorSpace__c"] = "n/a"
else:
file_dict["instantiationDigitalColorSpace__c"] = (mi_Video_Text.split("<Color_space>"))[1].split("</Color_space>")[0]
except:
print(bcolors.FAIL + "MEDIAINFO ERROR: Could not parse Digital Color Space for " + file_dict["instantiationIdentifierDigital__c"] + "\n\n" + bcolors.ENDC)
try:
file_dict["instantiationDigitalChromaSubsampling__c"] = (mi_Video_Text.split("<Chroma_subsampling>"))[1].split("</Chroma_subsampling>")[0]
except:
print(bcolors.FAIL + "MEDIAINFO ERROR: Could not parse Chroma Subsampling for " + file_dict["instantiationIdentifierDigital__c"] + "\n\n" + bcolors.ENDC)
# Audio Stuff
try:
file_dict["essenceTrackBitDepthAudio__c"] = (mi_Audio_Text.split("<Resolution>"))[1].split("</Resolution>")[0]
except:
try:
file_dict["essenceTrackBitDepthAudio__c"] = (mi_Audio_Text.split("<Bit_depth>"))[1].split("</Bit_depth>")[0]
except:
print(bcolors.FAIL + "MEDIAINFO ERROR: Could not parse Audio Bit Depth for " + file_dict["instantiationIdentifierDigital__c"] + "\n\n" + bcolors.ENDC)
try:
samplingRate = (mi_Audio_Text.split("<Sampling_rate>"))[1].split("</Sampling_rate>")[0]
if samplingRate == "44100":
samplingRate = "44.1"
else:
samplingRate = int(samplingRate)/1000
file_dict["essenceTrackSamplingRate__c"] = str(samplingRate) + " kHz"
except:
print(bcolors.FAIL + "MEDIAINFO ERROR: Could not parse Audio Sampling Rate for " + file_dict["instantiationIdentifierDigital__c"] + "\n\n" + bcolors.ENDC)
try:
file_dict["essenceTrackEncodingAudio__c"] = (mi_Audio_Text.split("<Codec>"))[1].split("</Codec>")[0]
if file_dict["essenceTrackEncodingAudio__c"] == "PCM":
file_dict["essenceTrackEncodingAudio__c"] = "Linear PCM"
except:
try:
file_dict["essenceTrackEncodingAudio__c"] = (mi_Audio_Text.split("<Format>"))[1].split("</Format>")[0]
except:
print(bcolors.FAIL + "MEDIAINFO ERROR: Could not parse Audio Track Encoding for " + file_dict["instantiationIdentifierDigital__c"] + "\n\n" + bcolors.ENDC)
try:
audioDataRate = (mi_Audio_Text.split("<Bit_rate>"))[1].split("</Bit_rate>")[0]
audioDataRate = int(audioDataRate)/1000
file_dict["instantiationDataRateAudio__c"] = str(audioDataRate) + " Kbps"
except:
try:
if file_dict["essenceTrackSamplingRate__c"] == "48 kHz" and file_dict["essenceTrackBitDepthAudio__c"] == "24":
file_dict["instantiationDataRateAudio__c"] = "2304 Kbps"
except:
print(bcolors.FAIL + "MEDIAINFO ERROR: Could not parse Audio Data Rate for " + file_dict["instantiationIdentifierDigital__c"] + "\n\n" + bcolors.ENDC)
try:
file_dict["instantiationChannelConfigurationDigital__c"] = (mi_Audio_Text.split("<Channel_s_>"))[2].split("</Channel_s_>")[0]
except:
print(bcolors.FAIL + "MEDIAINFO ERROR: Could not parse Channel Configuration for " + file_dict["instantiationIdentifierDigital__c"] + "\n\n" + bcolors.ENDC)
try:
file_dict["instantiationChannelConfigDigitalLayout__c"] = (mi_Audio_Text.split("<ChannelLayout>"))[1].split("</ChannelLayout>")[0]
except:
if file_dict["instantiationChannelConfigurationDigital__c"] == "2 channels":
file_dict["instantiationChannelConfigDigitalLayout__c"] = "L R"
else:
print(bcolors.FAIL + "MEDIAINFO ERROR: Could not parse Channel Layout for " + file_dict["instantiationIdentifierDigital__c"] + "\n\n" + bcolors.ENDC)
try:
# Checksum
if hashType == "none" or MKVMaster == 1:
file_dict["messageDigest"] = ""
file_dict["messageDigestAlgorithm"] = ""
else:
file_dict["messageDigest"] = hashfile(filePath, hashType, blocksize=65536)
file_dict["messageDigestAlgorithm"] = hashType
if sidecar == 1:
sidecarPath = filePath + "." + hashType
f = open(sidecarPath,'w')
f.write(file_dict["messageDigest"])
if hashFormat == 2:
f.write(" *" + os.path.basename(filePath).replace("BAVC" + (file_dict["Name"] + "_"),""))
f.close()
if sidecar == 1 and "_prsv.mkv" in filePath: #if file contains _prsv.mkv then we assume it's a CA-R file and we're gonna make a sidecar for it
accessPath = filePath.replace("_prsv.mkv", "_access.HD.mp4")
if os.path.isfile(accessPath): #double checks to see if file exists, this basically only works because the CA-R naming schema is so specific
accessHash = hashfile(accessPath, hashType, blocksize=65536)
sidecarPath = accessPath + "." + hashType
f = open(sidecarPath,'w')
f.write(accessHash)
if hashFormat == 2:
f.write(" *" + os.path.basename(accessPath).replace("BAVC" + (file_dict["Name"] + "_"),""))
f.close()
if sidecar == 1 and "_prsv.wav" in filePath: #if file contains _prsv.wav then we assume it's a CA-R file and we're gonna make a sidecar for it
accessPath = filePath.replace("_prsv.wav", "_access.m4a")
if os.path.isfile(accessPath): #double checks to see if file exists, this basically only works because the CA-R naming schema is so specific
accessHash = hashfile(accessPath, hashType, blocksize=65536)
sidecarPath = accessPath + "." + hashType
f = open(sidecarPath,'w')
f.write(accessHash)
if hashFormat == 2:
f.write(" *" + os.path.basename(accessPath).replace("BAVC" + (file_dict["Name"] + "_"),""))
f.close()
except:
print(bcolors.FAIL + "Error creating checksum for " + file_dict["instantiationIdentifierDigital__c"] + "\n\n" + bcolors.ENDC)
return file_dict
# Create a CSV file from a dict
def createCSV(media_info_list, csv_path):
keys = media_info_list[0].keys()
with open(csv_path, 'w') as output_file:
dict_writer = csv.DictWriter(output_file, keys)
dict_writer.writeheader()
dict_writer.writerows(media_info_list)
# Generate checksum for the file
def hashfile(filePath, hashalg, blocksize=65536):
afile = open(filePath,'rb')
hasher = hashlib.new(hashalg) #grab the hashing algorithm decalred by user
buf = afile.read(blocksize) # read the file into a buffer cause it's more efficient for big files
while len(buf) > 0: # little loop to keep reading
hasher.update(buf) # here's where the hash is actually generated
buf = afile.read(blocksize) # keep reading
return hasher.hexdigest()
# Runs the scripting. FFmpeg, QCLI
def processVideo(inPath, processDict, videoCodec, aspectRatio):
processVideoCMD = createString(inPath, processDict, processVideo, videoCodec, aspectRatio)
runCommand(processVideoCMD)
# Creates the string based off the inputs
def createString(inPath, processDict, processVideo, videoCodec, aspectRatio):
# ffmpeg_string = "/usr/local/bin/ffmpeg -hide_banner -loglevel panic -vsync 0 -i '" + inPath + "' "
ffmpeg_string = "/usr/local/bin/ffmpeg -hide_banner -loglevel panic -i '" + inPath + "' "
if aspectRatio == "": #in case we're unable to get the aspect ratio for some reason or another
aspectRatioColon = "4:3"
aspectRatioSlash = "4/3"
else:
aspectRatioColon = aspectRatio
aspectRatioSlash = aspectRatio.replace(":", "/")
for derivCount in range(len(processDict['derivDetails'])):
#skip the following if deriv type is MP3
if processDict['derivDetails'][derivCount]['derivType'] <= 4:
# See if user opted to not crop MP4s
if processDict['derivDetails'][derivCount]['frameSize'] == 2:
frameSizeString = "720x486"
processDict['crop'] = 2
elif processDict['derivDetails'][derivCount]['frameSize'] == 1:
frameSizeString = "640x480"
elif processDict['derivDetails'][derivCount]['frameSize'] == 3:
frameSizeString = "720x540"
# Figure out the video filter string, then add to the basepath
if processDict['crop'] == 1 and processDict['derivDetails'][derivCount]['doInterlace'] == 1: # if de-interlace and crop
videoFilterString = " -vf crop=720:480:0:4,yadif "
elif processDict['crop'] == 2 and processDict['derivDetails'][derivCount]['doInterlace'] == 1: # if de-interlace and no crop
videoFilterString = " -vf yadif "
elif processDict['crop'] == 1 and processDict['derivDetails'][derivCount]['doInterlace'] == 2: # if no de-interlace and crop
videoFilterString = " -vf crop=720:480:0:4 "
elif processDict['crop'] == 2 and processDict['derivDetails'][derivCount]['doInterlace'] == 2: # if no de-interlace and no crop
videoFilterString = " "
else:
videoFilterString = " "
else:
videoFilterString = ""
# Figure out the audio filter string, then add to the basepath
if processDict['derivDetails'][derivCount]['audioMap'] == 1: # keep original
audioFilterString = " "
elif processDict['derivDetails'][derivCount]['audioMap'] == 2: # pan left center
audioFilterString = " -af 'pan=stereo|c0=c0|c1=c0' "
elif processDict['derivDetails'][derivCount]['audioMap'] == 3: # pan right center
audioFilterString = " -af 'pan=stereo|c0=c1|c1=c1' "
elif processDict['derivDetails'][derivCount]['audioMap'] == 4: # sum stereo to mono
audioFilterString = " -af 'pan=stereo|c0=c0+c1|c1=c0+c1' "
else:
audioFilterString = " "
# Figure out basestring
if processDict['derivDetails'][derivCount]['mp3Kbps'] == 1:
mp3kpbs = "-b:a 320k"
elif processDict['derivDetails'][derivCount]['mp3Kbps'] == 2:
mp3kpbs = "-b:a 240k"
elif processDict['derivDetails'][derivCount]['mp3Kbps'] == 3:
mp3kpbs = "-b:a 160k"
elif processDict['derivDetails'][derivCount]['mp3Kbps'] == 4:
mp3kpbs = "-b:a 128k"
elif processDict['derivDetails'][derivCount]['mp3Kbps'] == 5:
mp3kpbs = "-q:a 2"
else:
mp3kpbs = "0"
if processDict['derivDetails'][derivCount]['derivType'] == 1: # Basestring for H264/MP4
baseString = " -c:v libx264 -pix_fmt yuv420p -movflags faststart -crf 18 -b:a 160000 -ar 48000 -aspect " + aspectRatioColon + " -s " + frameSizeString + " "
if videoFilterString == " ":
videoFilterString = "-vf setdar=" + aspectRatioSlash
elif videoFilterString == "":
videoFilterString = "-vf setdar=" + aspectRatioSlash
else:
videoFilterString = videoFilterString.replace("-vf ", "-vf setdar=" + aspectRatioSlash + ",")
#if "720x540" in baseString: #this was part of the NYU job, not necessary anymore
# processDict['derivDetails'][derivCount]['outPath'] = inPath.replace(processDict['masterExtension'],"_s.mp4")
#else:
if "_prsv" in inPath:
processDict['derivDetails'][derivCount]['outPath'] = inPath.replace("_prsv" + processDict['masterExtension'],"_access.HD.mp4")
else:
processDict['derivDetails'][derivCount]['outPath'] = inPath.replace(processDict['masterExtension'],"_access.mp4")
elif processDict['derivDetails'][derivCount]['derivType'] == 2: # Basestring for ProRes/MOV
baseString = " -c:v prores -profile:v 3 -c:a pcm_s24le -aspect " + aspectRatioColon + " -ar 48000 "
if videoFilterString == " ":
videoFilterString = "-vf setdar=" + aspectRatioSlash
elif videoFilterString == "":
videoFilterString = "-vf setdar=" + aspectRatioSlash
else:
videoFilterString = videoFilterString.replace("-vf ", "-vf setdar=" + aspectRatioSlash + ",")
processDict['derivDetails'][derivCount]['outPath'] = inPath.replace(processDict['masterExtension'],"_mezzanine.mov")
elif processDict['derivDetails'][derivCount]['derivType'] == 3: # Basestring for FFv1/MKV
baseString = " -c:v ffv1 -level 3 -g 1 -slices 16 -slicecrc 1 -color_primaries smpte170m -color_trc bt709 -colorspace smpte170m -color_range mpeg -metadata:s:v:0 'encoder= FFV1 version 3' -c:a copy -vf setfield=bff,setsar=40/27,setdar=4/3 -metadata creation_time=now -f matroska "
videoFilterString = "-vf setfield=bff,setdar=" + aspectRatioSlash
processDict['derivDetails'][derivCount]['outPath'] = inPath.replace(processDict['masterExtension'],".mkv")
elif processDict['derivDetails'][derivCount]['derivType'] == 5: # Basestring for MP3
baseString = " -c:a libmp3lame " + mp3kpbs + " -write_xing 0 -ac 2 "
processDict['derivDetails'][derivCount]['outPath'] = inPath.replace(".wav","_access.mp3")
elif processDict['derivDetails'][derivCount]['derivType'] == 6: # Basestring for M4A
baseString = " -c:a aac " + mp3kpbs + " -ac 2 -ar 44100 "
if "_prsv.wav" in inPath:
processDict['derivDetails'][derivCount]['outPath'] = inPath.replace("_prsv.wav","_access.m4a")
else:
processDict['derivDetails'][derivCount]['outPath'] = inPath.replace(".wav","_access.m4a")
ffmpeg_string = ffmpeg_string + baseString + videoFilterString + audioFilterString + " -y '" + processDict['derivDetails'][derivCount]['outPath'] + "' "
if processDict['createQCT'] == 1:
if videoCodec == "DV":
qctString = " && dvrescue '" + inPath + "' -x '" + inPath + ".dvrescue.xml' -c '" + inPath + ".dvrescue.scc' -s '" + inPath + ".dvrescue.vtt'"
else:
if processDict['MKVMaster'] == 1:
qctString = " && qcli -i '" + inPath.replace(".mov",".mkv") + "'"
else:
qctString = " && qcli -i '" + inPath + "'"
else:
qctString = ""
cmd = ffmpeg_string + qctString
return cmd
# Runs a command
def runCommand(cmd):
print(bcolors.OKGREEN + "Running Command: " + cmd + "\n" + bcolors.ENDC)
ffmpeg_out = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True).communicate()[0]
return
# Runs the command that will move files to PresRAID via rsync
def moveToBackup(inPathList, processDict, NoSalesForce):
if processDict['moveToPresRAID'] == 1:
print(bcolors.OKBLUE + "Moving to PresRAID!\n" + bcolors.ENDC)
inPathList_String = ""
for i in range(len(inPathList)):
inPathList_String = inPathList_String + "'" + inPathList[i] + "' "
rsync_command = "rsync -av " + inPathList_String + " /Volumes/presraid/" + processDict['presRaidFolderPath']
#runCommand(rsync_command)
run_rsync(rsync_command)
print(bcolors.OKBLUE + "Done Moving. Verifying Move Now\n" + bcolors.ENDC)
for i in range(len(inPathList)): # this cute little sections checks to see that the rysnc worked and the updates salesforce
inputFileSize = os.path.getsize(inPathList[i])
outputFileSize = os.path.getsize("/Volumes/presraid/" + processDict['presRaidFolderPath'] + "/" + os.path.basename(inPathList[i]))
if inputFileSize == outputFileSize:
barcode = getBarcode(inPathList[i])
if barcode is False:
print(bcolors.FAIL + "\nERROR: Barcode is malformed. Cannot update SalesForce info for file: " + os.path.basename(inPathList[i]) + "\n" + bcolors.ENDC)
else:
print(bcolors.OKGREEN + "\nFile size of original and PresRAID version match for Barcode: " + barcode + "\n" + bcolors.ENDC)
updateSalesForceFileBackup(barcode, NoSalesForce)
else:
print(bcolors.FAIL + "\nERROR: File size of original and PresRAID version did not match. Please investigate\n" + bcolors.ENDC)
return
else:
print(bcolors.OKBLUE + "Skipping Sync to PresRAID According to User-Selected Option\n" + bcolors.ENDC)
return
# Allows us to see the progress of rsync (on a file-by-file basis)
def run_rsync(command):
print(bcolors.OKGREEN + "Running Command: " + command + "\n\n" + bcolors.ENDC)
p = subprocess.Popen(shlex.split(command), stdout=subprocess.PIPE)
p.communicate()
# while True:
# output = process.stdout.readline()
# if output == '' and process.poll() is not None:
# break
# if output:
# print(bcolors.OKGREEN + output.strip().decode("utf-8") + bcolors.ENDC)