forked from NRCHKB/nrchkb-ffmpeg-build
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-alpine.sh
executable file
·527 lines (466 loc) · 13.9 KB
/
build-alpine.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
#!/bin/bash
# nrchkb-ffmpeg-build Version 1.1
# MIT License
# Copyright (c) 2022 Marcus Davies
# Copyright (c) 2022 Garrett Porter
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# Colors
Red=$'\e[0;31m'
Yellow=$'\e[1;33m'
End=$'\e[0m'
# Defaults
JOBS=3 # Jobs Value
JOBSPARAM=false # Arg provided
OMX="n" # OMX Choice Value
OMXPARAM=false # Arg provided
FDK="y" # FDK Choice Value
FDKPARAM=false # Arg provided
FLAGSYN="n" # Flags Choice Value
FLAGS="" # Flasg Value
FLAGSPARAM=false # Arg provided
MODE=0 # Mode Value
MODEPARAM=false # Arg provided
INTERACTIVE="y" # Interactive
# Prefix
PREFIX="/usr/local"
export LDFLAGS="-L$PREFIX/lib"
export CFLAGS="-I$PREFIX/include"
# Specific package manager implemention
INSTALL() {
apk add $@
}
REMOVE() {
apk del $1
}
CHECK() {
apk info $1
}
# Print Header
printHeader() {
printf "\033c"
echo
echo " ---------------------------------------------------------"
echo " | 1.1 |"
echo " | P&M FFmpeg Build Script (Alpine) |"
echo " | An FFmpeg build & installation utility for NRCHKB |"
echo " | |"
echo " ---------------------------------------------------------"
echo
echo " ${Red}Note: This script will install into $PREFIX/bin and $PREFIX/lib respectively.${End}"
echo
}
# Print menu
menu() {
echo " ${Yellow}What would you like to do:${End}"
echo
echo " 1 - Install build tools (dependencies from apk)"
echo " 2 - Build/install libfdk-aac (AAC encoder, needed for HomeKit audio)"
echo " 3 - Build/install FFmpeg (video processor, builds from source)"
echo " 4 - All of the above"
echo " 5 - Cleanup build directories"
echo " q - Quit"
echo
echo " Note: this script will download and compile these software packages from source code."
echo " This will take a long time. Option 4 will take over 6 hours on a Pi Zero W."
echo
echo " If you have previously run this script, running it again will update your software."
echo
printf " Choice: "
read -r
if [[ "$REPLY" = "q" ]]; then
exit 0
fi
MODE=$REPLY
if [[ $MODE -gt 5 || $MODE -lt 1 ]]; then
printHeader
menu
fi
processOptions $MODE
}
# Error Check
checkForError() {
if [[ $? -gt 0 ]]; then
stopWatch "stop"
echo "${Red}"
echo " ---------------------------------------------------------"
echo " | |"
echo " | Errors occurred |"
echo " | Please check the logs and try again |"
echo " | |"
echo " ---------------------------------------------------------"
echo "${End}"
echo
exit 1
fi
}
# Install Dependencies
installDependencies() {
echo
echo " ---------------------------------------------------------"
echo " | |"
echo " | Installing Dependencies |"
echo " | |"
echo " ---------------------------------------------------------"
echo
INSTALL pkgconfig autoconf automake libtool git wget make g++ gcc nasm yasm
CHECK x264-dev
if [[ $? -gt 0 ]]; then
installLibx264
else
INSTALL x264-dev
fi
}
# Install Libx264
installLibx264() {
cd ~ || {
echo "cd failed, aborting at installLibx264:01"
exit 1
}
echo
echo " ---------------------------------------------------------"
echo " | |"
echo " | Building/Installing libx264 |"
echo " | |"
echo " ---------------------------------------------------------"
echo
REMOVE x264-dev
git clone https://code.videolan.org/videolan/x264.git
cd x264 || {
echo "cd failed, aborting at installLibx264:02"
exit 1
}
./configure --prefix=$PREFIX --enable-static --enable-pic
checkForError
make -j"$JOBS"
checkForError
make install
checkForError
ldconfig
cd ~ || {
echo "cd failed, aborting at installLibx264:03"
exit 1
}
}
# Install Libfdk
installLibfdk() {
cd ~ || {
echo "cd failed, aborting at installLibfdk:01"
exit 1
}
echo
echo " ---------------------------------------------------------"
echo " | |"
echo " | Building/Installing libfdk-aac |"
echo " | |"
echo " ---------------------------------------------------------"
echo
CHECK fdk-aac-dev
if [[ $? = 0 ]]; then
apk add fdk-aac-dev
return
fi
REMOVE fdk-aac-dev
git clone https://github.com/mstorsjo/fdk-aac.git
cd fdk-aac || {
echo "cd failed, aborting at installLibfdk:02"
exit 1
}
./autogen.sh
./configure --prefix=$PREFIX --enable-static --disable-shared
checkForError
make -j"$JOBS"
checkForError
make install
checkForError
ldconfig
cd ~ || {
echo "cd failed, aborting at installLibfdk:03"
exit 1
}
}
# Install FFmpeg
installFFmpeg() {
cd ~ || {
echo "cd failed, aborting at installFFmpeg:01"
exit 1
}
echo
echo " ---------------------------------------------------------"
echo " | |"
echo " | Building/Installing FFmpeg |"
echo " | |"
echo " ---------------------------------------------------------"
echo
REMOVE ffmpeg
wget -O ffmpeg-snapshot.tar.bz2 https://ffmpeg.org/releases/ffmpeg-snapshot.tar.bz2
echo "Extracting source code..."
tar xjf ffmpeg-snapshot.tar.bz2
cd ffmpeg || {
echo "cd failed, aborting at installFFmpeg:02"
exit 1
}
CMD="--prefix=$PREFIX --enable-nonfree --enable-gpl --enable-hardcoded-tables --disable-ffprobe --disable-ffplay --enable-libx264"
if [[ "$FDK" = "y" ]]; then
CMD="$CMD --enable-libfdk-aac"
fi
if [[ "$OMX" = "y" ]]; then
CMD="$CMD --enable-mmal"
CMD="$CMD --enable-omx"
CMD="$CMD --enable-omx-rpi"
fi
if [[ "$FLAGSYN" = "y" ]]; then
CMD="$CMD $FLAGS"
fi
./configure $CMD
checkForError
make -j"$JOBS"
checkForError
make install
checkForError
cd ~ || {
echo "cd failed, aborting at installFFmpeg:03"
exit 1
}
}
# Clear Up
cleanDirectory() {
rm -rf ffmpeg
rm -rf fdk-aac
rm -rf x264
rm -f ffmpeg-snapshot.tar.bz2
}
# Ask for Threads
getJobsCount() {
if [[ $JOBSPARAM = true || "$INTERACTIVE" = "n" ]]; then
return
fi
echo
echo " ${Yellow}How many simultaneous jobs would you like to use for build processes (if needed)${End}"
echo
echo " The more you specify - the higher chance of CPU throttling and memory constraints"
printf " we recommend no more than 3 for a Pi 4 (1-4): "
read -r
if [[ $REPLY -lt 5 && $REPLY -gt 0 ]]; then
JOBS=$REPLY
fi
}
# Ask for omx
getOMX() {
if [[ $OMXPARAM = true || "$INTERACTIVE" = "n" ]]; then
return
fi
echo
echo " ${Yellow}Would you like to enable 'h264_omx'?${End}"
echo
echo " Note: 'h264_omx' is deprecated and should not be used on new installs."
printf " If you already use it, choose yes here. Enter (y/n): "
read -r
if [[ "$REPLY" = "y" || "$REPLY" = "n" ]]; then
OMX="$REPLY"
fi
}
# Ask for FDK
getFDK() {
if [[ $FDKPARAM = true || "$INTERACTIVE" = "n" ]]; then
return
fi
echo
echo " ${Yellow}Would you like to enable 'libfdk-aac'?${End}"
echo
echo " Note: 'libfdk-aac' is needed for HomeKit audio. We recommend enabling libfdk-aac."
printf " If you are running Option 4, you can enable this lib. Enter (y/n): "
read -r
if [[ "$REPLY" = "y" || "$REPLY" = "n" ]]; then
FDK="$REPLY"
fi
}
# Get Compile Flags
getFlags() {
if [[ $FLAGSPARAM = true || "$INTERACTIVE" = "n" ]]; then
return
fi
echo
echo " ${Yellow}Would you like to add any extra FFmpeg compile flags?"
echo
echo " ADVANCED: ${End}Compile flags could be added to enable libx265 or the countless others"
printf " You are responsible for ensuring any required parts are installed (y/n): "
read -r
if [[ "$REPLY" = "y" || "$REPLY" = "n" ]]; then
FLAGSYN="$REPLY"
if [[ "$FLAGSYN" = "y" ]]; then
echo
echo " ${Yellow}Please enter your compile flags below, separated by a space${End}"
echo
printf " Example '--enable-libx265 --enable-libopus' : "
read -r
if [[ ${#REPLY} -gt 0 ]]; then
FLAGS="$REPLY"
else
FLAGSYN="n"
fi
fi
fi
}
# Performance Stop Watch
stopWatch() {
if [[ "$1" = "stop" ]]; then
endEpoch=$(date +%s)
endTime=$(date)
durationEpoch=$((endEpoch - startEpoch))
echo
echo " Start time: ${startTime}"
echo " End time: ${endTime}"
echo " Duration: ${durationEpoch} seconds"
echo " Max jobs: $JOBS"
echo " Option: $MODE"
echo
else
startEpoch=$(date +%s)
startTime=$(date)
fi
}
# Command Processor
processOptions() {
case $1 in
1)
getJobsCount
stopWatch "start"
installDependencies
stopWatch "stop"
if [ "$INTERACTIVE" = "y" ]; then
echo " ${Yellow}All Done!${End} ...press enter"
read -r
printHeader
menu
fi
;;
2)
getJobsCount
stopWatch "start"
installLibfdk
stopWatch "stop"
if [ "$INTERACTIVE" = "y" ]; then
echo " ${Yellow}All Done!${End} ...press enter"
read -r
printHeader
menu
fi
;;
3)
getJobsCount
getOMX
getFDK
getFlags
stopWatch "start"
installFFmpeg
stopWatch "stop"
if [ "$INTERACTIVE" = "y" ]; then
echo " ${Yellow}All Done!${End} ...press enter"
read -r
printHeader
menu
fi
;;
4)
cleanDirectory
getJobsCount
getOMX
getFDK
getFlags
stopWatch "start"
installDependencies
if [[ "$FDK" = "y" ]]; then
installLibfdk
fi
installFFmpeg
cleanDirectory
stopWatch "stop"
if [ "$INTERACTIVE" = "y" ]; then
echo " ${Yellow}All Done!${End} ...press enter"
read -r
printHeader
menu
fi
;;
5)
stopWatch "start"
cleanDirectory
stopWatch "stop"
if [ "$INTERACTIVE" = "y" ]; then
echo " ${Yellow}All Done!${End} ...press enter"
read -r
printHeader
menu
fi
;;
esac
}
# Entry Point
cd ~ || {
echo "cd failed, aborting"
exit 1
}
while [ $# -gt 0 ]; do
case "$1" in
--help | -h)
echo "Options:"
echo
echo " --interactive -i [y|n] Default: y Disables interactive mode"
echo " --mode -m [1|2|3|4|5] Sets the mode (requires --interactive 0, ignored otherwise)"
echo " --max-jobs -j [#] Default: 3 Set maximum number of builds jobs"
echo " --libfdk-aac -a [y|n] Default: y Compile/enable libfdk-aac"
echo " --h264-omx -x [y|n] Default: n Enable h264-omx"
echo " --extra-flags -f [\"--*-* --*-*\"] Default: empty Provides compile flags for ffmpeg"
echo
echo " Example: ./build-debian.sh --interactive n --max-jobs 2 --mode 4"
echo
exit 0
;;
--interactive | -i)
INTERACTIVE="$2"
;;
--mode | -m)
MODE=$2
MODEPARAM=true
;;
--max-jobs | -j)
JOBS=$2
JOBSPARAM=true
;;
--h264-omx | -x)
OMX="$2"
OMXPARAM=true
;;
--libfdk-aac | -a)
FDK="$2"
FDKPARAM=true
;;
--extra-flags | -f)
FLAGSYN="y"
FLAGS="$2"
FLAGSPARAM=true
;;
esac
shift
done
if [ "$INTERACTIVE" = "n" ]; then
printHeader
processOptions $MODE
else
printHeader
menu
fi