11、ffmpeg學習筆記—ffmpeg源碼編譯-Android



所需材料
1、ubuntu_16.04
2、android-ndk-r14b
3、ffmpeg 源碼

1、創建工作目錄

    我是在桌面創建的develop目錄。

2、安裝git工具

    安裝git,用來下載ffmepg源碼。
1
bassy@ubuntu:~/Desktop/develop$ sudo apt-get install git
2
Reading package lists... Done
3
Building dependency tree       
4
Reading state information... Done
5
...

3、下載ffmepg源碼

   進入工作目錄develop,使用git下載ffmepg源碼。
    當然,如果你覺得下載太慢的話,可以去官網使用迅雷等工具下載,然後解壓出來。
    下載完成後,如下:
1
bassy@ubuntu:~/Desktop/develop$ cd ffmpeg/
2
bassy@ubuntu:~/Desktop/develop/ffmpeg$ ll
3
total 1584
4
drwxrwxrwx 18 bassy bassy   4096 Jun  6 10:27 ./
5
drwxrwxr-x  3 bassy bassy   4096 Jun  7 08:08 ../
6
-rwxrw-rw-  1 bassy bassy  56143 Jun  6 10:26 Changelog*
7
-rwxrw-rw-  1 bassy bassy  73279 Jun  6 10:26 cmdutils.c*
8
-rwxrw-rw-  1 bassy bassy  24505 Jun  6 10:26 cmdutils.h*
9
-rwxrw-rw-  1 bassy bassy  10627 Jun  6 10:26 cmdutils_opencl.c*
10
drwxrwxrwx 13 bassy bassy   4096 Jun  6 10:26 compat/
11
-rwxrw-rw-  1 bassy bassy 228905 Jun  6 10:26 configure*
12
-rwxrw-rw-  1 bassy bassy    418 Jun  6 10:26 CONTRIBUTING.md*
13
-rwxrw-rw-  1 bassy bassy  18092 Jun  6 10:26 COPYING.GPLv2*
14
-rwxrw-rw-  1 bassy bassy  35147 Jun  6 10:26 COPYING.GPLv3*
15
-rwxrw-rw-  1 bassy bassy  26526 Jun  6 10:26 COPYING.LGPLv2.1*
16
-rwxrw-rw-  1 bassy bassy   7651 Jun  6 10:26 COPYING.LGPLv3*
17
-rwxrw-rw-  1 bassy bassy    274 Jun  6 10:26 CREDITS*
18
drwxrwxrwx  4 bassy bassy   4096 Jun  6 10:26 doc/
19
drwxrwxrwx  2 bassy bassy   4096 Jun  6 10:26 ffbuild/
20
-rwxrw-rw-  1 bassy bassy 169685 Jun  6 10:26 ffmpeg.c*
21
-rwxrw-rw-  1 bassy bassy   2412 Jun  6 10:26 ffmpeg_cuvid.c*
22
-rwxrw-rw-  1 bassy bassy  15057 Jun  6 10:26 ffmpeg_dxva2.c*
23
-rwxrw-rw-  1 bassy bassy  44795 Jun  6 10:26 ffmpeg_filter.c*
24
-rwxrw-rw-  1 bassy bassy  19841 Jun  6 10:26 ffmpeg.h*
25
-rwxrw-rw-  1 bassy bassy 148299 Jun  6 10:26 ffmpeg_opt.c*
26
-rwxrw-rw-  1 bassy bassy   3064 Jun  6 10:26 ffmpeg_qsv.c*
27
-rwxrw-rw-  1 bassy bassy   6720 Jun  6 10:26 ffmpeg_vaapi.c*
28
-rwxrw-rw-  1 bassy bassy   4469 Jun  6 10:26 ffmpeg_vdpau.c*
29
-rwxrw-rw-  1 bassy bassy   6642 Jun  6 10:26 ffmpeg_videotoolbox.c*
30
-rwxrw-rw-  1 bassy bassy 132011 Jun  6 10:26 ffplay.c*
31
-rwxrw-rw-  1 bassy bassy 134506 Jun  6 10:26 ffprobe.c*
32
-rwxrw-rw-  1 bassy bassy 128352 Jun  6 10:26 ffserver.c*
33
-rwxrw-rw-  1 bassy bassy  51173 Jun  6 10:26 ffserver_config.c*
34
-rwxrw-rw-  1 bassy bassy   5814 Jun  6 10:26 ffserver_config.h*
35
drwxrwxrwx  7 bassy bassy   4096 Jun  6 10:27 .git/
36
-rwxrw-rw-  1 bassy bassy     50 Jun  6 10:26 .gitattributes*
37
-rwxrw-rw-  1 bassy bassy    279 Jun  6 10:26 .gitignore*
38
-rwxrw-rw-  1 bassy bassy    595 Jun  6 10:26 INSTALL.md*
39
drwxrwxrwx 14 bassy bassy  49152 Jun  6 10:26 libavcodec/
40
drwxrwxrwx  3 bassy bassy   4096 Jun  6 10:26 libavdevice/
41
drwxrwxrwx  4 bassy bassy  16384 Jun  6 10:26 libavfilter/
42
drwxrwxrwx  3 bassy bassy  20480 Jun  6 10:26 libavformat/
43
drwxrwxrwx  6 bassy bassy   4096 Jun  6 10:26 libavresample/
44
drwxrwxrwx 12 bassy bassy   4096 Jun  6 10:26 libavutil/
45
drwxrwxrwx  2 bassy bassy   4096 Jun  6 10:26 libpostproc/
46
drwxrwxrwx  6 bassy bassy   4096 Jun  6 10:26 libswresample/
47
drwxrwxrwx  7 bassy bassy   4096 Jun  6 10:26 libswscale/
48
-rwxrw-rw-  1 bassy bassy   4368 Jun  6 10:26 LICENSE.md*
49
-rwxrw-rw-  1 bassy bassy  28407 Jun  6 10:26 MAINTAINERS*
50
-rwxrw-rw-  1 bassy bassy   7142 Jun  6 10:26 Makefile*
51
drwxrwxrwx  2 bassy bassy   4096 Jun  6 10:26 presets/
52
-rwxrw-rw-  1 bassy bassy   1893 Jun  6 10:26 README.md*
53
-rwxrw-rw-  1 bassy bassy      8 Jun  6 10:26 RELEASE*
54
drwxrwxrwx  7 bassy bassy   4096 Jun  6 10:27 tests/
55
drwxrwxrwx  2 bassy bassy   4096 Jun  6 10:27 tools/
56
-rwxrw-rw-  1 bassy bassy    474 Jun  6 10:26 .travis.yml*

