-
Notifications
You must be signed in to change notification settings - Fork 2
/
ff_social_media.sh
executable file
·231 lines (174 loc) · 8.75 KB
/
ff_social_media.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
#!/bin/bash
# ╭──────────────────────────────────────────────────────────────────────────────╮
# │ │
# │ Converts the video, ready for various social platforms │
# │ │
# ╰──────────────────────────────────────────────────────────────────────────────╯
# ╭──────────────────────────────────────────────────────────╮
# │ Set Defaults │
# ╰──────────────────────────────────────────────────────────╯
# set -o errexit # If a command fails bash exits.
# set -o pipefail # pipeline fails on one command.
if [[ "${DEBUG-0}" == "1" ]]; then set -o xtrace; fi # DEBUG=1 will show debugging.
# ╭──────────────────────────────────────────────────────────╮
# │ VARIABLES │
# ╰──────────────────────────────────────────────────────────╯
INPUT_FILENAME=""
OUTPUT_FILENAME="ff_audio.mp4"
LOGLEVEL="error"
INSTAGRAM=""
function stylesheet()
{
TEXT_GREEN_400="\e[38;2;74;222;128m"
TEXT_ORANGE_500="\e[38;2;249;115;22m"
TEXT_RED_400="\e[38;2;248;113;113m"
TEXT_BLUE_600="\e[38;2;37;99;235m"
TEXT_YELLOW_500="\e[38;2;234;179;8m"
TEXT_PURPLE_500="\e[38;2;168;85;247m"
TEXT_RESET="\e[39m"
}
stylesheet
# ╭──────────────────────────────────────────────────────────╮
# │ Usage. │
# ╰──────────────────────────────────────────────────────────╯
usage()
{
if [ "$#" -lt 2 ]; then
printf "ℹ️ Usage:\n $0 -i <INPUT_FILE> [-ig] [-o <OUTPUT_FILE>] [-l loglevel]\n\n" >&2
printf "Summary:\n"
printf "Convert ready for Social Media (pix_fmt=yuv420p)\n\n"
printf "Flags:\n"
printf " -i | --input <INPUT_FILE>\n"
printf "\tThe name of an input file.\n\n"
printf " -o | --output <OUTPUT_FILE>\n"
printf "\tDefault is %s\n" "${OUTPUT_FILENAME}"
printf "\tThe name of the output file.\n\n"
printf " -ig | --instagram \n"
printf "\tConvert ready for Instagram.\n"
printf " -C | --config <CONFIG_FILE>\n"
printf "\tSupply a config.json file with settings instead of command-line. Requires JQ installed.\n\n"
printf " -l | --loglevel <LOGLEVEL>\n"
printf "\tThe FFMPEG loglevel to use. Default is 'error' only.\n"
printf "\tOptions: quiet,panic,fatal,error,warning,info,verbose,debug,trace\n\n"
exit 0
fi
}
# ╭──────────────────────────────────────────────────────────╮
# │ Take the arguments from the command line │
# ╰──────────────────────────────────────────────────────────╯
function arguments()
{
POSITIONAL_ARGS=()
while [[ $# -gt 0 ]]; do
case $1 in
-i|--input)
INPUT_FILENAME=$(realpath $2)
shift
shift
;;
-o|--output)
OUTPUT_FILENAME="$2"
shift
shift
;;
-ig|--instagram)
INSTAGRAM="true"
shift
;;
-C|--config)
CONFIG_FILE="$2"
shift
shift
;;
-l|--loglevel)
LOGLEVEL="$2"
shift
shift
;;
--description) # IGNORED. used for descriptions in JSON
shift
shift
;;
-*|--*)
echo "Unknown option $1"
exit 1
;;
*)
POSITIONAL_ARGS+=("$1") # save positional arg back onto variable
shift # remove argument and shift past it.
;;
esac
done
}
# ╭──────────────────────────────────────────────────────────╮
# │ Read config-file if supplied. Requires JQ │
# ╰──────────────────────────────────────────────────────────╯
function read_config()
{
# Check if config has been set.
if [ -z ${CONFIG_FILE+x} ]; then return 0; fi
# Check dependencies
if ! command -v jq &> /dev/null; then
printf "JQ is a dependency and could not be found. Please install JQ for JSON parsing. Exiting.\n"
exit
fi
# Read file
LIST_OF_INPUTS=$(cat ${CONFIG_FILE} | jq -r 'to_entries[] | ["--" + .key, .value] | @sh' | xargs)
# Sen to the arguments function again to override.
arguments $LIST_OF_INPUTS
}
# ╭──────────────────────────────────────────────────────────╮
# │ Exit the app by just skipping the ffmpeg processing. │
# │ Then copy the input to the output. │
# ╰──────────────────────────────────────────────────────────╯
function exit_gracefully()
{
cp -f ${INPUT_FILENAME} ${OUTPUT_FILENAME}
exit 0
}
# ╭──────────────────────────────────────────────────────────╮
# │ Run these checks before you run the main script │
# ╰──────────────────────────────────────────────────────────╯
function pre_flight_checks()
{
# Check input filename has been set.
if [[ -z "${INPUT_FILENAME+x}" ]]; then
printf "\t❌ No input file specified. Exiting.\n"
exit_gracefully
fi
# Check input file exists.
if [ ! -f "$INPUT_FILENAME" ]; then
printf "\t❌ Input file not found. Exiting.\n"
exit_gracefully
fi
# Check input filename is a movie file.
if ffprobe -v quiet -select_streams v:0 -show_entries stream=codec_name -print_format csv=p=0 "${INPUT_FILENAME}" > /dev/null 2>&1; then
printf ""
else
printf "\t❌ Input file: '%s' not a movie file. Exiting.\n" "${INPUT_FILENAME}"
ffprobe "${INPUT_FILENAME}"
exit_gracefully
fi
}
function print_flags()
{
printf "🤳 ${TEXT_GREEN_400}%-10s :${TEXT_RESET} %s\n" "Instagram" "$INSTAGRAM"
}
# ╭──────────────────────────────────────────────────────────╮
# │ │
# │ Main Function │
# │ │
# ╰──────────────────────────────────────────────────────────╯
function main()
{
pre_flight_checks
print_flags
if [ ! -z ${INSTAGRAM+x} ]; then
ffmpeg -v ${LOGLEVEL} -i ${INPUT_FILENAME} -pix_fmt yuv420p ${OUTPUT_FILENAME}
fi
printf "✅ ${TEXT_PURPLE_500}%-10s :${TEXT_RESET} %s\n" "Output" "$OUTPUT_FILENAME"
}
usage $@
arguments $@
read_config "$@"
main $@