-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathchapter-csv
executable file
·98 lines (75 loc) · 3.04 KB
/
chapter-csv
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
#!/bin/sh
#===============================================================================
# chapter-csv
# convert a csv file into a chapter metadata file for ffmpeg
#===============================================================================
# dependencies:
# awk
#===============================================================================
# script usage
#===============================================================================
usage () {
# if argument passed to function echo it
[ -z "${1}" ] || echo "! ${1}"
# display help
echo "\
convert a csv file into a chapter metadata file for ffmpeg
$(basename "$0") -i input -o output"
exit 2
}
#===============================================================================
# error messages
#===============================================================================
INVALID_OPT_ERR='Invalid option:'
REQ_ARG_ERR='requires an argument'
WRONG_ARGS_ERR='wrong number of arguments passed to script'
#===============================================================================
# check the number of arguments passed to the script
#===============================================================================
[ $# -gt 0 ] || usage "${WRONG_ARGS_ERR}"
#===============================================================================
# getopts check the options passed to the script
#===============================================================================
while getopts ':i:o:h' opt
do
case ${opt} in
i) input="${OPTARG}";;
h) usage;;
o) output="${OPTARG}";;
\?) usage "${INVALID_OPT_ERR} ${OPTARG}" 1>&2;;
:) usage "${INVALID_OPT_ERR} ${OPTARG} ${REQ_ARG_ERR}" 1>&2;;
esac
done
shift $((OPTIND-1))
#===============================================================================
# variables
#===============================================================================
# get the input file name
input_nopath="${input##*/}"
input_name="${input_nopath%.*}"
# output file name
output_default="${input_name}-metadata.txt"
#===============================================================================
# sexagesimal function
#===============================================================================
sexagesimal () {
total="$(awk 'END { print NR }' < "${input}")"
awk -F "," '
{
time = $1
chapter = $2
if (time ~ /:/) {
split(time, t, ":")
combined = (t[1] * 3600) + (t[2] * 60) + t[3]
milliseconds = (combined * 1000)
}
printf("%d,%s\n"), milliseconds, chapter
}' < "${input}" \
| awk -v records="$total" -F "," ' \
{ if (FNR < records) end = $1-1; else end = $1 }
NR == 1 {print ";FFMETADATA1"} NR > 1 {printf("%s\n%s\n%s%s\n%s%s\n%s%s\n"), "[CHAPTER]", "TIMEBASE=1/1000", "START=", prev, "END=", end, "title=", chapter}; {prev = $1; chapter = $2}' > "${output:=${output_default}}"
}
#===============================================================================
# run function
#===============================================================================
sexagesimal