4、安裝NDK


    下載之後,解壓到工作目錄develop下,如下所示:
 

5、ffmpeg編譯配置


    在ffmpeg目錄下新建一個文件“build_android.sh”,並輸入如下內容,注意,請根據自己的情況進行適當的修改:
1
#!/bin/bash
2
NDK=/home/bassy/Desktop/develop/android-ndk-r14b
3
SYSROOT=$NDK/platforms/android-14/arch-arm/
4
TOOLCHAIN=$NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64
5
6
CPU=arm
7
PREFIX=$(pwd)/android/$CPU
8
ADDI_CFLAGS="-marm"
9
TARGET_ARCH="arm"
10
11
12
function build_one
13
{
14
./configure \
15
 --prefix=$PREFIX \
16
 --disable-shared \
17
 --enable-static \
18
 --disable-doc \
19
 --disable-ffmpeg \
20
 --disable-ffplay \
21
 --disable-ffprobe \
22
 --disable-ffserver \
23
 --disable-avdevice \
24
 --disable-doc \
25
 --disable-symver \
26
 --cross-prefix=$TOOLCHAIN/bin/arm-linux-androideabi- \
27
 --target-os=android \
28
 --arch=$TARGET_ARCH\
29
 --enable-cross-compile \
30
 --sysroot=$SYSROOT \
31
 --extra-cflags="-Os -fPIC -DANDROID $ADDI_CFLAGS" \
32
 --extra-ldflags="$ADDI_LDFLAGS" \
33
 $ADDITIONAL_CONFIGURE_FLAG
34
make clean
35
make
36
make install
37
}
38
39
build_one
    build_android.sh文件保存之後,需要賦予可執行權限,如下:
    注意,上面有行代碼:“SYSROOT=$NDK/platforms/android-14/arch-arm/”指定了Android Platform的版本,這個很重要的,因爲它決定了你用它開發的時候所能使用的Android SDK的版本。舉個例子,如果這裏填寫的是android-21,開發時minSdkVersion填寫的是android-14,會出現很多奇怪的錯誤。

    如果需要編譯動態庫的話,請修改上面的配置“--disable--shared”改爲“--enable-shared”。

    如需要編譯其它CPU架構的話,可查看https://github.com/RoyGuanyu/build-scripts-of-ffmpeg-x264-for-android-ndk,或者打開ffmpeg目錄下的configure文件,看看裏面聲明的常量值。

    更多配置,請用文本查看器打開“(ffmpeg)/configure”文件,裏面可以找到對應的說明。以下是其中一部分內容。
