-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathtrim-clip
executable file
·262 lines (222 loc) · 7.16 KB
/
trim-clip
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
#!/bin/sh
#===============================================================================
# trim-clip
# trim video or audio clips with millisecond accuracy
#===============================================================================
# dependencies:
# ffmpeg file
#===============================================================================
# script usage
#===============================================================================
usage () {
# if argument passed to function echo it
[ -z "${1}" ] || echo "! ${1}"
# display help
echo "\
# trim video or audio clips with millisecond accuracy
https://trac.ffmpeg.org/wiki/Seeking
$(basename "$0") -s 00:00:00.000 -i input -t 00:00:00.000 -o output)
-s 00:00:00.000 : start time
-i input.(mp4|mov|mkv|m4v|webm|aac|m4a|wav|mp3|ogg)
-t 00:00:00.000 : number of seconds after start time
-o output.(mp4|webm|aac|mp3|wav|ogg) : optional argument
# if option not provided defaults input-name-[start end].(mp4|webm|aac|mp3|wav|ogg)"
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'
NOT_MEDIA_FILE_ERR='is not a media file'
#===============================================================================
# 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 ':s:i:t:o:h' opt
do
case ${opt} in
s) start="${OPTARG}";;
i) infile="${OPTARG}";;
t) end="${OPTARG}";;
h) usage;;
o) outfile="${OPTARG}";;
\?) usage "${INVALID_OPT_ERR} ${OPTARG}" 1>&2;;
:) usage "${INVALID_OPT_ERR} ${OPTARG} ${REQ_ARG_ERR}" 1>&2;;
esac
done
shift $((OPTIND-1))
#===============================================================================
# variables
#===============================================================================
# input name
infile_nopath="${infile##*/}"
infile_name="${infile_nopath%.*}"
# input file extension
infile_ext="${infile##*.}"
# file command check input file mime type
filetype="$(file --mime-type -b "${infile}")"
# video mimetypes
mov_mime='video/quicktime'
mkv_mime='video/x-matroska'
mp4_mime='video/mp4'
webm_mime='video/webm'
m4v_mime='video/x-m4v'
wav_mime='audio/x-wav'
audio_mime='audio/mpeg'
aac_mime='audio/x-hx-aac-adts'
m4a_mime='audio/mp4'
ogg_mime='audio/ogg'
# the file command wrongly identifies .m4a audio as a video file
# so we check if the file extension is .m4a and set the mime type to audio/mp4
if [ "${infile_ext}" = 'm4a' ]; then
filetype="${m4a_mime}"
fi
# awk convert start and end time from sexagesimal time to seconds
# add the start and end time to get the endpoint and convert from seconds back to sexagesimal time
endtime=$(printf "%s %s\n" "${start}" "${end}" \
| awk '
{
start = $1
end = $2
if (start ~ /:/) {
split(start, t, ":")
sseconds = (t[1] * 3600) + (t[2] * 60) + t[3]
}
if (end ~ /:/) {
split(end, t, ":")
eseconds = (t[1] * 3600) + (t[2] * 60) + t[3]
}
duration = eseconds + sseconds
printf("%s\n"), duration
}' \
| awk -F. 'NF==1 { printf("%02d:%02d:%02d\n"), ($1 / 3600), ($1 % 3600 / 60), ($1 % 60) }\
NF==2 { printf("%02d:%02d:%02d.%s\n"), ($1 / 3600), ($1 % 3600 / 60), ($1 % 60), ($2) }'
)
# defaults for variables if not defined
videofile_default="${infile_name}-[${start}-${endtime}].mp4"
webm_default="${infile_name}-[${start}-${endtime}].webm"
aac_default="${infile_name}-[${start}-${endtime}].aac"
mp3_default="${infile_name}-[${start}-${endtime}].mp3"
wav_default="${infile_name}-[${start}-${endtime}].wav"
m4a_default="${infile_name}-[${start}-${endtime}].m4a"
ogg_default="${infile_name}-[${start}-${endtime}].ogg"
#===============================================================================
# check if the libfdk_aac codec is installed, if not fall back to the aac codec
#===============================================================================
aac_codec="$(ffmpeg -hide_banner -stats -v panic -h encoder=libfdk_aac)"
aac_error="Codec 'libfdk_aac' is not recognized by FFmpeg."
aac_check="$(echo "${aac_codec}" | grep "${aac_error}")"
# check ffmpeg aac codecs
if [ -z "${aac_check}" ]; then
aac='libfdk_aac' # libfdk_aac codec is installed
else
aac='aac' # libfdk_aac codec isnt installed, fall back to aac codec
fi
#===============================================================================
# audio and video functions
#===============================================================================
# trim video clip
trim_video () {
ffmpeg \
-hide_banner \
-stats -v panic \
-ss "${start}" \
-i "${infile}" \
-t "${end}" \
-c:a "${aac}" \
-c:v libx264 -profile:v high \
-pix_fmt yuv420p -movflags +faststart \
-f mp4 \
"${outfile:=${videofile_default}}"
}
# trim webm video clip
trim_webm () {
ffmpeg \
-hide_banner \
-stats -v panic \
-ss "${start}" \
-i "${infile}" \
-t "${end}" \
-c:a libopus \
-c:v vp9 \
-f webm \
"${outfile:=${webm_default}}"
}
# trim aac audio clip
trim_aac () {
ffmpeg \
-hide_banner \
-stats -v panic \
-ss "${start}" \
-i "${infile}" \
-t "${end}" \
-c:a "${aac}" \
-f adts \
"${outfile:=${aac_default}}"
}
# trim m4a audio clip
trim_m4a () {
ffmpeg \
-hide_banner \
-stats -v panic \
-ss "${start}" \
-i "${infile}" \
-t "${end}" \
-c:a "${aac}" \
-f mp4 \
"${outfile:=${m4a_default}}"
}
# trim mp3 audio clip
trim_mp3 () {
ffmpeg \
-hide_banner \
-stats -v panic \
-ss "${start}" \
-i "${infile}" \
-t "${end}" \
-c:a libmp3lame \
-f mp3 \
"${outfile:=${mp3_default}}"
}
# trim wav audio clip
trim_wav () {
ffmpeg \
-hide_banner \
-stats -v panic \
-ss "${start}" \
-i "${infile}" \
-t "${end}" \
-c:a pcm_s16le \
-f wav \
"${outfile:=${wav_default}}"
}
# trim ogg audio clip
trim_ogg () {
ffmpeg \
-hide_banner \
-stats -v panic \
-ss "${start}" \
-i "${infile}" \
-t "${end}" \
-c:a libopus \
-f ogg \
"${outfile:=${ogg_default}}"
}
#===============================================================================
# check the files mime type
#===============================================================================
case "${filetype}" in
"${mov_mime}"|"${mkv_mime}"|"${mp4_mime}"|"${m4v_mime}") trim_video "${infile}";;
"${webm_mime}") trim_webm "${infile}";;
"${aac_mime}") trim_aac "${infile}";;
"${m4a_mime}") trim_m4a "${infile}";;
"${audio_mime}") trim_mp3 "${infile}";;
"${wav_mime}") trim_wav "${infile}";;
"${ogg_mime}") trim_ogg "${infile}";;
*) usage "${infile} ${NOT_MEDIA_FILE_ERR}";;
esac