ffmpeg ndk

来源:互联网 发布:solidworks有mac版吗 编辑:程序博客网 时间:2024/06/05 19:28

基于eclipse:

1.创建一个android project :kjcodec

2.右键点击kjcodec然后new 一个Folder,名字叫jni

3.把如下FFmpeg已经编译好的动态库以及include中的头文件拷贝进来



4.创建一个class,KUtils.java, 包含本地方法public native int kCodec(String inPath,String outPath);

5.右键src,找到其路径D:\eclipse_ws\kjcodec\src,然后操作如下图来生成.h头文件com_example_kjcodec_KUtils.h,并将其移入jni目录



6.右键点击project,在Android Tools下找到 Add Native Support, 在弹出的对话框点击确定后就会在jni目录下生成一个kjcodec.cpp的文件,我们改名为kjcodec.c



7.这个时候工程可能会有报错,没关系,把如下的选中状态去掉即可



8.然后现在就可以在kjcodec.c 中进行编写代码实现.h文件中的方法了,如果要调用FFmpeg库中的方法,引入头文件即可,调用哪个方法就include哪个头文件

#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
#include "libavutil/log.h"

如果要debug打印日志,加入以下代码就可

#ifdef ANDROID
#include <jni.h>
#include <android/log.h>
#define LOGE(format, ...)  __android_log_print(ANDROID_LOG_ERROR, "(>_<)", format, ##__VA_ARGS__)
#define LOGI(format, ...)  __android_log_print(ANDROID_LOG_INFO,  "(=_=)", format, ##__VA_ARGS__)
#else
#define LOGE(format, ...)  printf("(>_<) " format "\n", ##__VA_ARGS__)
#define LOGI(format, ...)  printf("(^_^) " format "\n", ##__VA_ARGS__)
#endif


9.实现了本地方法之后需要修改一下android.mk文件,将下面框出来的文件改成自己的文件,以下已做修改



10.在第4步创建的类文件中把libs中的所有的库都load进来,并且不能打乱顺序,注意最后一个库就是上一步中LOCAL_MODULE所对应的库名
static {
System.loadLibrary("avutil-54");
System.loadLibrary("swresample-1");
System.loadLibrary("avcodec-56");
System.loadLibrary("avformat-56");
System.loadLibrary("swscale-3");
System.loadLibrary("postproc-53");
System.loadLibrary("avfilter-5");
System.loadLibrary("avdevice-56");
System.loadLibrary("sffdecoder");
}

11.可以编译运行了
:sintel.mp4 的属性可以看到其款高为640*360,所以解码之后播放时也要设置播放器的frame size为640*360再播放,否则播放有问题,播放时一定要用YUV播放器。