-
Notifications
You must be signed in to change notification settings - Fork 0
/
mmfunctions
1509 lines (1400 loc) · 61.4 KB
/
mmfunctions
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
# assign variables
SCRIPTNAME=$(basename "${0}")
SCRIPTDIR=$(dirname "${0}")
MM_CONFIG_FILE="${SCRIPTDIR}/mm.conf"
WHAT_IS_THIS="mediamicroservices"
# load configuration file
if [ -f "${MM_CONFIG_FILE}" ] ; then
. "${MM_CONFIG_FILE}"
elif [ ! "${CONFIG}" = "Y" -a "${REQUIRECONFIG}" = "Y" ] ; then
echo "The configuration file is not set. You must first create ${MM_CONFIG_FILE} by running mmconfig." 1>&2
exit 1
fi
#Setup differences for systems
if [[ "$(uname -s)" = "Darwin" ]] ; then
FFMPEGFONTPATH='/Library/Fonts/Andale Mono.ttf'
XMLSTARLET='xml'
elif [[ "$(uname -s)" = "Linux" ]] ; then
FFMPEGFONTPATH='/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf'
XMLSTARLET='xmlstarlet'
else
echo "Warning: Unsupported system detected. Certain configurations not set. This may cause errors for some scripts."
fi
_cleanup_mount_dir(){
if [ -d "${MOUNT_DIR}" ] ; then
echo calling umount
umount "${MOUNT_DIR}"
rmdir "${MOUNT_DIR}"
fi
}
_check_for_lto_md5_flags(){
if [ -z "${LTO_MD5_FLAGS}" ] ; then
LTO_MD5_FLAGS="md5deep -rel"
fi
}
_check_for_lto_index_dir(){
if [ -z "${LTO_INDEX_DIR}" ] ; then
LTO_INDEX_DIR="${HOME}/Documents/lto_indexes"
fi
}
_check_for_colons(){
COLONALERT=$(find "${1}" -iname '*:*')
if [ -n "${COLONALERT}" ] ; then
_report -w "Illegal characters (colons) have been detected in the following file(s):"
for i in ${COLONALERT} ; do
_report -w " ${i}"
done
_report -w "Exiting"
exit 1
fi
}
_escape_for_db(){
echo "${1}" | tr "’" "'"| sed "s/'/\''/g"
}
_report_to_db(){
if [ "${PREMIS_DB}" = "Y" ] ; then
eventDetail=$(basename "${0}")
if [ -z "${MEDIA_ID}" ] ; then
MEDIA_ID=$(basename "${INPUT}")
fi
LOGDIR="${OUTDIR_INGESTFILE}/${MEDIAID}/metadata/logs"
INGESTLOG="${LOGDIR}/capture.log"
if [ -f "${INGESTLOG}" ] ; then
OP=$(grep "operator" "${INGESTLOG}" | cut -c 11-)
else
OP="${OP}"
fi
if [ -z "${OP}" ] ; then
OP="${USER}"
fi
_premis_event_list
table_name="object"
echo "INSERT IGNORE INTO object (objectIdentifierValue,object_LastTouched) VALUES ('${MEDIA_ID}',NOW()) ON DUPLICATE KEY UPDATE object_LastTouched = NOW()" | mysql --login-path="${PREMIS_PROFILE}" "${PREMIS_NAME}" 2> /dev/null
_db_error_check
table_name="event"
echo "INSERT INTO event (objectIdentifierValue,eventType,eventDetail,eventDetailOPT,eventDetailCOMPNAME,linkingAgentIdentifierValue) VALUES ('${MEDIA_ID}','${eventType}','${eventDetail}','${user_input}','${HOSTNAME}', '${OP}')" | mysql --login-path="${PREMIS_PROFILE}" "${PREMIS_NAME}" 2> /dev/null
_db_error_check
if [ -n "${MEDIAINFO}" ] || [ -n "${CAPTURELOG}" ] ; then
table_name="objectCharacteristics"
echo "INSERT INTO objectCharacteristics (objectIdentifierValue,mediaInfo,captureLog) VALUES ('${MEDIA_ID}','${MEDIAINFO}','${CAPTURELOG}') ON DUPLICATE KEY UPDATE mediaInfo=COALESCE(NULLIF(mediaInfo,''),'${MEDIAINFO}'),captureLog=COALESCE(NULLIF(captureLog,''),'${CAPTURELOG}')" | mysql --login-path="${PREMIS_PROFILE}" "${PREMIS_NAME}" 2> /dev/null
_db_error_check
fi
fi
}
_report_schema_db(){
if [ "${PREMIS_DB}" = "Y" ] ; then
_premis_event_list
table_name="ltoSchema"
if [ -z "${xmlschema}" ] ; then
xmlschema="$LTO_LOGS/${TAPE_SERIAL}.schema"
fi
schema_sorted=$("${XMLSTARLET}" sel -t -m ".//file" -v "concat(name,'|',length, '|', modifytime)" -o "|" -m "ancestor-or-self::directory" -v "name" -o "/" -b -n "${xmlschema}")
schema_tape=$(basename "${xmlschema}" | cut -d'.' -f1)
IFS=$(echo -en "\n\b")
(for i in ${schema_sorted}; do LINE=$i;
schema_name=$(_escape_for_db $(echo "${LINE}" | cut -d'|' -f1))
schema_path=$(_escape_for_db $(echo "${LINE}" | cut -d'|' -f4))
schema_length=$(echo "${LINE}" | cut -d'|' -f2)
schema_date=$(echo "${LINE}" | cut -d'|' -f3)
echo "INSERT IGNORE INTO ltoSchema (ltoID,fileName,fileSize,modifyTime,filePath) VALUES ('${schema_tape}','${schema_name}','${schema_length}','$schema_date','${schema_path}${schema_name}')" | mysql --login-path="${PREMIS_PROFILE}" "${PREMIS_NAME}" 2> /dev/null
if [ "${?}" != "0" ]; then
sleep 1
echo "INSERT IGNORE INTO ltoSchema (ltoID,fileName,fileSize,modifyTime,filePath) VALUES ('${schema_tape}','${schema_name}','${schema_length}','$schema_date','${schema_path}${schema_name}')" | mysql --login-path="${PREMIS_PROFILE}" "${PREMIS_NAME}" || exit 1
fi
done)
unset IFS
_db_error_check
fi
}
_report_fixity_db(){
if [ "${PREMIS_DB}" = "Y" ] ; then
_premis_event_list
table_name="fixity"
if [ -z "${db_fixity}" ] ; then
echo -e "\033[1;31mNo fixity data found! Fixity information not reported to the database.\033[0m"
event_outcome="No fixity information"
fi
if [ -z "${lastrecord}" ] ; then
lastrecord=$(echo "SELECT eventIdentifierValue FROM event WHERE objectIdentifierValue = '${MEDIA_ID}' AND eventDetail = '${eventDetail}' ORDER BY eventIdentifierValue DESC LIMIT 1" | mysql --login-path="${PREMIS_PROFILE}" "${PREMIS_NAME}")
fi
EVENT_ID_TRIM=$(echo $lastrecord | cut -d " " -f2)
IFS=$(echo -en "\n\b")
(for i in ${db_fixity}; do LINE=$i;
messageDigestHASH=$(echo "$LINE" | cut -c -32 )
messageDigestPATH=$(_escape_for_db $(echo "$LINE" | cut -c 35- ))
messageDigestFILENAME=$(basename "${messageDigestPATH}")
echo "INSERT INTO fixity (eventIdentifierValue,objectIdentifierValue,eventDetail,messageDigestAlgorithm,messageDigestSOURCE,messageDigestPATH,messageDigestFILENAME,messageDigestHASH) VALUES ('${EVENT_ID_TRIM}','${MEDIA_ID}','${eventDetail}','md5','${db_source}','${messageDigestPATH}','${messageDigestFILENAME}','${messageDigestHASH}')" | mysql --login-path="${PREMIS_PROFILE}" "${PREMIS_NAME}"
if [ "${?}" != "0" ]; then
sleep 1
echo "INSERT INTO fixity (eventIdentifierValue,objectIdentifierValue,eventDetail,messageDigestAlgorithm,messageDigestSOURCE,messageDigestPATH,messageDigestFILENAME,messageDigestHASH) VALUES ('${EVENT_ID_TRIM}','${MEDIA_ID}','${eventDetail}','md5','${db_source}','${messageDigestPATH}','${messageDigestFILENAME}','${messageDigestHASH}')" | mysql --login-path="${PREMIS_PROFILE}" "${PREMIS_NAME}" || exit 1
fi
done)
_db_error_check
unset IFS
fi
}
_db_email_delivery(){
if [ "${PREMIS_DB}" = "Y" ] ; then
EMAILTO="${1}"
if [ "${EMAILTO}" ] ; then
temp_message=$(_maketemp)
if [[ -f "${REPORT_DUMP}" ]] ; then
echo -e "\033[1;103;95mZipping and emailing a copy of "${REPORT_DUMP} to to ${EMAILTO}"\033[0m"
gzip -f "${REPORT_DUMP}"
echo -e "Subject:Database insertion report for ${MEDIA_ID}\nThere was a problem with database reporting for the Media ID ${MEDIA_ID}.\n A database insertion report has been created at ${REPORT_DUMP}.gz on ${HOSTNAME}. A copy is attached to this email. \n\n" > "$temp_message"
uuencode "${REPORT_DUMP}".gz $(basename "${REPORT_DUMP}".gz) >> "$temp_message"
sendmail -f "${EMAIL_FROM}" -F "${EMAILTO}" "${EMAILTO}" < "$temp_message"
if [ -f "$temp_message" ] ; then
rm "$temp_message"
fi
fi
fi
fi
}
_eventoutcome_update(){
if [ "${PREMIS_DB}" = "Y" ] ; then
if [ -z "$event_outcome" ] ; then
event_outcome="Success"
fi
if [ -n "${RUN_ERR}" ] ; then
if [[ "${RUN_ERR}" != 0 ]] ; then
event_outcome="Fail"
fi
fi
lastrecord=$(echo "SELECT eventIdentifierValue FROM event WHERE objectIdentifierValue = '${MEDIA_ID}' AND eventDetail = '${eventDetail}' ORDER BY eventIdentifierValue DESC LIMIT 1" | mysql --login-path="${PREMIS_PROFILE}" "${PREMIS_NAME}")
lastrecord_trim=$(echo $lastrecord | cut -d " " -f2)
echo "update event set eventOutcome='${event_outcome}' WHERE eventIdentifierValue='${lastrecord_trim}'" | mysql --login-path="${PREMIS_PROFILE}" "${PREMIS_NAME}" 2> /dev/null
fi
}
_db_error_check(){
if [ "$?" != "0" ] ; then
ERROR_DATE=$(date +%Y%m%d)
REPORT_DUMP=~/Desktop/"${MEDIA_ID}"_"${ERROR_DATE}"_dbreport.sh
echo -e "\033[1;5;95mERROR:\033[0m \033[1;103;95mCould not access database "${PREMIS_NAME}". Information was not added to the "${table_name}" table. SQL command history has been written to "${REPORT_DUMP}"\033[0m"
if [ "$table_name" = "object" ] ; then
echo "echo \"INSERT IGNORE INTO object (objectIdentifierValue,object_LastTouched) VALUES ('${MEDIA_ID}',NOW()) ON DUPLICATE KEY UPDATE object_LastTouched = NOW()\" | mysql --login-path=\"${PREMIS_PROFILE}\" \"${PREMIS_NAME}\"" >> "${REPORT_DUMP}"
fi
if [ "${table_name}" = "fixity" ] ; then
IFS=$(echo -en "\n\b")
for i in ${db_fixity} ; do
LINE=$i;
messageDigestHASH=$(echo "$LINE" | cut -c -32 )
messageDigestPATH=$(_escape_for_db $(echo "$LINE" | cut -c 35- ))
messageDigestFILENAME=$(basename "${messageDigestPATH}")
echo "echo \"INSERT INTO fixity (eventIdentifierValue,objectIdentifierValue,eventDetail,messageDigestAlgorithm,messageDigestSOURCE,messageDigestPATH,messageDigestFILENAME,messageDigestHASH) VALUES ('${EVENT_ID_TRIM}','${MEDIA_ID}','${eventDetail}','md5','${db_source}','${messageDigestPATH}','${messageDigestFILENAME}','${messageDigestHASH}')\" | mysql --login-path=\"${PREMIS_PROFILE}\" \"${PREMIS_NAME}\"" >> "${REPORT_DUMP}"
done
unset IFS
fi
if [ "${table_name}" = "event" ] ; then
echo "echo \"INSERT INTO event (objectIdentifierValue,eventType,eventDetail,eventDetailOPT,eventDetailCOMPNAME,linkingAgentIdentifierValue) VALUES ('${MEDIAID}','${eventType}','${eventDetail}','${user_input}','${HOSTNAME}', '${OP}')\" | mysql --login-path=\"${PREMIS_PROFILE}\" \"${PREMIS_NAME}\"" >> "${REPORT_DUMP}"
fi
if [ "${table_name}" = "objectCharacteristics" ] ; then
echo "echo \"INSERT INTO objectCharacteristics (objectIdentifierValue,mediaInfo,captureLog,videoFingerprint,videoFingerprintSorted) VALUES ('${MEDIA_ID}','${MEDIAINFO}','${CAPTURELOG}','${VIDEOFINGERPRINT}','${VIDEOFINGERPRINT_SORTED}') ON DUPLICATE KEY UPDATE mediaInfo=COALESCE(NULLIF(mediaInfo,''),'${MEDIAINFO}'),captureLog=COALESCE(NULLIF(captureLog,''),'${CAPTURELOG}'),videoFingerprint=COALESCE(NULLIF(videoFingerprint,''),'${VIDEOFINGERPRINT}'),videoFingerprintSorted=COALESCE(NULLIF(videoFingerprintSorted,''),'${VIDEOFINGERPRINT_SORTED}')\" | mysql --login-path=\"${PREMIS_PROFILE}\" \"$PREMIS_NAME\"" >> "${REPORT_DUMP}"
fi
if [ "${table_name}" = "ltoSchema" ] ; then
echo "echo \"INSERT INTO ltoSchema (ltoID,fileName,fileSize,modifyTime,filePath) VALUES ('${schema_tape}','${schema_name}','${schema_length}','$schema_date','${schema_path}${schema_name}')\" | mysql --login-path=\"${PREMIS_PROFILE}\" \"${PREMIS_NAME}\"" >> "${REPORT_DUMP}"
fi
if [ "${table_name}" = "fingerprints" ] ; then
(IFS=$'\n'
for i in ${VIDEOFINGERPRINT} ; do
hash1=$(echo "$i" | cut -d':' -f3)
hash2=$(echo "$i" | cut -d':' -f4)
hash3=$(echo "$i" | cut -d':' -f5)
hash4=$(echo "$i" | cut -d':' -f6)
hash5=$(echo "$i" | cut -d':' -f7)
startframe=$(echo "$i" | cut -d':' -f1)
endframe=$(echo "$i" | cut -d':' -f2)
echo "echo \"INSERT INTO fingerprints (objectIdentifierValue,startframe,endframe,hash1,hash2,hash3,hash4,hash5) VALUES ('${MEDIA_ID}','${startframe}','${endframe}','${hash1}','${hash2}','${hash3}','${hash4}','${hash5}')\" | mysql --login-path=\"${PREMIS_PROFILE}\" \"${PREMIS_NAME}\"" >> "${REPORT_DUMP}"
done)
fi
else
echo -e "\033[1;103;95mSuccessfully reported infomation to the "${table_name}" table in the database "${PREMIS_NAME}"\033[0m"
fi
}
_premis_event_list(){
eventDetail=$(basename "${0}")
eventType="${eventDetail}"
if [ "$eventDetail" = "ingestfile" ] ; then
eventType="creation"
METADIR="${AIP_STORAGE}/${MEDIAID}/metadata"
MEDIAINFO=$(_escape_for_db "$(cat "${METADIR}/fileMeta/objects/${MEDIA_ID}_mediainfo.xml")")
CAPTURELOG=$(_escape_for_db "$(cat "${METADIR}/logs/capture.log")")
MEDIA_ID="${MEDIAID}"
fi
if [ "$eventDetail" = "makebroadcast" ] ; then
eventType="creation"
fi
if [ "$eventDetail" = "makeyoutube" ] ; then
eventType="creation"
fi
if [ "$eventDetail" = "makemp3" ] ; then
eventType="creation"
fi
if [ "$eventDetail" = "makedvd" ] ; then
eventType="creation"
fi
if [ "$eventDetail" = "makeframes" ] ; then
eventType="creation"
fi
if [ "$eventDetail" = "makewaveform" ] ; then
eventType="creation"
fi
if [ "$eventDetail" = "makefingerprint" ] ; then
eventType="creation"
MEDIA_ID=$(basename "${SOURCEFILE}")
fi
if [ "$eventDetail" = "migratefiles" ] ; then
eventType="replication"
fi
if [ "$eventDetail" = "checksumpackage" ] ; then
eventType="fixity"
LOGDIR="${ABS_PATH}/${MEDIAID}/metadata/logs"
INGESTLOG="${LOGDIR}/capture.log"
if [ -f "${INGESTLOG}" ] ; then
OP=$(grep "operator" "${INGESTLOG}" | cut -c 11-)
fi
if [ -z "${OP}" ] ; then
OP="${USER}"
fi
fi
if [ "$eventDetail" = "collectionchecksum" ] ; then
eventType="fixity"
db_fixity=$(cat "${TAPECHECKSUMFILE}")
db_source="${TAPECHECKSUMFILE}"
fi
if [ "$eventDetail" = "writelto" ] ; then
eventType="ingest"
MEDIA_ID="${TAPE_SERIAL}"
if [ "${VERIFY_CHECK}" = "Y" ] ; then
eventDetail="writelto(VERIFY)"
fi
fi
if [ "$eventDetail" = "verifylto" ] ; then
eventType="fixity"
MEDIA_ID="${TAPE_SERIAL}"
db_fixity=$(cat "${READBACKDIR}/${TAPE_SERIAL}_ReadBack_checksum_${VERIFYTIME}.md5")
db_source="${READBACKDIR}/${TAPE_SERIAL}_ReadBack_checksum_${VERIFYTIME}.md5"
fi
if [ "$eventDetail" = "ingestcollectionchecksum" ] ; then
eventType="ingest"
fi
#Escape ID for SQL
if [ -z ${ESCAPE_CHECK1} ] ; then
MEDIA_ID=$(_escape_for_db ${MEDIA_ID})
ESCAPE_CHECK1="Y"
fi
if [ -z ${ESCAPE_CHECK2} ] ; then
db_source=$(_escape_for_db ${db_source})
ESCAPE_CHECK2="Y"
fi
if [ -z ${ESCAPE_CHECK3} ] ; then
user_input=$(_escape_for_db ${user_input})
ESCAPE_CHECK3="Y"
fi
}
_open_mysql_plist(){
plist_path=$(find /usr/local/Cellar/mysql -iname homebrew.mxcl.mysql.plist | sort | tail -n1)
if [ -n "$plist_path" ] ; then
ip_address="0.0.0.0"
cat "$plist_path" | grep "$ip_address" > /dev/null
if [ "$?" != "0" ] ; then
sed -i '' "12s/.*/<string>--bind-address=$ip_address<\/string>/" "$plist_path"
fi
fi
}
_version_schema(){
SCHEMA_FILE="$LTO_LOGS/${TAPE_SERIAL}.schema"
if [ -f "${SCHEMA_FILE}" ] ; then
echo "${GREEN}Creating a new version of the ltfs schema file.${NC}"
LASTSCHEMA="${SCHEMA_FILE%.*}_$(stat -t '%Y%m%d-%H%M%S' -l "${SCHEMA_FILE}" | awk '{print $6}').schema"
mv -v "${SCHEMA_FILE}" "${LASTSCHEMA}"
echo "${GREEN}$(basename ${0}): schema file is versioned - ${SCHEMA_FILE} -> ${LASTSCHEMA}.${NC}"
fi
}
_checkdir(){
if [[ ! -d "${1}" ]] ; then
echo "${1}" is not a directory.
_usage
exit 1
fi
}
_get_iso8601(){
date +%FT%T
}
_get_iso8601_c(){
date +%Y%m%d-%H%M%S
}
_unset_ffreport(){
if [ "${FFREPORT}" ] ; then
unset FFREPORT
fi
}
_mkdir2(){
local DIR2MAKE=""
while [ "${*}" != "" ] ; do
DIR2MAKE="${1}"
if [ ! -d "${DIR2MAKE}" ] ; then
mkdir -p "${DIR2MAKE}"
if [ "${?}" -ne 0 ]; then
_report -wt "${0}: Can't create directory at ${DIR2MAKE}"
exit 1
fi
fi
shift
done
}
_writeerrorlog(){
if [ ! -d "${ERRORLOGS}" ] ; then
_mkdir2 ~/Desktop/ERRORLOGS/
touch ~/Desktop/ERRORLOGS/error.log
fi
ERRORLOG=~/Desktop/ERRORLOGS/error.log
KEY="${1}"
VALUE="${2}"
DATE=$(_get_iso8601_c)
echo "${MEDIAID}, ${DATE}, ${KEY}: ${VALUE}" >> "${ERRORLOG}"
}
_maketemp(){
mktemp -q "/tmp/$(basename "${0}").XXXXXX"
if [ "${?}" -ne 0 ]; then
echo "${0}: Can't create temp file, exiting..."
_writeerrorlog "_maketemp" "was unable to create the temp file, so the script had to exit."
exit 1
fi
}
_log(){
MMLOGNAME="mm.log"
MMLOGDIR="${CUSTOM_LOG_DIR:-/tmp}"
MMLOGFILE="${MMLOGDIR}/${MMLOGNAME}"
if [ ! -d "${MMLOGDIR}" ] ; then
_mkdir2 "${MMLOGDIR}"
if [ "${?}" -ne 0 ]; then
echo "${0}: Can't create log directory at ${MMLOGDIR}, exiting... Use mmconfig to change logging directory."
exit 1
fi
fi
OPTIND=1
while getopts ":beacw" OPT; do
case "${OPT}" in
b) STATUS="start" ;; # script is beginning
e) STATUS="end" ;; # script is ending
a) STATUS="abort" ;; # script is aborted
c) STATUS="comment" ;; # comment about what script is doing
w) STATUS="warning" ;; # warning information
esac
done
shift $(( ${OPTIND} - 1 ))
NOTE="${1}"
echo $(_get_iso8601)", $(basename "${0}"), ${STATUS}, ${OP}, ${MEDIAID}, ${NOTE}" >> "${MMLOGFILE}"
}
_report(){
local RED="$(tput setaf 1)" # Red - For Warnings
local GREEN="$(tput setaf 2)" # Green - For Declarations
local BLUE="$(tput setaf 4)" # Blue - For Questions
local NC="$(tput sgr0)" # No Color
local COLOR=""
local STARTMESSAGE=""
local ENDMESSAGE=""
local ECHOOPT=""
local LOG_MESSAGE=""
OPTIND=1
while getopts ":qdwstn" OPT; do
case "${OPT}" in
q) COLOR="${BLUE}" ;; # question mode, use color blue
d) COLOR="${GREEN}" ;; # declaration mode, use color green
w) COLOR="${RED}" ; LOG_MESSAGE="Y" ;; # warning mode, use color red
s) STARTMESSAGE+=([$(basename "${0}")] ) ;; # prepend scriptname to the message
t) STARTMESSAGE+=($(_get_iso8601) '- ' ) ;; # prepend timestamp to the message
n) ECHOOPT="-n" ;; # to avoid line breaks after echo
esac
done
shift $(( ${OPTIND} - 1 ))
MESSAGE="${1}"
echo "${ECHOOPT}" "${COLOR}${STARTMESSAGE[@]}${MESSAGE}${NC}"
[ "${LOG_MESSAGE}" = "Y" ] && _log -w "${MESSAGE}"
}
_test_config(){
for DIRECTORYVARIABLE in OUTDIR_INGESTFILE OUTDIR_INGESTXDCAM OUTDIR_PAPER AIP_STORAGE PODCASTDELIVER YOUTUBEDELIVER TMPDIR CUSTOM_LOG_DIR LTO_INDEX_DIR ; do
if [ -d "${!DIRECTORYVARIABLE}" ] ; then
_report -d "${DIRECTORYVARIABLE} is a valid directory"
else
_report -w "${DIRECTORYVARIABLE} is NOT a valid directory"
fi
done
}
_writeingestlog(){
if [ -z "${INGESTLOG}" ] ; then
echo "Error, can not write to log, ingest log not yet created"
exit
fi
if [ ! -f "${INGESTLOG}" ] ; then
_mkdir2 $(dirname "${INGESTLOG}")
touch "${INGESTLOG}"
fi
KEY="${1}"
VALUE="${2}"
# need to add yaml style escaping
echo "${KEY}: ${VALUE}" >> "${INGESTLOG}"
}
_readingestlog(){
if [ -f "${INGESTLOG}" ] ; then
KEY="${1}"
# need to add yaml style escaping
grep "^${1}:" "${INGESTLOG}" | tail -n1 | cut -d: -f2- | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'
fi
}
_run(){
RUN_ERR=""
_report -sdt "Running: ${*}"
if [[ ! "${DRYRUN}" == true ]] ; then
"${@}"
fi
RUN_ERR="${?}"
if [[ "${RUN_ERR}" != 0 ]] ; then
_report -wts "Error: Running: \"${*}\" gave an Error Code - ${RUN_ERR}"
_writeerrorlog "_run" "Running: \"${*}\" gave an Error Code - ${RUN_ERR}"
fi
}
_run_critical(){
_run "${@}"
if [[ "${RUN_ERR}" != 0 ]] ; then
_report -wts "The process ran into a critical error and can not proceed."
exit 1
fi
}
_run_critical_event(){
_run "${@}"
_eventoutcome_update
if [[ "${RUN_ERR}" != 0 ]] ; then
_report -wts "The process ran into a critical error and can not proceed."
exit 1
fi
}
_black_at_ends(){
INPUT_MOVIE="${1}"
ANALYSIS_HEAD_SECONDS=10
ANALYSIS_TAIL_SECONDS=-10
_report -dt "Analyzing $(basename "${INPUT_MOVIE}") for excessive black at head or tail."
HEAD_BLACK=$(ffmpeg -t "${ANALYSIS_HEAD_SECONDS}" -i "${1}" -an -filter_complex blackdetect=pix_th=0.05 -loglevel debug -f null - 2>&1 | grep -c -o picture_black_ratio:1)
_report -dt "Black frames in first ${ANALYSIS_HEAD_SECONDS} seconds: ${HEAD_BLACK}."
TAIL_BLACK=$(ffmpeg -sseof "${ANALYSIS_TAIL_SECONDS}" -i "${1}" -an -filter_complex blackdetect=pix_th=0.05 -loglevel debug -f null - 2>&1 | grep -c -o picture_black_ratio:1)
_report -dt "Black frames in last ${ANALYSIS_HEAD_SECONDS} seconds: ${TAIL_BLACK}."
}
_get_frame_count(){
INPUT_MOVIE="${1}"
CONTAINERFRAMECOUNT=$(mediainfo --inform="Video;%FrameCount%" "${INPUT_MOVIE}")
VIDEOTRACKFRAMECOUNT=$(mediainfo --inform="Video;%Source_FrameCount%" "${INPUT_MOVIE}")
if [ ! -z "${VIDEOTRACKFRAMECOUNT}" ] ; then
if [ "${VIDEOTRACKFRAMECOUNT}" -ne "${CONTAINERFRAMECOUNT}" ] ; then
_report -dt "The frame count according to the container is: ${CONTAINERFRAMECOUNT}"
_report -dt "The frame count according to the video track is: ${VIDEOTRACKFRAMECOUNT}"
_report -wt "warning - there are discrepancies between the container frame count and video track frame count. This could result in the desynchronization of audio and video."
echo "Enter q to quit, any other key to continue: "
read A2
[[ "${A2}" = "q" ]] && exit 0
fi
fi
}
_ask_operator(){
if [ -z "${OP}" ] ; then
_report -qn "Enter the name of the operator or 'q' to quit: "
read -e OP
[ -z "${OP}" ] && _ask_operator
[[ "${OP}" = "q" ]] && exit 0
fi
}
_ask_mediaid(){
if [ -z "${MEDIAID}" ] ; then
_report -qn "Enter a unique MEDIA ID: "
read -e MEDIAID
[ -z "${MEDIAID}" ] && _ask_mediaid
# option to quit
[[ "${MEDIAID}" = "q" ]] && exit 0
# validate id and perhaps fail with exit
[ -z "${MEDIAID}" ] && { _report -wt "ERROR You must enter a valid MEDIA ID" ; exit ;};
[ ! -z $(echo "${MEDIAID}" | grep -v "^[A-Z0-9_-]*$") ] && { _report -wt "ERROR The MEDIA ID must only contain capital letters, numbers, hyphen and underscore" ; exit 1 ;};
fi
}
_ask_input(){
if [ -z "${INPUT}" ] ; then
_report -qn "Drag in the file: "
read -e INPUT
[ -z "${INPUT}" ] && _ask_input
[[ "${INPUT}" = "q" ]] && exit 0
basename=$(basename "${INPUT}")
fi
}
_ask_digitization_logs(){
_report -qn "Drag in any logs from digitization: "
read -e -a FILES
[[ "$FILES[0]" = "q" ]] && exit 0
[ ! -z "${FILES}" ] && log+="digitization_logs: ${FILES}\n"
}
_reset_directory(){
if [ "${PRESERVATION_MODE}" == "Y" ] ; then
OUTDIR_INGESTFILE=$(dirname "${INPUT}")
_report -dt "I'm going to place the AIP in the same directory as the original input file."
fi
}
_ask_intime(){
# TIME_REGEX tests for either S.mmm or HH:MM:SS.mmm time formats where HH is two digit hour, MM is two digit minute, S is number of seconds, SS is two digit seconds, and .mmm is milliseconds from between 0 and 3 decimals
TIME_REGEX="^\([0-9]\+\(\.[0-9]\{1,3\}\)\?\|[0-9]\{2\}:[0-5][0-9]:[0-5][0-9]\(\.[0-9]\{1,3\}\)\?\)$"
while [[ ! $(echo "${INTIME}" | grep "${TIME_REGEX}") ]] ; do
_report -q "Enter point of time to start transcoding."
_report -q "Enter no value if no intime for transcoding is needed. Transcoding will then start from the beginning."
_report -q "Must be in HH:MM:SS.mmm or S.mmm format. Note mmm is milliseconds and not frames."
_report -qn "INTIME: "
read INTIME
if [[ "${INTIME}" = "" ]] ; then
break
elif [[ ! $(echo "${INTIME}" | grep "${TIME_REGEX}") ]] ; then
_report -w "In time must be in seconds or in HH:MM:SS.mmm format."
fi
done
}
_ask_outtime(){
# TIME_REGEX tests for either S.mmm or HH:MM:SS.mmm time formats where HH is two digit hour, MM is two digit minute, S is number of seconds, SS is two digit seconds, and .mmm is milliseconds from between 0 and 3 decimals
TIME_REGEX="^\([0-9]\+\(\.[0-9]\{1,3\}\)\?\|[0-9]\{2\}:[0-5][0-9]:[0-5][0-9]\(\.[0-9]\{1,3\}\)\?\)$"
while [[ ! $(echo "${OUTTIME}" | grep "${TIME_REGEX}") ]] ; do
_report -q "Enter point of time to stop transcoding."
_report -q "Enter no value if no outtime for transcoding is needed. Transcoding will proceed to the end."
_report -q "Must be in HH:MM:SS.mmm or S.mmm format. Note mmm is milliseconds and not frames."
_report -qn "OUTTIME: "
read OUTTIME
if [[ "${OUTTIME}" = "" ]] ; then
break
elif [[ ! $(echo "${OUTTIME}" | grep "${TIME_REGEX}") ]] ; then
_report -w "Out time must be in seconds or in HH:MM:SS.mmm format."
fi
done
}
_check_dependencies(){
DEPS_OK=YES
while [ "${*}" != "" ] ; do
DEPENDENCY="${1}"
if [ ! $(which "${DEPENDENCY}") ] ; then
_report -wt "This script requires ${DEPENDENCY} to run but it is not installed"
_report -wt "If you are running ubuntu or debian you might be able to install ${DEPENDENCY} with the following command"
_report -wt "sudo apt-get install ${DEPENDENCY}"
_report -wt "If you are running mac you might be able to install ${DEPENDENCY} with the following command"
_report -wt "brew install ${DEPENDENCY}"
DEPS_OK=NO
fi
shift
done
if [[ "${DEPS_OK}" = "NO" ]]; then
_report -wt "Unmet dependencies"
_report -wt "Aborting!"
exit 1
else
return 0
fi
}
_initialize_make(){
DELIVERDIR=""
DRYRUN=false
EMAILADDRESS=""
OUTPUTDIR_FORCED=""
_cleanup(){
_log -a "Process aborted"
echo
_report -wts "THE PROCESS WAS ABORTED"
exit 1
}
trap _cleanup SIGHUP SIGINT SIGTERM
}
_check_deliverdir(){
if [ ! -d "${DELIVERDIR}" ] ; then
_report -wt "The delivery directory, ${DELIVERDIR}, does not exist. Can not deliver the OUTPUT of $(basename "${0}")."
_writeerrorlog "_checkdeliverdir" "The specified delivery directory does not exist, and the output could not be delivered."
fi
}
_check_outputdir_forced(){
if [ ! -d "${OUTPUTDIR_FORCED}" ] ; then
_report -wt "The directory, ${OUTPUTDIR_FORCED}, does not exist. Can not write the output of $(basename "${0}")."
_writeerrorlog "_check_outputdir_forced" "The specified directory does not exist, and the output could not be delivered."
fi
}
_check_emailaddress(){
EMAILREGEX="^((\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)*([,])*)*$"
if ! $(echo "${1}" | grep -Eq "^((\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)*([,])*)*$") ; then
_report -wt "${1} is not a valid email address."
_writeerrorlog "_check_emailaddress" "The email address provided was not a valid email address."
fi
}
_get_prettyduration(){
PRETTYDURATION=$(ffprobe "${1}" -show_format -pretty 2>/dev/null | grep "^duration=" | cut -d= -f2)
}
_get_duration(){
DURATION=$(ffprobe "${1}" -show_format 2>/dev/null | grep "^duration=" | cut -d = -f 2)
}
_get_broadcastduration(){
BROADCASTDURATION=$(mediainfo --inform="General;%Duration_String4%" "${1}")
}
_get_seconds(){
FILESECONDS=$(ffprobe "${1}" -show_format 2>/dev/null | grep "^duration=" | cut -d= -f2)
}
_email_delivery(){
EMAILTO="${1}"
if [ "${EMAILTO}" ] ; then
_get_prettyduration "${OUTPUT}"
temp_message=$(_maketemp)
if [[ -f "${INGESTLOG}" ]] ; then
FROMLOG="Operator: $(_readingestlog "operator")\n
Processing Computer: $(_readingestlog "computer_name")
Audio True Peak (dBTP): $(_readingestlog "measured_true_peak")
Integrated Loudness (LUFS): $(_readingestlog "r128_integrated_loudness")
Loudness Adjustment (LUFS): $(_readingestlog "loudness_adjustment")
Loudness Range (LU): $(_readingestlog "r128_loudness_range")
Threshold (LUFS) : $(_readingestlog "measured_threshold")
Out of Phase Audio Ranges: $(_readingestlog "Out_of_phase_audio_ranges")"
fi
echo -e "Subject: [delivery] $(basename "${OUTPUT}")\n
A file has been delivered to ${DELIVERDIR}.\n
Process: $(basename "${0}")
MediaID: ${MEDIAID}\n
Filename: ${OUTPUT}
Sourcefile: ${SOURCEFILE}
Duration: ${PRETTYDURATION}\n
Decoding_options: ${INPUTOPTIONS[@]}
Encoding_options: ${MIDDLEOPTIONS[@]}\n
Delivery Exit Status: ${DELIVER_EXIT_STATUS}\n
${FROMLOG}
\n
Enjoy!\n\n" > "$temp_message"
waveform_image="${OUTDIR_INGESTFILE}/${MEDIAID}/metadata/depictions/${MEDIAID}_waveform.png"
if [ -f "${waveform_image}" ] ; then
_report -d "Adding a waveform to the email from ${waveform_image}"
# email solution borrowed from http://stackoverflow.com/a/8063489
# --- generated values ---
BOUNDARY="unique-boundary-$RANDOM"
BODY_MIMETYPE=$(file -Ib "$temp_message" | cut -d";" -f1) # detect mime type
ATT_MIMETYPE=$(file -Ib "${waveform_image}" | cut -d";" -f1) # detect mime type
ATT_ENCODED=$(base64 < "${waveform_image}") # encode attachment
# --- generate MIME message and pipe to sendmail ---
cat <<EOF | sendmail -f "${EMAIL_FROM}" -F "${EMAILTO}" "${EMAILTO}"
MIME-Version: 1.0
From: "${EMAIL_FROM}"
To: "${EMAILTO}"
Subject: [delivery] $(basename "${OUTPUT}")
Content-Type: multipart/mixed; boundary="$BOUNDARY"
--$BOUNDARY
Content-Type: $BODY_MIMETYPE
Content-Disposition: inline
$(cat "$temp_message")
--$BOUNDARY
Content-Type: $ATT_MIMETYPE; name="$(basename "${waveform_image}")"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="$(basename "${waveform_image}")"
$ATT_ENCODED
--$BOUNDARY
EOF
else
sendmail -f "${EMAIL_FROM}" -F "${EMAILTO}" "${EMAILTO}" < "$temp_message"
fi
if [ -f "$temp_message" ] ; then
rm "$temp_message"
fi
fi
}
_deliver_output(){
# argument 1 if used should be the email to report delivery to
EMAILTO="${1}"
if [ "${DELIVERDIR}" ] ; then
echo DELIVERING OUTPUT ACTIVITED with "${DELIVERDIR}"
_report -dt "Delivering ${OUTPUT} to ${DELIVERDIR}"
_run cp -av "${OUTPUT}" "${DELIVERDIR}/"
DELIVER_EXIT_STATUS="${?}"
_email_delivery "${EMAILTO}"
fi
}
_ask(){
# This function requires 2 arguments
# 1) A prompt
# 2) The label for the metadata value
read -e -p "${1}" RESPONSE
if [ -z "${RESPONSE}" ] ; then
_ask "${1}" "${2}"
else
LOG+="${2}: ${RESPONSE}\n"
fi
echo
}
_offerChoice(){
# This function requires 3 arguments
# 1) A prompt
# 2) The label for the metadata value
# 3) A vocabulary list
PS3="${1}"
LABEL="${2}"
eval set "${3}"
select OPTION in "${@}" ; do
break
done
LOG+="${LABEL}: ${OPTION}\n"
echo
}
_make_mezz_for_xdcam(){
SOM="${1}"
DUR="${2}"
TMC_MS=$(mediainfo --inform="Video;%Delay%" "${3}")
TMC_SMP=$(mediainfo --inform="Video;%Delay/String3%" "${3}")
TMC_SEC=$(echo "${TMC_MS} * 0.001" | bc)
SOM_H=$(echo "${SOM}" | cut -c 1-2)
SOM_M=$(echo "${SOM}" | cut -c 4-5)
SOM_S=$(echo "${SOM}" | cut -c 7-8)
SOM_F=$(echo "${SOM}" | cut -c 10-11)
SOM_FC=$(echo "scale=3; (((((3600 * ${SOM_H})+(60 * ${SOM_M})+ ${SOM_S} ) * 30)+ ${SOM_F} ) - ( 2 * (((60 * ${SOM_H})+ ${SOM_M} ) - (((60 * ${SOM_H})+ ${SOM_M} ) / 10 )))) / 29.97" | bc)
DUR_H=$(echo "${DUR}" | cut -c 1-2)
DUR_M=$(echo "${DUR}" | cut -c 4-5)
DUR_S=$(echo "${DUR}" | cut -c 7-8)
DUR_F=$(echo "${DUR}" | cut -c 10-11)
DUR_FC=$(echo "scale=3; (((((3600 * ${DUR_H})+(60 * ${DUR_M})+ ${DUR_S} ) * 30)+ ${DUR_F} ) - ( 2 * (((60 * ${DUR_H})+ ${DUR_M} ) - (((60 * $DUR_H)+ $DUR_M ) / 10 )))) / 29.97" | bc)
REL_START=$(echo "scale=3; ${SOM_FC} - ${TMC_SEC}" | bc)
pushd $(dirname "${4}")
_report -dt "Starting ffmpeg to trim mxf file at $(date) This will take a few minutes..."
ffmpeg 2</dev/null -report -y -ss "${REL_START}" -t "${DUR_FC}" -i "${3}" -map 0:v -map 0:a:0 -map 0:a:1 -c copy "${4}"
popd
}
_add_video_filter(){
OPTIND=1
unset ADDASPREFIX
while getopts ":p" OPT ; do
case "${OPT}" in
p) ADDASPREFIX=true ;;
esac
done
shift $(( ${OPTIND} - 1 ))
local FILTER2ADD="${1}"
if [[ -n "${FILTER2ADD}" ]] ; then
if [[ -n "${next_video_filter_prefix}" ]] ; then
FILTER2ADD="${next_video_filter_prefix}${FILTER2ADD}"
unset next_video_filter_prefix
fi
if [[ -z "${VIDEOFILTERCHAIN}" ]] ; then
VIDEOFILTERCHAIN="${FILTER2ADD}"
elif [[ "${ADDASPREFIX}" = true ]] ; then
if [[ "${FILTER2ADD: -1}" = ";" || "${FILTER2ADD: -1}" = "," ]] ; then
VIDEOFILTERCHAIN="${FILTER2ADD}${VIDEOFILTERCHAIN}"
else
VIDEOFILTERCHAIN="${FILTER2ADD},${VIDEOFILTERCHAIN}"
fi
else
if [[ "${VIDEOFILTERCHAIN: -1}" = ";" || "${VIDEOFILTERCHAIN: -1}" = "," ]] ; then
VIDEOFILTERCHAIN="${VIDEOFILTERCHAIN}${FILTER2ADD}"
else
VIDEOFILTERCHAIN="${VIDEOFILTERCHAIN},${FILTER2ADD}"
fi
fi
fi
}
_add_audio_filter(){
OPTIND=1
unset ADDASPREFIX
while getopts ":p" OPT ; do
case "${OPT}" in
p) ADDASPREFIX=true ;;
esac
done
shift $(( ${OPTIND} - 1 ))
local FILTER2ADD="${1}"
if [[ -n "${FILTER2ADD}" ]] ; then
if [[ -n "${next_audio_filter_prefix}" ]] ; then
FILTER2ADD="${next_audio_filter_prefix}${FILTER2ADD}"
unset next_audio_filter_prefix
fi
if [[ -z "${AUDIOFILTERCHAIN}" ]] ; then
AUDIOFILTERCHAIN="${FILTER2ADD}"
elif [[ "${ADDASPREFIX}" = true ]] ; then
if [[ "${FILTER2ADD: -1}" = ";" || "${FILTER2ADD: -1}" = "," ]] ; then
AUDIOFILTERCHAIN="${FILTER2ADD}${AUDIOFILTERCHAIN}"
else
AUDIOFILTERCHAIN="${FILTER2ADD},${AUDIOFILTERCHAIN}"
fi
else
if [[ "${AUDIOFILTERCHAIN: -1}" = ";" || "${AUDIOFILTERCHAIN: -1}" = "," ]] ; then
AUDIOFILTERCHAIN="${AUDIOFILTERCHAIN}${FILTER2ADD}"
else
AUDIOFILTERCHAIN="${AUDIOFILTERCHAIN},${FILTER2ADD}"
fi
fi
fi
}
_find_input (){
CONCATSOURCE=""
SOURCEFILE=""
ISOBJECT=""
unset FFMPEGINPUT
EXPECTEDSERVICEFILE="${1}/objects/service/$(basename "${1}").mov"
if [ -f "${1}" ] ; then
# if the input is a file then just `-i file`
SOURCEFILE="${1}"
FFMPEGINPUT+=(-i)
FFMPEGINPUT+=("${SOURCEFILE}")
elif [ -d "${1}/objects" ] ; then
if [[ -s "${EXPECTEDSERVICEFILE}" && "${ADDSLATE}" != "Y" ]] && ! [[ "$(basename "${0}")" = 'makeyoutube' ]] ; then
# if the expectedservicefile exists and we should add a slate, then use the `-i expectedservicefile`
ISOBJECT="N"
SOURCEFILE="${EXPECTEDSERVICEFILE}"
FFMPEGINPUT+=(-i)
FFMPEGINPUT+=("${SOURCEFILE}")
else
ISOBJECT="Y"
INPUTFILES=$(_maketemp)
# find av files in a directory and output to a temp list
find "${1}/objects" -type f -size +0 ! -path "*/access/*" ! -path "*/service/*" ! -name ".DS_Store" | while read file ; do
streamcount=$(ffprobe -loglevel quiet "$file" -show_entries format=nb_streams -of default=nw=1:nk=1)
duration_ts=$(ffprobe -loglevel quiet "$file" -show_entries stream=duration_ts -of default=nw=1:nk=1)
if [[ "$streamcount" > 0 && "${duration_ts}" != 1 ]] ; then
_report -d "Input file: $file"
echo "$file" >> "${INPUTFILES}"
fi
done
INPUTFILECOUNT=$(wc -l "${INPUTFILES}" | awk '{print $1}')
if [[ "${INPUTFILECOUNT}" = 0 ]] ; then
_report -w "Error no audiovisual input files were found."
_writeerrorlog "_find_input" "A valid source audiovisual file was not found, so the script could not proceed."
exit 1
elif [[ "${INPUTFILECOUNT}" = 1 ]] ; then
SOURCEFILE=$(cat "${INPUTFILES}" | head -n 1)
FFMPEGINPUT+=(-i)
FFMPEGINPUT+=("${SOURCEFILE}")
else
VOLADJUST="N"
INPUTOPTIONS+=(-f concat)
INPUTOPTIONS+=(-safe 0)
FFCONCATFILE=$(_maketemp)
while read i ; do
duration4cat=$(ffprobe -loglevel quiet "${i}" -show_entries format=duration -of default=nw=1:nk=1)
printf "file " >> "${FFCONCATFILE}"
printf '%q' "${i}" >> "${FFCONCATFILE}"
echo >> "${FFCONCATFILE}"
echo "duration $duration4cat" >> "${FFCONCATFILE}"
done < "${INPUTFILES}"
SOURCEFILE=$(cat "${INPUTFILES}" | head -n 1)
FFMPEGINPUT+=(-i)
FFMPEGINPUT+=("${FFCONCATFILE}")
fi
fi
else
_report -wt "A valid source file isn't found."
_writeerrorlog "_find_input" "A valid source file was not found, so the script could not proceed."
exit 1
fi
LOG+="SOURCEFILE: ${SOURCEFILE}\n"
# handle exceptions for certain types of source files
if [[ "${SOURCEFILE#*.}" = "iso" ]] ; then
_report -dt "Detecting iso input and hoping its a dvd image."
if [[ "$(uname -s)" = "Darwin" ]] ; then
MOUNTPATH=$(hdiutil mount "${SOURCEFILE}" | cut -d " " -f 2- | sed 's/^[ \t]*//')
_report -dt "Mounting iso at ${MOUNTPATH}"
else
MOUNTPATH="/tmp/temporary_dvd_path"
rm -rfv "${MOUNTPATH}"
if [ ! -d "${MOUNTPATH}" ] ; then
mkdir -p "${MOUNTPATH}"
fi
7z e -r -o"${MOUNTPATH}" "${SOURCEFILE}"
fi
OLDSOURCEFILE="${SOURCEFILE}"
CONCATSOURCE="concat:$(find "${MOUNTPATH}" -iname "VTS*[1-9].VOB" | sort | sed -e :a -e '$!N;s/\n/|/;ta')"
SOURCEFILE=$(find "${MOUNTPATH}" -iname "VTS_*_1.VOB" | head -1)
_report -dt "Using ${CONCATSOURCE} for transcoding."
_report -dt "Using ${SOURCEFILE} for assessment."
_report -wt "Extracting to ${MOUNTPATH}. Please delete ${MOUNTPATH} later."
if [[ "${CONCATSOURCE}" = "" ]] ; then
_report -wt "ERROR: Can not mount iso or find vob files to concatenate within ${OLDSOURCEFILE}. Exiting."
_writeerrorlog "_find_input" "Could not mount iso or find vob files to concatenate within ${OLDSOURCEFILE}."
exit
fi
elif [[ "${SOURCEFILE#*.}" = "mkv" ]] ; then
# if the sorucefile is an mkv, then check if it has a Presentation Chapter Edition
if [[ -n $(mkvextract tags "${SOURCEFILE}" | "${XMLSTARLET}" sel -t -m "/Tags/Tag/Simple[String='Presentation']" -v String) ]] ; then
_report -dt "The input file ($(basename "${SOURCEFILE}")) contains a Chapter Edition called 'Presentation', will use that for transcoding."
LISTCHAPTERS=$(mkvextract chapters "${SOURCEFILE}" | "${XMLSTARLET}" sel -t -m Chapters/EditionEntry[EditionFlagDefault='1']/ChapterAtom -v ChapterTimeStart -o "-" -v ChapterTimeEnd -o "-" -v ChapterSegmentUID -n -)
SegmentUIDs=$(mkvextract chapters "${SOURCEFILE}" | "${XMLSTARLET}" sel -t -m Chapters/EditionEntry[EditionFlagDefault='1']/ChapterAtom/ChapterSegmentUID -v . -n | sort -u)
if [[ -n "${LISTCHAPTERS}" ]]; then
_report -d "Found this chapter list:"
_report -d "${LISTCHAPTERS}"
unset FFMPEGINPUT
INPUTOPTIONS+=(-f concat)
INPUTOPTIONS+=(-safe 0)
FFCONCATFILE=$(_maketemp)
while read chapter ; do
STARTTIME=$(echo "${chapter}" | cut -d- -f1)
ENDTIME=$(echo "${chapter}" | cut -d- -f2)
CHAPSEGMENTUID=$(echo "${chapter}" | cut -d- -f3)
if [[ -n "${CHAPSEGMENTUID}" ]]; then
MKVFOLDER=$(dirname "${SOURCEFILE}")
while read file ; do
FILESEGMENTUID=$(mkvinfo "${file}" | grep "Segment UID:" | cut -d : -f2 | sed 's/0x//g;s/ //g')