-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathcombine-clips
executable file
·208 lines (175 loc) · 6.05 KB
/
combine-clips
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
#!/bin/sh
#===============================================================================
# combine-clips
# combine an image or video file with an audio clip
#===============================================================================
# dependencies:
# ffmpeg file grep
#===============================================================================
# script usage
#===============================================================================
usage ()
{
# if argument passed to function echo it
[ -z "${1}" ] || echo "! ${1}"
# display help
echo "\
# combine an image or video file with an audio clip
$(basename "$0") -i input.(mp4|mkv|mov|m4v|png|jpg) -a audio.(m4a|aac|wav|mp3) -o output.mp4
-i input.(mp4|mkv|mov|m4v|png|jpg)
-a audio.(m4a|aac|wav|mp3)
-o output.mp4 : optional argument # if option not provided defaults to input-name-combined-date-time"
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 ':i:a:o:h' opt
do
case ${opt} in
i) infile="${OPTARG}";;
a) audio="${OPTARG}";;
o) outfile="${OPTARG}";;
h) usage;;
\?) usage "${INVALID_OPT_ERR} ${OPTARG}" 1>&2;;
:) usage "${INVALID_OPT_ERR} ${OPTARG} ${REQ_ARG_ERR}" 1>&2;;
esac
done
shift $((OPTIND-1))
#===============================================================================
# variables
#===============================================================================
# input, input name
infile_nopath="${infile##*/}"
infile_name="${infile_nopath%.*}"
# audio file extension
audio_ext="${audio##*.}"
# file command check input file mime type
infile_filetype="$(file --mime-type -b "${infile}")"
audio_filetype="$(file --mime-type -b "${audio}")"
# audio and video mimetypes
mov_mime='video/quicktime'
mkv_mime='video/x-matroska'
mp4_mime='video/mp4'
m4v_mime='video/x-m4v'
aac_mime='audio/x-hx-aac-adts'
m4a_mime='audio/mp4'
# 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 [ "${audio_ext}" = 'm4a' ]; then
audio_filetype="${m4a_mime}"
fi
# image mimetypes
png_mime='image/png'
jpg_mime='image/jpeg'
# defaults for variables
outfile_default="${infile_name}-combined-$(date +"%Y-%m-%d-%H-%M-%S").mp4"
# 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
#===============================================================================
# functions
#===============================================================================
# video - audio is aac, copy audio stream
record_copy () {
ffmpeg \
-hide_banner \
-stats -v panic \
-i "${infile}" \
-i "${audio}" \
-shortest -fflags shortest -max_interleave_delta 100M \
-c:a copy \
-c:v copy \
-map 0:0 -map 1:0 \
-pix_fmt yuv420p \
-movflags +faststart \
-f mp4 \
"${outfile:=${outfile_default}}"
}
# video - audio isnt aac, encode audio as aac
record_aac () {
ffmpeg \
-hide_banner \
-stats -v panic \
-i "${infile}" \
-i "${audio}" \
-shortest -fflags shortest -max_interleave_delta 100M \
-c:a "${aac}" \
-c:v copy \
-map 0:0 -map 1:0 \
-pix_fmt yuv420p \
-movflags +faststart \
-f mp4 \
"${outfile:=${outfile_default}}"
}
# image - audio is aac, copy audio stream
record_img_copy () {
dur="$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "${audio}")"
ffmpeg \
-hide_banner \
-stats -v panic \
-framerate 1/"${dur}" \
-i "${infile}" \
-i "${audio}" \
-c:a copy \
-c:v libx264 -crf 18 -profile:v high \
-r 30 -pix_fmt yuv420p \
-movflags +faststart -f mp4 \
"${outfile:=${outfile_default}}"
}
# image - audio isnt aac, encode audio as aac
record_img_aac () {
dur="$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "${audio}")"
ffmpeg \
-hide_banner \
-stats -v panic \
-framerate 1/"${dur}" \
-i "${infile}" \
-i "${audio}" \
-c:a "${aac}" \
-c:v libx264 -crf 18 -profile:v high \
-r 30 -pix_fmt yuv420p \
-movflags +faststart -f mp4 \
"${outfile:=${outfile_default}}"
}
#===============================================================================
# case statement
#===============================================================================
# run the ffmpeg function based on the audio mime type
case "${infile_filetype}" in
"${mov_mime}"|"${mkv_mime}"|"${mp4_mime}"|"${m4v_mime}")
if [ "${audio_filetype}" = 'audio/x-hx-aac-adts' ]; then
# video - audio is aac, copy audio stream
record_copy
else
# video - audio isnt aac, encode audio as aac
record_aac
fi;;
"${png_mime}"|"${jpg_mime}")
if [ "${audio_filetype}" = "${aac_mime}" ]; then
# image - audio is aac, copy audio stream
record_img_copy
else
# image - audio isnt aac, encode audio as aac
record_img_aac
fi;;
*) usage "${infile} ${NOT_MEDIA_FILE_ERR}";;
esac