-
Notifications
You must be signed in to change notification settings - Fork 26
/
comchap
executable file
·266 lines (225 loc) · 5.44 KB
/
comchap
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
#!/usr/bin/env bash
#LD_LIBRARY_PATH is set and will mess up ffmpeg, unset it, then re-set it when done
ldPath=${LD_LIBRARY_PATH}
unset LD_LIBRARY_PATH
exitcode=0
ffmpegPath="ffmpeg"
comskipPath="comskip"
if [[ $# -lt 1 ]]; then
exename=$(basename "$0")
echo "Add chapters to video file using EDL file"
echo " (If no EDL file is found, comskip will be used to generate one)"
echo ""
echo "Usage: $exename infile [outfile]"
exit 1
fi
comskipini=$HOME/.comskip.ini
deleteedl=true
deletemeta=true
deletelog=true
deletelogo=true
deletetxt=true
verbose=false
lockfile=""
workdir=""
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
--keep-edl)
deleteedl=false
shift
;;
--keep-meta)
deletemeta=false
shift
;;
--verbose)
verbose=true
shift
;;
--ffmpeg=*)
ffmpegPath="${key#*=}"
shift
;;
--comskip=*)
comskipPath="${key#*=}"
shift
;;
--comskip-ini=*)
comskipini="${key#*=}"
shift
;;
--lockfile=*)
lockfile="${key#*=}"
shift
;;
--work-dir=*)
workdir="${key#*=}"
shift
;;
-*)
echo "Option $1 doesn't exist, please check the documentation"
exit 1
;;
*)
if [ -z $infile ]; then
infile=$1
if [ ! -f "$infile" ]; then
echo "Inputfile '$infile' doesn't exist. Please check."
exit 1
fi
else
if [ -z $outfile ]; then
outfile=$1
else
echo "Error: too many parameters. Inputfile and Outputfile already defined. Please check your command."
exit 1
fi
fi
shift
;;
esac
done
# Setup for verbose
exec 3>&1
exec 4>&2
if ! $verbose; then
exec 1>/dev/null
exec 2>/dev/null
fi
if [ ! -z "$lockfile" ]; then
echo "lockfile: $lockfile" 1>&3 2>&4
while [[ -f "$lockfile" ]]; do
echo "Waiting" 1>&3 2>&4
sleep 5
done
touch "$lockfile"
fi
if [ ! -f "$comskipini" ]; then
echo "output_edl=1" > "$comskipini"
elif ! grep -q "output_edl=1" "$comskipini"; then
echo "output_edl=1" >> "$comskipini"
fi
if [[ -z "$outfile" ]]; then
outfile="$infile"
fi
outdir=$(dirname "$outfile")
outextension="${outfile##*.}"
if [[ ! -z "$workdir" ]]; then
case "$workdir" in
*/)
;;
*)
comskipoutput="--output=$workdir"
workdir="$workdir/"
;;
esac
infileb=`basename "$infile"`
edlfile="$workdir${infileb%.*}.edl"
metafile="$workdir${infileb%.*}.ffmeta"
logfile="$workdir${infileb%.*}.log"
logofile="$workdir${infileb%.*}.logo.txt"
txtfile="$workdir${infileb%.*}.txt"
else
edlfile="$workdir${infile%.*}.edl"
metafile="$workdir${infile%.*}.ffmeta"
logfile="$workdir${infile%.*}.log"
logofile="$workdir${infile%.*}.logo.txt"
txtfile="$workdir${infile%.*}.txt"
fi
if [ ! -f "$edlfile" ]; then
$comskipPath --ini="$comskipini" "$infile"
if [ ! -f "$edlfile" ] ; then
echo "Error running comskip. EDL File not found: $infile" 1>&3 2>&4 >&2
exitcode=-1
fi
fi
start=0
i=0
hascommercials=false
echo ";FFMETADATA1" > "$metafile"
# Reads in from $edlfile, see end of loop.
while IFS=$'\t' read -r -a line
do
((i++))
end=`awk -vp="${line[0]}" 'BEGIN{printf "%.0f" ,p*1000}'`
startnext=`awk -vp="${line[1]}" 'BEGIN{printf "%.0f" ,p*1000}'`
hascommercials=true
echo [CHAPTER] >> "$metafile"
echo TIMEBASE=1/1000 >> "$metafile"
echo START="$start" >> "$metafile"
echo END="$end" >> "$metafile"
echo "title=Chapter $i" >> "$metafile"
echo [CHAPTER] >> "$metafile"
echo TIMEBASE=1/1000 >> "$metafile"
echo START="$end" >> "$metafile"
echo END="$startnext" >> "$metafile"
echo "title=Commercial $i" >> "$metafile"
start=$startnext
done < "$edlfile"
if $hascommercials ; then
((i++))
echo [CHAPTER] >> "$metafile"
echo TIMEBASE=1/1000 >> "$metafile"
echo START="$start" >> "$metafile"
echo END=`$ffmpegPath -i "$infile" 2>&1 | grep Duration | awk '{print $2}' | tr -d , | awk -F: '{ print ($1*3600)+($2*60)+$3 }'| awk '{printf "%.0f",$1*1000}'` >> "$metafile"
echo "title=Chapter $i" >> "$metafile"
if [ "$infile" -ef "$outfile" ] ; then
tempfile=`mktemp --suffix=."$outextension" "$outdir"/XXXXXXXX`
echo Writing file to temporary file: "$tempfile"
if $ffmpegPath -loglevel error -hide_banner -nostdin -i "$infile" -i "$metafile" -map_metadata 1 -codec copy -y "$tempfile" 1>&3 2>&4; then
mv -f "$tempfile" "$outfile"
echo Saved to: "$outfile"
else
echo Error running ffmpeg: "$infile" 1>&3 2>&4 >&2
exitcode=-1
fi
else
if $ffmpegPath -loglevel error -hide_banner -nostdin -i "$infile" -i "$metafile" -map_metadata 1 -codec copy -y "$outfile" 1>&3 2>&4; then
echo Saved to: "$outfile"
else
echo Error running ffmpeg: "$infile" 1>&3 2>&4 >&2
exitcode=-1
fi
fi
if [ ! -f "$outfile" ]; then
echo Error, "$outfile" does not exist. 1>&3 2>&4 >&2
exitcode=-1
fi
else
echo No commercials found: "$infile" 1>&3 2>&4 >&2
fi
if [ "$deleteedl" == true ] ; then
if [ -f "$edlfile" ] ; then
rm "$edlfile";
fi
fi
if [ "$deletemeta" == true ] ; then
if [ -f "$metafile" ]; then
rm "$metafile";
fi
fi
if [ "$deletelog" == true ] ; then
if [ -f "$logfile" ]; then
rm "$logfile";
fi
fi
if [ "$deletelogo" == true ] ; then
if [ -f "$logofile" ]; then
rm "$logofile";
fi
fi
if [ "$deletetxt" == true ] ; then
if [ -f "$txtfile" ]; then
rm "$txtfile";
fi
fi
if [ ! -z $ldPath ] ; then
#re-set LD_LIBRARY_PATH
export LD_LIBRARY_PATH="$ldPath"
fi
if [ ! -z "$lockfile" ]; then
rm "$lockfile"
fi
exit $exitcode