/
extract.sh
193 lines (152 loc) · 4.88 KB
/
extract.sh
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
#! /usr/bin/env bash
####################################################################
# BigBrother CCTV Recording & Live Viewing (mirroring) software #
# Copyright 2016 Andrew Wood #
# #
# extract.sh Bourne shell script to extract a segment from a #
# recording #
# #
# www.simple.org/bigbrother #
# #
# Licensed under the GNU Public License v 3 #
# The full license can be read at www.gnu.org/licenses/gpl-3.0.txt #
# and is included in the License.txt file included with this #
# software. #
# #
# BigBrother is free open source software but if you find it #
# useful please consider making a donation to the Communications #
# Museum Trust at www.comms.org.uk/donate #
####################################################################
usagestring="Syntax error. Usage: $0 -in /path/to/inputfile -out /path/to/outputfile -start HH:MM:SS -duration HH:MM:SS"
copyrightstring="Simple BigBrother Copyright Andrew Wood 2016"
echoUsage()
{
echo " "
echo $usagestring
echo " "
echo $copyrightstring
echo " "
}
if [ $# -ne 8 ]
then
echoUsage
echo "$0 started with incorrect number of arguments, cannot continue"
exit 1
fi
inputfile="" #safety default init
outputfile="" #safety default init
start="" #safety default init
duration="" #safety default init
parseParams()
{
if [ $1 == '-in' ] && [ $2 != '-in' ] && [ $2 != '-out' ] && [ $2 != '-start' ] && [ $2 != '-duration' ]
then
inputfile=$2
return 0
elif [ $1 == '-out' ] && [ $2 != '-out' ] && [ $2 != '-in' ] && [ $2 != '-start' ] && [ $2 != '-duration' ]
then
outputfile=$2
return 0
elif [ $1 == '-start' ] && [ $2 != '-start' ] && [ $2 != '-in' ] && [ $2 != '-out' ] && [ $2 != '-duration' ]
then
start=$2
return 0
elif [ $1 == '-duration' ] && [ $2 != '-duration' ] && [ $2 != '-in' ] && [ $2 != '-out' ] && [ $2 != '-start' ]
then
duration=$2
return 0
fi
#invalid params
return 1
}
parseParams $1 $2
if [ $? -ne 0 ]
then
echoUsage
echo "$0 started with incorrect arguments, cannot continue"
exit 1
fi
parseParams $3 $4
if [ $? -ne 0 ]
then
echoUsage
echo "$0 started with incorrect arguments, cannot continue"
exit 1
fi
parseParams $1 $2
if [ $? -ne 0 ]
then
echoUsage
echo "$0 started with incorrect arguments, cannot continue"
exit 1
fi
parseParams $5 $6
if [ $? -ne 0 ]
then
echoUsage
echo "$0 started with incorrect arguments, cannot continue"
exit 1
fi
parseParams $1 $2
if [ $? -ne 0 ]
then
echoUsage
echo "$0 started with incorrect arguments, cannot continue"
exit 1
fi
parseParams $7 $8
if [ $? -ne 0 ]
then
echoUsage
echo "$0 started with incorrect arguments, cannot continue"
exit 1
fi
#check all params have been initialised, they are not still the default
if [ $inputfile == "" ]
then
echoUsage
echo "$0 started with incorrect arguments, cannot continue"
exit 1
fi
if [ $outputfile == "" ]
then
echoUsage
echo "$0 started with incorrect arguments, cannot continue"
exit 1
fi
if [ $start == "" ]
then
echoUsage
echo "$0 started with incorrect arguments, cannot continue"
exit 1
fi
if [ $duration == "" ]
then
echoUsage
echo "$0 started with incorrect arguments, cannot continue"
exit 1
fi
#start must be hh:mm:ss (start time)
#duration must be hh:mm:ss (time to run for)
matchinglines=$(echo "$start" | grep -c -E '^[0-9]{2}:[0-9]{2}:[0-9]{2}$')
if [ $matchinglines -ne 1 ]
then
echo "Error: -start must be in format HH:MM:SS e.g 00:10:00 to start extracting 10 minutes in"
echoUsage
exit 1
fi
matchinglines=$(echo "$duration" | grep -c -E '^[0-9]{2}:[0-9]{2}:[0-9]{2}$')
if [ $matchinglines -ne 1 ]
then
echo "Error: -duration must be in format HH:MM:SS e.g 00:00:30 to extract 30 seconds of video"
echoUsage
exit 1
fi
#to prevent accidental overwriting of input, check input & output files are not same
if [ $inputfile == $outputfile ]
then
echo "Error: Input file and output file must not be the same"
echoUsage
exit 1
fi
ffmpeg -loglevel fatal -ss $start -i $inputfile -t $duration -vcodec copy -acodec copy $outputfile