-
Notifications
You must be signed in to change notification settings - Fork 1
/
nf-video.sh
executable file
·268 lines (244 loc) · 6.98 KB
/
nf-video.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
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
267
268
#!/bin/bash
#==========================================================================
#
# File: nf-video.sh
#
# Usage: ./nf-video.sh -i $FILETOENCODE
#
# Description: Runs a video job.
#
# Requirements: -t -c -d
# Author: Christopher Mundus <crashGoBoom>
# Project: nf-video
# Created: 20190116
#==========================================================================
#==========================================================================
# Error checking
#==========================================================================
set -o errexit # Exit when a command fails
#==========================================================================
# Constants
#==========================================================================
WARN="\033[31m"
INFO="\033[32m"
NOCOLOR="\033[0m"
INSTALL_DIR="/usr/local/bin"
BACKGROUND_COLOR='WhiteSmoke'
FONT=''
FONT_COLOR='DarkGray'
DUR=5
TITLE_TEXT=''
SRT=''
SRT_LANG='eng'
CRF=23
X=10
Y=10
BUMPER_IMAGE=''
#==========================================================================
# Functions
#==========================================================================
#==== FUNCTION ============================================================
# NAME: info
# DESCRIPTION: Output some info.
# ARGS: $1 = Type of info to display, $2 = Text to display
# CREATED: 20190116
#==========================================================================
function log() {
local type="$1"
local text="$2"
echo -e "${type}${text}${NOCOLOR}"
}
#==== FUNCTION ============================================================
# NAME: usage
# DESCRIPTION: Displays usage
# AUTHOR: Christopher Mundus <[email protected]>
# CREATED: 20190115
#==========================================================================
function usage() {
cat <<help_message
--- script subcommand -------------------------------------------------------------
Commands related to this script
USAGE:
./nf-video.sh [FLAGS] [SUBCOMMAND]
FLAGS:
--bgcolor Background Color of bumper video
--bumperimage Image to use for the bumper video
-c CRF Number for ffmpeg
-d Set the duration of the bumper video
-f Font for bumper title
--fontcolor Font Color of bumper video
-h Prints help information
-i Video to Process (Required.)
-l Language of the subtitle track in ISO 639. 3 letter language code. (eng, fra, etc..)
-s Adds a subtitle file.
-t Adds text to the bumper video.
-w Adds a watermark (Defaults to upper left.)
-x X location for the watermark
-y Y location for the watermark
SUBCOMMANDS:
all Do everything (blah, blah2) [default]
help_message
return 1
}
function get_opts() {
# Parse options to the main command.
while getopts ':-:c:d:f:h?:i:l:s:t:w:x:y:' opt; do
case "${opt}" in
-)
case "${OPTARG}" in
bgcolor)
BACKGROUND_COLOR="${!OPTIND}"
OPTIND=$(( OPTIND + 1 ))
;;
bumperimage)
BUMPER_IMAGE="${!OPTIND}"
OPTIND=$(( OPTIND + 1 ))
;;
fontcolor)
FONT_COLOR="${!OPTIND}"
OPTIND=$(( OPTIND + 1 ))
;;
esac
;;
c)
CRF="${OPTARG}"
;;
d)
DUR="${OPTARG}"
;;
f)
FONT=${OPTARG}
log $INFO "Adding Font: ${FONT}"
;;
h|\?)
log $INFO "Help:\n-w Add a watermark image (PNG,JPG). "
usage
;;
i)
VIDEO_INPUT=${OPTARG}
log $INFO "Processing: ${VIDEO_INPUT}"
;;
l)
SRT_LANG=${OPTARG}
log $INFO "Setting srt language to: ${SRT_LANG} "
;;
s)
SRT=${OPTARG}
log $INFO "Adding Subtitles from: ${SRT}"
;;
t)
TITLE_TEXT=${OPTARG}
log $INFO "Adding title text: ${TITLE_TEXT}"
;;
w)
WATERMARK="${OPTARG}"
log $INFO "Adding a watermark with ${WATERMARK}"
;;
x)
X="${OPTARG}"
;;
y)
Y="${OPTARG}"
;;
*)
usage
;;
esac
done
shift $((OPTIND-1))
if [[ ${VIDEO_INPUT} = "" && ${BUMPER_IMAGE} = "" ]]; then
log "${WARN}" "Please provide a video to process."
usage
fi
# Remove the main command from the argument list.
local -r _subcommand="${1:-}"
if [[ -z ${_subcommand} ]]; then
install_nextflow
run_nextflow
return 0
fi
shift
case "${_subcommand}" in
run)
run_nextflow
;;
install)
install_nextflow
;;
*)
usage
;;
esac
return 0
}
#==== FUNCTION ============================================================
# NAME: install_nextflow
# DESCRIPTION: Install nextflow if its not already.
# CREATED: 20190116
#==========================================================================
function install_nextflow() {
if type nextflow &>/dev/null; then
log "${INFO}" "Nextflow found..."
return 0
elif type ./nextflow &>/dev/null; then
log "${INFO}" "Nextflow found..."
return 0
else
log "${WARN}" "Nextflow not found!"
prompt_user "Would you like to install it?"
log "${INFO}" "Installing from nextflow.io..."
curl -s https://get.nextflow.io | bash >/dev/null
mv "${PWD}/nextflow" "${INSTALL_DIR}/nextflow"
fi
return 0
}
#==== FUNCTION ============================================================
# NAME: prompt_user
# DESCRIPTION: Prompts the user for something.
# CREATED: 20190116
#==========================================================================
function prompt_user() {
log "${WARN}" "${1}"
read -p "(Y)es or (N)o?" -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
[[ "$0" = "${BASH_SOURCE[*]}" ]] && exit 1 || return 1 # handle exits from shell or function but don't exit interactive shell
fi
}
#==== FUNCTION ============================================================
# NAME: run_nextflow
# DESCRIPTION: Run the nextflow command
# CREATED: 20190116
#==========================================================================
function run_nextflow() {
if nextflow run video.nf \
--video_input="${VIDEO_INPUT}" \
--bumper_image="${BUMPER_IMAGE}" \
--srt="${SRT}" \
--language="${SRT_LANG}" \
--watermark="${WATERMARK}" \
--image="${IMAGE}" \
--crf="${CRF}" \
--font="${FONT}" \
--text="${TITLE_TEXT}" \
--duration="${DUR}" \
--background_color="${BACKGROUND_COLOR}" \
--font_color="${FONT_COLOR}" \
--y="${Y}" --x="${X}"; then
log "${INFO}" "Successfully processed ${VIDEO_INPUT} as completed.mp4!"
log "${INFO}" "Cleaning up..."
# nextflow clean -f &>/dev/null
fi
return 0
}
#==== FUNCTION ============================================================
# NAME: main
# DESCRIPTION: Triggers the whole process.
# CREATED: 20190116
#==========================================================================
function main() {
get_opts "${@}"
return 0
}
main "${@}"