android ndk x86 arm,NDK r21编译FFmpeg 4.2.2(x86、x86_64、armv7、armv8)

1.编译FFmpeg

准备Ununtu、ndk r21(linux)、FFmpeg。

准备编译脚本,这里有两个,其中一个是专门针对armv7的。

armv7

#!/bin/bash

API=21

#armv7-a

ARCH=armv7

PREFIX=./SO/$ARCH

TOOLCHAIN=/home/qwe/android-ndk-r21/toolchains/llvm/prebuilt/linux-x86_64

build()

{

./configure \

--prefix=$PREFIX \

--disable-static \

--enable-shared \

--enable-small \

--enable-gpl \

--disable-doc \

--disable-programs \

--disable-avdevice \

--enable-cross-compile \

--target-os=android \

--arch=$ARCH \

--cc=$TOOLCHAIN/bin/armv7a-linux-androideabi$API-clang \

--cross-prefix=$TOOLCHAIN/bin/arm-linux-androideabi-

make clean

make -j4

make install

}

build

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

arm64 x86 x86_64

#!/bin/bash

API=21

#arm64  x86 x86_64 对应 aarch64  i686  x86_64

ARCH=arm64

ARCH2=aarch64

PREFIX=./SO/$ARCH

TOOLCHAIN=/home/qwe/android-ndk-r21/toolchains/llvm/prebuilt/linux-x86_64

build()

{

./configure \

--prefix=$PREFIX \

--disable-static \

--enable-shared \

--enable-small \

--disable-doc \

--disable-programs \

--disable-avdevice \

--enable-cross-compile \

--target-os=android \

--arch=$ARCH \

--cc=$TOOLCHAIN/bin/$ARCH2-linux-android$API-clang \

--cross-prefix=$TOOLCHAIN/bin/$ARCH2-linux-android-

make clean

make -j4

make install

}

build

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

在FFmpeg的根目录运行脚本。最初运行会提示缺少软件,按提示进行安装就行。

[email protected]:~/FFmpeg-n4.2.2$ sudo su

[sudo] qwe 的密码:

[email protected]:/home/qwe/FFmpeg-n4.2.2# ./build3.sh

1

2

3

在这里插入图片描述

2.使用FFmpeg的so库

android studio创建项目

在这里插入图片描述

将so文件复制到如下目录。include是FFmpeg的.h文件,x86、x86_64、armv7、armv8生成的.h一样的。

在这里插入图片描述

编辑build.gradle(:app)

android {

.....

sourceSets {

main {//jniLibs

jniLibs.srcDirs = [‘src/main/libs‘]

}

}

}

1

2

3

4

5

6

7

8

编辑cmakeList.txt

cmake_minimum_required(VERSION 3.4.1)

#设置两个目录,${ANDROID_ABI}会根据设备变化

set(DIR ${PROJECT_SOURCE_DIR}/../libs/${ANDROID_ABI})

set(DIR2 ${PROJECT_SOURCE_DIR}/../libs)

#指定h文件的目录,编译过程需要找.h文件

include_directories(${DIR2}/include)

add_library( # Sets the name of the library.

native-lib

# Sets the library as a shared library.

SHARED

# Provides a relative path to your source file(s).

native-lib.cpp)

#导入FFmpeg的库

add_library(libavcodec SHARED IMPORTED)

#so文件的位置

set_target_properties(libavcodec

PROPERTIES IMPORTED_LOCATION

${DIR}/libavcodec.so)

add_library(libavfilter SHARED IMPORTED)

set_target_properties(libavfilter

PROPERTIES IMPORTED_LOCATION

${DIR}/libavfilter.so)

add_library(libavformat SHARED IMPORTED)

set_target_properties(libavformat

PROPERTIES IMPORTED_LOCATION

${DIR}/libavformat.so)

add_library(libavutil SHARED IMPORTED)

set_target_properties(libavutil

PROPERTIES IMPORTED_LOCATION

${DIR}/libavutil.so)

add_library(libswresample SHARED IMPORTED)

set_target_properties(libswresample

PROPERTIES IMPORTED_LOCATION

${DIR}/libswresample.so)

add_library(libswscale SHARED IMPORTED)

set_target_properties(libswscale

PROPERTIES IMPORTED_LOCATION

${DIR}/libswscale.so)

find_library( # Sets the name of the path variable.

log-lib

log)

#连接库

target_link_libraries( # Specifies the target library.

native-lib

# Links the target library to the log library

# included in the NDK.

${log-lib}

libavfilter

libavformat

libswresample

libswscale

libavutil

libavcodec)

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

更改native-lib.cpp中的stringFromJNI方法

#include

#include

extern  "C"{

#include "libavcodec/avcodec.h"

}

extern "C" JNIEXPORT jstring JNICALL

Java_cn_study_testffmpeg_MainActivity_stringFromJNI(

JNIEnv *env,

jobject /* this */) {

std::string hello = avcodec_configuration();

return env->NewStringUTF(hello.c_str());

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

运行效果

原文:https://www.cnblogs.com/YZFHKMS-X/p/13228038.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值