forked from crashGoBoom/nf-video
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nf-video.sh
executable file
·209 lines (184 loc) · 5.37 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
#!/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"
CRF=23
X=10
Y=10
#==========================================================================
# 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:
-c CRF Number for ffmpeg
-h Prints help information
-i Video to Process (Required.)
-w Adds a watermark (Defaults to lower right.)
-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:h?:w:i:x:y:' opt; do
case "${opt}" in
c)
CRF="${OPTARG}"
;;
h|\?)
log $INFO "Help:\n-w Add a watermark image (PNG,JPG). "
usage
;;
i)
VIDEO_INPUT=${OPTARG}
log $INFO "Processing: ${VIDEO_INPUT}"
;;
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} = "" ]]; 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 \
--inputs="${VIDEO_INPUT}" \
--watermark="${WATERMARK}" \
--crf="${CRF}" \
--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 "${@}"