1
...
2
3
Program options:
4
  --disable-programs       do not build command line programs
5
  --disable-ffmpeg         disable ffmpeg build
6
  --disable-ffplay         disable ffplay build
7
  --disable-ffprobe        disable ffprobe build
8
  --disable-ffserver       disable ffserver build
9
10
Documentation options:
11
  --disable-doc            do not build documentation
12
  --disable-htmlpages      do not build HTML documentation pages
13
  --disable-manpages       do not build man documentation pages
14
  --disable-podpages       do not build POD documentation pages
15
  --disable-txtpages       do not build text documentation pages
16
17
Component options:
18
  --disable-avdevice       disable libavdevice build
19
  --disable-avcodec        disable libavcodec build
20
  --disable-avformat       disable libavformat build
21
  --disable-swresample     disable libswresample build
22
  --disable-swscale        disable libswscale build
23
24
...
    常用的arch有:
類型 名稱
aarch64|arm64 aarch64
arm*|iPad*|iPhone* arm
mips*|IP* mips
parisc*|hppa* parisc
"Power Macintosh"|ppc*|powerpc* ppc
s390|s390x s390
sh4|sh sh4
sun4*|sparc* sparc
tilegx|tile-gx tilegx
i[3-6]86*|i86pc|BePC|x86pc|x86_64|x86_32|amd64 x86
    常用的target_os有:
target_os
android
freebsd
darwin
mingw32*|mingw64*
win32|win64
cygwin*
*-dos|freedos|opendos
linux

6、開始編譯


    進入在ffmpeg目錄,執行 build_android.sh”,如下:
    接下來可以去喝杯水,休閒地等它編譯……快的話,十多分鐘就完成編譯了。
1
...
2
INSTALL libavutil/lzo.h
3
INSTALL libavutil/avconfig.h
4
INSTALL libavutil/ffversion.h
5
INSTALL libavutil/libavutil.pc
6
bassy@ubuntu:~/Desktop/develop/ffmpeg$ 

7、查看編譯結果


    進入在"ffmpeg/android/arm"可以看到編譯結果。
1
bassy@ubuntu:~/Desktop/develop/ffmpeg/android/arm$ ll
2
total 16
3
drwxr-xr-x 4 root root 4096 Jun  7 09:03 ./
4
drwxr-xr-x 3 root root 4096 Jun  7 09:03 ../
5
drwxr-xr-x 8 root root 4096 Jun  7 09:03 include/
6
drwxr-xr-x 3 root root 4096 Jun  7 09:03 lib/
7
bassy@ubuntu:~/Desktop/develop/ffmpeg/android/arm$ ll lib/
8
total 138992
9
drwxr-xr-x 3 root root     4096 Jun  7 09:03 ./
10
drwxr-xr-x 4 root root     4096 Jun  7 09:03 ../
11
-rw-r--r-- 1 root root 90607318 Jun  7 09:03 libavcodec.a
12
-rw-r--r-- 1 root root 13182384 Jun  7 09:03 libavfilter.a
13
-rw-r--r-- 1 root root 33612772 Jun  7 09:03 libavformat.a
14
-rw-r--r-- 1 root root  1636078 Jun  7 09:03 libavutil.a
15
-rw-r--r-- 1 root root   377648 Jun  7 09:03 libswresample.a
16
-rw-r--r-- 1 root root  2886158 Jun  7 09:03 libswscale.a
17
drwxr-xr-x 2 root root     4096 Jun  7 09:03 pkgconfig/
    "include"是頭文件,"lib"是靜態庫,在開發的時候會用到。
  • libavcodec encoding/decoding library
  • libavfilter graph-based frame editing library
  • libavformat I/O and muxing/demuxing library
  • libavdevice special devices muxing/demuxing library
  • libavutil common utility library
  • libswresample audio resampling, format conversion and mixing
  • libpostproc post processing library
  • libswscale color conversion and scaling library

    至此,ffmpeg的編譯已經完成了,直下來將介紹如果編寫第一個ffmpeg程序。


閱讀更多 登錄後自動展開
想對作者說點什麼? 我來說一句

沒有更多推薦了,返回首頁

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章