forked from NMAAHC/nmaahcmm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getduration
executable file
·82 lines (77 loc) · 3.77 KB
/
getduration
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
#!/usr/bin/env bash
# getduration
# microservice to calculate the duration of footage digitized
# load nmaahcmmfunctions into this script
SCRIPT_PATH="${0%/*}"
. "${SCRIPT_PATH}/nmaahcmmfunctions"
[[ -f "${SCRIPT_PATH}/nmaahcmmfunctions" ]] || { echo "Missing '${SCRIPT_PATH}/nmaahcmmfunctions'. Exiting." ; exit 1 ;};
_initialize_make # safe script termination process defined in nmaahcmmfunctions
DEPENDENCIES=(mediainfo) # list dependencies required by script
TOTAL_DURATION_MILLISECONDS=0
REQUIRECONFIG="N"
## USAGE
echo "0 is $0"
_usage(){
echo
echo "$(basename "${0}")"
echo "This script will calculate the total duration of your audio and video files. Duration is reported in HH:MM:SS."
echo "Running the script without selecting any options will report just the total duration of all media files supplied."
echo "You may supply a file or a directory. The script will ignore any files without a duration reported by MediaInfo."
echo "Dependencies: ${DEPENDENCIES[@]}"
echo "Usage: $(basename ${0}) [ -options ] fileordirectory1 [ fileordirectory2 ... ]"
echo " -c CSV output: generate a CSV file with durations for each file (format: 'filename,duration')"
echo " -v verbose: report durations for each file out to the terminal"
echo " -h display this help"
echo
exit
}
# getopts loop
OPTIND=1
while getopts ":cvh" OPT; do
case "${OPT}" in
c) CSV_CHOICE="Yes" ;;
v) VERBOSE_CHOICE="Yes" ;;
h) _usage ;; # if the operator runs "[scriptname] -h" then the _usage text above will display in the terminal
*) echo "Invalid option -${OPTARG}" ; _usage ;; # if the operator tries to use an option other than the ones listed above, the _usage text will display in the terminal
esac
done
shift $(( ${OPTIND} - 1 ))
## SCRIPT ACTIONS
TOTAL_DURATION=0
CSV="$(basename "${0}")_$(date +%F-%H%M%S).csv"
while [ "${*}" != "" ] ; do
INPUT="${1}" #set $INPUT as the first argument sent to script
if [[ -f "${INPUT}" ]] ; then
# get duration in milliseconds using mediainfo
DURATION_MILLISECONDS=$(mediainfo --Inform="General;%Duration%" "${INPUT}")
elif [[ -d "${INPUT}" ]] ; then
# look for all files; exclude some types of text file that might hold mediainfo output (and which will consequently report a false "duration")
for FILE in $(find "${INPUT}" -type f -not -iname *.xml* -not -iname *.txt) ; do
INPUT="${FILE}"
DURATION_MILLISECONDS=$(mediainfo --Inform="General;%Duration%" "${INPUT}")
# if there is a duration associated with the file, add it to the total duration for all files
done
fi
if [[ "${DURATION_MILLISECONDS}" -gt 0 ]] ; then
TOTAL_DURATION_MILLISECONDS=$(( ${TOTAL_DURATION_MILLISECONDS} + ${DURATION_MILLISECONDS} ))
# convert milliseconds to seconds for reporting purposes
DURATION_SECONDS=$(( ${DURATION_MILLISECONDS} / 1000 ))
# if operator selected verbose mode, report out to terminal
if [[ "${VERBOSE_CHOICE}" == "Yes" ]] ; then
echo "File ${INPUT} duration: $(_seconds_to_hhmmss ${DURATION_SECONDS})"
fi
# if operator selected CSV mode, append to CSV
if [[ "${CSV_CHOICE}" == "Yes" ]] ; then
echo "${INPUT},$(_seconds_to_hhmmss ${DURATION_SECONDS})" >> "${CSV}"
fi
fi
shift
done
# convert milliseconds to seconds (this is done again at the end so that rounding errors don't compound too much)
TOTAL_DURATION_SECONDS=$(( ${TOTAL_DURATION_MILLISECONDS} / 1000 ))
# convert seconds to timecode - function defined in nmaahcmmfunctions
_report -g "Total duration HH:MM:SS is:"
_seconds_to_hhmmss "${TOTAL_DURATION_SECONDS}"
if [[ "${CSV_CHOICE}" == "Yes" ]] ; then
_report -g "CSV report can be found at ${CSV}. Type 'open ${CSV}' to open."
fi