-
Notifications
You must be signed in to change notification settings - Fork 0
/
ffmpeg-linux.sh
executable file
·505 lines (402 loc) · 11.1 KB
/
ffmpeg-linux.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
#/bin/bash
# info
VERSION=0.1
AUTHOR=wzjing
DESCRIPTION="this script can build ffmpeg lib"
# command line argment
ARGS=`getopt -o vhy --long version,help,yes,enable-libx264,enable-text -n 'testh.sh' -- "$@"`
if [[ $? != 0 ]]; then
echo "invalid command, use -h/--help to see how to use."
exit 1;
fi
# Congifure deault values
DEBUG=false
MAX_TH=$(cat /proc/cpuinfo | grep "processor" | wc -l)
ENABLE_X264=false
ENABLE_FDK_AAC=false
ENABLE_TEXT=false
EXTRA_CFLAGS="-fPIC -fPIE"
EXTRA_LDFLAGS="-fpie"
ASM_CONFIG=""
ALL_YES=false
FFMPEG_SOURCE=`pwd`/ffmpeg
X264_SOURCE=`pwd`/x264
FREE_TYPE_SOURCE=`pwd`/freetype
FONT_CONFIG_SOURCE=`pwd`/fontconfig
XML2_SOURCE=`pwd`/xml2
# FRIBIDI_SOURCE=`/fribidi`
FF_FLAGS="--disable-avdevice \
--disable-postproc \
--disable-decoders \
--disable-encoders \
--disable-muxers \
--disable-demuxers \
--enable-decoder=aac \
--enable-decoder=mp3 \
--enable-decoder=h263 \
--enable-decoder=h264 \
--enable-decoder=hevc \
--enable-decoder=mpeg4 \
--enable-decoder=png \
--enable-decoder=gif \
--enable-encoder=aac \
--enable-encoder=h263 \
--enable-encoder=libx264 \
--enable-encoder=mpeg4 \
--enable-encoder=png \
--enable-encoder=gif \
--enable-bsf=h264_mp4toannexb \
--enable-bsf=hevc_mp4toannexb \
--enable-parser=aac \
--enable-parser=aac_latm \
--enable-parser=mpegaudio \
--enable-parser=mpegvideo \
--enable-parser=mpeg4video \
--enable-parser=h263 \
--enable-parser=h264 \
--enable-parser=hevc \
--enable-parser=gif \
--enable-parser=png \
--enable-demuxer=flv \
--enable-demuxer=gif \
--enable-demuxer=h264 \
--enable-demuxer=hevc \
--enable-demuxer=aac \
--enable-demuxer=mp3 \
--enable-demuxer=mov \
--enable-demuxer=mpegts \
--enable-demuxer=mpegtsraw \
--enable-demuxer=mpegvideo \
--enable-muxer=flv \
--enable-muxer=mp3 \
--enable-muxer=latm \
--enable-muxer=adts \
--enable-muxer=mov \
--enable-muxer=mp4 \
--enable-muxer=gif \
--enable-muxer=mpegts"
debug() {
if $DEBUG
then
echo -e $*
fi
}
info() {
echo -e "\033[36m$*\033[0m"
}
log() {
echo -e $*
}
warn() {
echo -e "\033[33m$*\033[0m"
}
error() {
echo -e "\033[31m$*\033[0m"
}
success() {
echo -e "\033[32m$*\033[0m"
}
die() {
error $*
exit 1
}
lib_info() {
if $1
then
echo -n "enable"
else
echo "disable"
return
fi
$2 && echo "(source code: yes)" || echo "(source code: no)"
}
make_choice() {
warn "$1 Y/N[Y]"
if $ALL_YES
then
choice=Y
else
read -n1 choice
fi
if [[ $choice =~ ^[yY] || -z $choice ]]
then
return 0
elif [[ $choice =~ ^[nN] ]]
then
echo ""
return 1
else
choice=""
warn -n "invalid choice '$choice' use Y or N: "
make_choice
fi
}
build_xml2() {
if [[ -e $BUILD_DIR/lib/libxml2.so ]]
then
make_choice "found exist libxml2 builds, use it?" && return 0
fi
log "libxml2: start build..."
cd $XML2_SOURCE
log "libxml2 configuring..."
./autogen.sh --prefix=$1 \
--disable-static \
--enable-shared \
--with-debug=off || die "libxml2: autogen.sh failed"
./configure --prefix=$1 || die "libxml2: configure fialed"
make_choice "libxml2: configure done, start build?" || exit 0
make clean
make -j $MAX_TH || die "libxml2: build failed"
make install
success "libxml2: build finished"
return 0
}
build_fontconfig() {
if [[ -e $BUILD_DIR/lib/libfontconfig.so ]]
then
make_choice "found exist libfontconfig builds, use it?" && return 0
fi
log "libfontconfig: start build..."
cd $FONT_CONFIG_SOURCE
log "libfontconfig configuring..."
./autogen.sh --prefix=$1 \
--disable-static \
--enable-shared \
--enable-libxml2 || die "libfontconfig: autogen.sh failed"
./configure --prefix=$1 || die "libfontconfig: configure fialed"
make_choice "libfontconfig: configure done, start build?" || exit 0
make clean
make -j $MAX_TH || die "libfontconfig: build failed"
make install
success "libfontconfig: build finished"
return 0
}
build_freetype() {
if [[ -e $BUILD_DIR/lib/libfreetype.so ]]
then
make_choice "found exist libfreetype builds, use it?" && return 0
fi
log "libfreetype: start build..."
cd $FREE_TYPE_SOURCE
log "libfreetype configuring..."
./autogen.sh --prefix=$1 \
--disable-static \
--enable-shared || die "libfreetype: autogen.sh failed"
./configure --prefix=$1 || die "libfreetype: configure failed"
make_choice "libfreetype: configure done, start build?" || exit 0
make clean
make -j $MAX_TH || die "libfreetype: build failed"
make install
success "libfreetype: build finished"
return 0
}
build_x264() {
if [[ -e $BUILD_DIR/lib/libx264.so ]]
then
make_choice "found exist libx264 builds, use it?" && return 0
fi
log "libx264: start build..."
cd $X264_SOURCE
log "libx264: configuring..."
./configure --prefix=$1 \
--host=x86_64-linux \
--disable-static \
--enable-shared \
--enable-pic \
--enable-strip \
--extra-cflags="$EXTRA_CFLAGS" \
--extra-ldflags="$EXTRA_LDFLAGS" \
--disable-cli \
$ASM_CONFIG || die "libx264: configure failed"
warn "CFLAGS: $EXTRA_CFLAGS"
warn "LDLAGS: $EXTRA_LDFLAGS"
make_choice "libx264: configure done, start build?" || exit 0
make clean
make -j $MAX_TH || die "libx264: build failed"
make install
success "libx264: build finished"
return 0
}
build_ffmpeg() {
log "ffmpeg: start build..."
log "ffmpeg: check pkg-config"
pkg-config --libs x264 freetype2
pkg-config --cflags x264 freetype2
log "ffmpeg: configuring..."
cd $FFMPEG_SOURCE
./configure $FF_FLAGS \
--enable-gpl \
--enable-version3 \
--enable-nonfree \
--target-os=linux \
--disable-static \
--enable-shared \
--enable-pic \
--arch=x86_64 \
--prefix=$1 \
--enable-small \
--disable-programs \
--disable-doc \
--enable-debug \
--logfile=$CONFIGURE_LOG \
--cc=$CC \
--cxx=$CXX \
--objcc=$CC \
--dep-cc=$CC \
--pkg-config=pkg-config \
--extra-cflags="$EXTRA_CFLAGS" \
--extra-ldflags="$EXTRA_LDFLAGS" \
$ASM_CONFIG || die "ffmpeg: configure failed"
warn "CFLAGS: $EXTRA_CFLAGS"
warn "LDLAGS: $EXTRA_LDFLAGS"
make_choice "ffmpeg: configure done, start compile?" || exit 0
make clean
make -j $MAX_TH || die "ffmpeg: build failed"
make install
success "ffmpeg: build finished"
}
eval set -- "${ARGS}"
while true
do
case "$1" in
-v|--version)
cat <<EOF
Version: $VERSION
Author: $AUTHOR
Release date: 2019/5/1
EOF
exit 0
;;
-h|--help)
cat <<EOF
This is script to compile ffmpeg
Usage: $0 --arch <arch> --api <min-version>
Options:
--arch <arch> cpu arch [arm, arm64, x86, x86_64]
--api <min-version> min api support, the lowest version is 16
if use 64-bit arch like arm64 and x86_64
api must greater or equal than 21
--enable-libx264 enable libx264 for H264 codec
--enable-libfdk-aac enable libfdk-aac for aac codec
--enable-text enable text libs for draw text
Example:
$0 --arch arm64 --api 21 --enable-libx264
EOF
exit 0
;;
--enable-libx264)
debug x264
ENABLE_X264=true
FF_FLAGS+=" --enable-libx264"
shift 1
;;
--enable-text)
debug text
FF_FLAGS+=" --enable-libfreetype --enable-libfontconfig"
ENABLE_TEXT=true
shift 1
;;
-y|--yes)
debug yes
ALL_YES=true
shift 1
;;
--)
debug --
shift
break
;;
*)
debug *
echo $1
die "use -h or --help to see how to use this script"
;;
esac
done
ARCH=x64
BUILD_DIR=`pwd`/build/$ARCH
CONFIGURE_LOG=$BUILD_DIR/configure.log
CONFIGURE_EXEC_LOG=$BUILD_DIR/configure_exec.log
test -e $FFMPEG_SOURCE/configure && FFMPEG_SOURCE_EXIST=true || FFMPEG_SOURCE_EXIST=false
test -e $X264_SOURCE/configure && X264_SOURCE_EXIST=true || X264_SOURCE_EXIST=false
test -e $FREE_TYPE_SOURCE/autogen.sh && FREE_TYPE_SOURCE_EXIST=true || FREE_TYPE_SOURCE_EXIST=false
test -e $FONT_CONFIG_SOURCE/autogen.sh && FONT_CONFIG_SOURCE_EXIST=true || FONT_CONFIG_SOURCE_EXIST=false
test -e $XML2_SOURCE/configure && XML2_SOURCE_EXIST=true || XML2_SOURCE_EXIST=false
# test -e $FRIBIDI_SOURCE/configure && FRIBIDI_SOURCE_EXIST=true || FRIBIDI_SOURCE_EXIST=false
if [[ $ARCH =~ ^x86* ]]
then
ASM_CONFIG="--disable-asm"
else
ASM_CONFIG=""
fi
# configure information
TOOLCHAIN=$NDK_ROOT/toolchains/llvm/prebuilt/linux-x86_64
cat <<EOF
Build info:
Libs:
ffmpeg `lib_info true $FFMPEG_SOURCE_EXIST`
libx264: `lib_info $ENABLE_X264 $X264_SOURCE_EXIST`
libfreetype: `lib_info $ENABLE_TEXT $FREE_TYPE_SOURCE_EXIST`
libfontconfig: `lib_info $ENABLE_TEXT $FONT_CONFIG_SOURCE_EXIST`
libxml2: `lib_info $ENABLE_TEXT $XML2_SOURCE_EXIST`
Build options:
CPU: $ARCH
MIN_API: $MIN_API
NDK: $NDK_ROOT
LOG: $CONFIGURE_LOG
BUILD_DIR: $BUILD_DIR
TRHEADS: $MAX_TH
TOOLCHAIN $TOOLCHAIN
ASM $ASM_CONFIG
EOF
make_choice "All configure OK?" || exit 0
info "> STEP 1/5 Check source code"
if ! $FFMPEG_SOURCE_EXIST
then
warn "> Cloning ffmpeg source code"
git clone https://git.ffmpeg.org/ffmpeg.git $FFMPEG_SOURCE
fi
if ! $X264_SOURCE_EXIST
then
warn "> Cloning x264 source code"
git clone https://code.videolan.org/videolan/x264.git $X264_SOURCE
fi
if ! $XML2_SOURCE_EXIST
then
warn "> Cloning xml2 source code"
git clone https://gitlab.gnome.org/GNOME/libxml2.git xml2
fi
if ! $FONT_CONFIG_SOURCE_EXIST
then
warn "> Cloning fontconfig source code"
git clone https://gitlab.freedesktop.org/fontconfig/fontconfig.git fontconfig
fi
if ! $FREE_TYPE_SOURCE_EXIST
then
warn "> Cloning freetype source code"
git clone https://git.savannah.gnu.org/git/freetype/freetype2.git freetype
fi
success "Source code is ok"
info "> STEP 2/5 Initial Toolchain"
export CC=clang-8
export CXX=clang++-8
cat <<EOF
Toolchain info:
CC: $CC (`$CC --version | grep -E "\bclang\sversion\s[0-9.]+" -o`)
CXX: $CXX (`$CXX --version | grep -E "\bclang++\sversion\s[0-9.]+" -o`)
AS $AS (`$AS --version | grep -E "\bclang\sversion\s[0-9.]+" -o`)
AR: $AR (`$AR --version | head -1`)
LD: $LD (`$LD --version | head -1`)
STRIP: $STRIP (`$STRIP --version | head -1`)
EOF
make_choice "Toolchain configure finished, continue?" || exit 0
info "> STEP 3/5 Build libraries"
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:$BUILD_DIR/lib/pkgconfig
$ENABLE_TEXT && build_xml2 $BUILD_DIR
$ENABLE_TEXT && build_freetype $BUILD_DIR
$ENABLE_TEXT && build_fontconfig $BUILD_DIR
$ENABLE_X264 && build_x264 $BUILD_DIR
info "> STEP 4/5 Build ffmpeg"
log "Start build ffmpeg: "
build_ffmpeg $BUILD_DIR
info "> All Finished"