今天折腾了一天,可算是有所收获,成功的用jni调用了libffmpeg中的一个方法-----avcodec_version(),至于avcodec_version()是干什么用的我不大清楚,应该是获取版本信息吧,没有深入的去研究ffmpeg。

这里主要是想把折腾一天所获取的经验记录下来,以免时间长全忘了,也希望能给其他人一点借鉴,不至于和我一样一点头绪都没有连猜带蒙的,本文纯属个人心得,高手可以无视....

要在android上用ffmpeg首先得奖ffmpeg工程移植到android上,这里就要用到ndk把这个开源工程编译成一个后缀为so的库,这个步骤这里就不多说了 网上的资料也挺多的,我是按照:http://www.cnblogs.com/scottwong/archive/2010/12/17/1909455.html在ubantu环境下编译的,你按照教程上一步一步来应该都没有问题,顺便给下在windows下编译ffmpeg的教程:http://abitno.me/compile-ffmpeg-android-ndk(这个要用非ie浏览器打开)。以上两篇文章给了我很大的指引,在此谢过。。。都是牛人啊~~~

编译完以后你会获得一个libffmpeg.so的文件,那么问题来了,怎么用呢。我在百度,google搜了半天也没有一个详细的教程,总是东一句西一句的,但思路是明确的,就是还得编译一个so文件,这个so里的是jni方法,可以由java层调用的,而这些jni方法里用到的函数则就是来至libffmpeg.so了。思路是有了,但是具体怎么做呢?又经过一顿摸索,n次的编译,终于编译成功了。我是拿一个标准的ndk例子来做的测试就是ndk samples文件夹里的hello-jni工程。进入该工程的jni目录,将ffmpeg的源代码拷到该目录下,做这部的原因是你要编译的so文件里需要调用ffmpeg的方法,自然要引用ffmpeg里的h文件,然后将libffmpeg.so文件拷到ndk目录下的platforms/android-5/arch-arm/usr/lib目录下(你会发现platfroms里有好几个android文件夹如 -3 -4 -5分别代表不同的版本,以防万一我每个目录都拷了,呵呵,应该是只要拷指定目录的),因为等等系统编译的时候要用。接下来就编辑android.mk和hello-jni.c文件了 代码如下

android.mk

  1. # Copyright (C) 2009 The Android Open Source Project
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. #      http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. #
  15. LOCAL_PATH := $(call my-dir)
  16. include $(CLEAR_VARS)
  17. PATH_TO_FFMPEG_SOURCE:=$(LOCAL_PATH)/ffmpeg
  18. LOCAL_C_INCLUDES += $(PATH_TO_FFMPEG_SOURCE)
  19. LOCAL_LDLIBS := -lffmpeg
  20. LOCAL_MODULE    := hello-jni
  21. LOCAL_SRC_FILES := hello-jni.c
  22. include $(BUILD_SHARED_LIBRARY)

PATH_TO_FFMPEG_SOURCE:=$(LOCAL_PATH)/ffmpeg

这行是定义一个变量,也就是ffmpeg源码的路径

LOCAL_C_INCLUDES += $(PATH_TO_FFMPEG_SOURCE)
这行是指定源代码的路径,也就是刚才拷过去的ffmpeg源码,$(LOCAL_PATH)是根目录,如果没有加这行那么引入ffmpeg库中的h文件编译就会出错说找不到该h文件。

LOCAL_LDLIBS := -lffmpeg
这行很重要,这是表示你这个so运行的时候依赖于libffmpeg.so这个库, 再举个例子:如果你要编译的so不仅要用到libffmpeg.so这个库还要用的libopencv.so这个库的话,你这个参数就应该写成

LOCAL_LDLIBS := -lffmpeg -lopencv

其他的参数都是正常的ndk编译用的了,不明白的话google一下。

hello-jni.c

  1. /*
  2. * Copyright (C) 2009 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. *      http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. #include <string.h>
  18. #include <stdio.h>
  19. #include <android/log.h>
  20. #include <stdlib.h>
  21. #include <jni.h>
  22. #include <ffmpeg/libavcodec/avcodec.h>
  23. /* This is a trivial JNI example where we use a native method
  24. * to return a new VM String. See the corresponding Java source
  25. * file located at:
  26. *
  27. *   apps/samples/hello-jni/project/src/com/example/HelloJni/HelloJni.java
  28. */
  29. jstring
  30. Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,
  31. jobject thiz )
  32. {
  33. char str[25];
  34. sprintf(str, "%d", avcodec_version());
  35. return (*env)->NewStringUTF(env, str);
  36. }

#include <ffmpeg/libavcodec/avcodec.h>
这行是因为下面要用到avcodec_version()这个函数。

改完这两个文件以后就可以编译了~~用ndk-build命令编译完后在工程的libs/armeabi目录底下就会有一个libhello-jni.so文件了!(两行眼泪啊~终于编译成功了)

编译完成后就可以进行测试了,记得将libffmpeg.so也拷到armeabi目录底下,并在java代码中写上

  1. static {
  2. System.loadLibrary("ffmpeg");
  3. System.loadLibrary("hello-jni");
  4. }

HelloJni.java

  1. /*
  2. * Copyright (C) 2009 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. *      http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.example.hellojni;
  17. import android.app.Activity;
  18. import android.widget.TextView;
  19. import android.os.Bundle;
  20. public class HelloJni extends Activity
  21. {
  22. /** Called when the activity is first created. */
  23. @Override
  24. public void onCreate(Bundle savedInstanceState)
  25. {
  26. super.onCreate(savedInstanceState);
  27. /* Create a TextView and set its content.
  28. * the text is retrieved by calling a native
  29. * function.
  30. */
  31. TextView  tv = new TextView(this);
  32. tv.setText( "1111" );
  33. //System.out.println();
  34. setContentView(tv);
  35. tv.setText(String.valueOf(stringFromJNI()));
  36. }
  37. /* A native method that is implemented by the
  38. * 'hello-jni' native library, which is packaged
  39. * with this application.
  40. */
  41. public native String  stringFromJNI();
  42. /* This is another native method declaration that is *not*
  43. * implemented by 'hello-jni'. This is simply to show that
  44. * you can declare as many native methods in your Java code
  45. * as you want, their implementation is searched in the
  46. * currently loaded native libraries only the first time
  47. * you call them.
  48. *
  49. * Trying to call this function will result in a
  50. * java.lang.UnsatisfiedLinkError exception !
  51. */
  52. public native String  unimplementedStringFromJNI();
  53. /* this is used to load the 'hello-jni' library on application
  54. * startup. The library has already been unpacked into
  55. * /data/data/com.example.HelloJni/lib/libhello-jni.so at
  56. * installation time by the package manager.
  57. */
  58. static {
  59. System.loadLibrary("ffmpeg");
  60. System.loadLibrary("hello-jni");
  61. }
  62. }

到此就完成了,将程序装到手机可看到打印出“3426306”,google搜索“ffmpeg 3426306”得知果然是ffmpeg的东西,证明成功的调用了libffmpeg.so库里的方法了。欣慰啊~~

接下来要做的就是学习ffmpeg库里的各种函数的使用方法,以实现自己想要的功能了。

今天折腾了一天,可算是有所收获,成功的用jni调用了libffmpeg中的一个方法-----avcodec_version(),至于avcodec_version()是干什么用的我不大清楚,应该是获取版本信息吧,没有深入的去研究ffmpeg。

这里主要是想把折腾一天所获取的经验记录下来,以免时间长全忘了,也希望能给其他人一点借鉴,不至于和我一样一点头绪都没有连猜带蒙的,本文纯属个人心得,高手可以无视....

要在android上用ffmpeg首先得奖ffmpeg工程移植到android上,这里就要用到ndk把这个开源工程编译成一个后缀为so的库,这个步骤这里就不多说了 网上的资料也挺多的,我是按照:http://www.cnblogs.com/scottwong/archive/2010/12/17/1909455.html在ubantu环境下编译的,你按照教程上一步一步来应该都没有问题,顺便给下在windows下编译ffmpeg的教程:http://abitno.me/compile-ffmpeg-android-ndk(这个要用非ie浏览器打开)。以上两篇文章给了我很大的指引,在此谢过。。。都是牛人啊~~~

编译完以后你会获得一个libffmpeg.so的文件,那么问题来了,怎么用呢。我在百度,google搜了半天也没有一个详细的教程,总是东一句西一句的,但思路是明确的,就是还得编译一个so文件,这个so里的是jni方法,可以由java层调用的,而这些jni方法里用到的函数则就是来至libffmpeg.so了。思路是有了,但是具体怎么做呢?又经过一顿摸索,n次的编译,终于编译成功了。我是拿一个标准的ndk例子来做的测试就是ndk samples文件夹里的hello-jni工程。进入该工程的jni目录,将ffmpeg的源代码拷到该目录下,做这部的原因是你要编译的so文件里需要调用ffmpeg的方法,自然要引用ffmpeg里的h文件,然后将libffmpeg.so文件拷到ndk目录下的platforms/android-5/arch-arm/usr/lib目录下(你会发现platfroms里有好几个android文件夹如 -3 -4 -5分别代表不同的版本,以防万一我每个目录都拷了,呵呵,应该是只要拷指定目录的),因为等等系统编译的时候要用。接下来就编辑android.mk和hello-jni.c文件了 代码如下

android.mk

  1. # Copyright (C) 2009 The Android Open Source Project
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. #      http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. #
  15. LOCAL_PATH := $(call my-dir)
  16. include $(CLEAR_VARS)
  17. PATH_TO_FFMPEG_SOURCE:=$(LOCAL_PATH)/ffmpeg
  18. LOCAL_C_INCLUDES += $(PATH_TO_FFMPEG_SOURCE)
  19. LOCAL_LDLIBS := -lffmpeg
  20. LOCAL_MODULE    := hello-jni
  21. LOCAL_SRC_FILES := hello-jni.c
  22. include $(BUILD_SHARED_LIBRARY)

PATH_TO_FFMPEG_SOURCE:=$(LOCAL_PATH)/ffmpeg

这行是定义一个变量,也就是ffmpeg源码的路径

LOCAL_C_INCLUDES += $(PATH_TO_FFMPEG_SOURCE)
这行是指定源代码的路径,也就是刚才拷过去的ffmpeg源码,$(LOCAL_PATH)是根目录,如果没有加这行那么引入ffmpeg库中的h文件编译就会出错说找不到该h文件。

LOCAL_LDLIBS := -lffmpeg
这行很重要,这是表示你这个so运行的时候依赖于libffmpeg.so这个库, 再举个例子:如果你要编译的so不仅要用到libffmpeg.so这个库还要用的libopencv.so这个库的话,你这个参数就应该写成

LOCAL_LDLIBS := -lffmpeg -lopencv

其他的参数都是正常的ndk编译用的了,不明白的话google一下。

hello-jni.c

  1. /*
  2. * Copyright (C) 2009 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. *      http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. #include <string.h>
  18. #include <stdio.h>
  19. #include <android/log.h>
  20. #include <stdlib.h>
  21. #include <jni.h>
  22. #include <ffmpeg/libavcodec/avcodec.h>
  23. /* This is a trivial JNI example where we use a native method
  24. * to return a new VM String. See the corresponding Java source
  25. * file located at:
  26. *
  27. *   apps/samples/hello-jni/project/src/com/example/HelloJni/HelloJni.java
  28. */
  29. jstring
  30. Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,
  31. jobject thiz )
  32. {
  33. char str[25];
  34. sprintf(str, "%d", avcodec_version());
  35. return (*env)->NewStringUTF(env, str);
  36. }

#include <ffmpeg/libavcodec/avcodec.h>
这行是因为下面要用到avcodec_version()这个函数。

改完这两个文件以后就可以编译了~~用ndk-build命令编译完后在工程的libs/armeabi目录底下就会有一个libhello-jni.so文件了!(两行眼泪啊~终于编译成功了)

编译完成后就可以进行测试了,记得将libffmpeg.so也拷到armeabi目录底下,并在java代码中写上

  1. static {
  2. System.loadLibrary("ffmpeg");
  3. System.loadLibrary("hello-jni");
  4. }

HelloJni.java

  1. /*
  2. * Copyright (C) 2009 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. *      http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.example.hellojni;
  17. import android.app.Activity;
  18. import android.widget.TextView;
  19. import android.os.Bundle;
  20. public class HelloJni extends Activity
  21. {
  22. /** Called when the activity is first created. */
  23. @Override
  24. public void onCreate(Bundle savedInstanceState)
  25. {
  26. super.onCreate(savedInstanceState);
  27. /* Create a TextView and set its content.
  28. * the text is retrieved by calling a native
  29. * function.
  30. */
  31. TextView  tv = new TextView(this);
  32. tv.setText( "1111" );
  33. //System.out.println();
  34. setContentView(tv);
  35. tv.setText(String.valueOf(stringFromJNI()));
  36. }
  37. /* A native method that is implemented by the
  38. * 'hello-jni' native library, which is packaged
  39. * with this application.
  40. */
  41. public native String  stringFromJNI();
  42. /* This is another native method declaration that is *not*
  43. * implemented by 'hello-jni'. This is simply to show that
  44. * you can declare as many native methods in your Java code
  45. * as you want, their implementation is searched in the
  46. * currently loaded native libraries only the first time
  47. * you call them.
  48. *
  49. * Trying to call this function will result in a
  50. * java.lang.UnsatisfiedLinkError exception !
  51. */
  52. public native String  unimplementedStringFromJNI();
  53. /* this is used to load the 'hello-jni' library on application
  54. * startup. The library has already been unpacked into
  55. * /data/data/com.example.HelloJni/lib/libhello-jni.so at
  56. * installation time by the package manager.
  57. */
  58. static {
  59. System.loadLibrary("ffmpeg");
  60. System.loadLibrary("hello-jni");
  61. }
  62. }

到此就完成了,将程序装到手机可看到打印出“3426306”,google搜索“ffmpeg 3426306”得知果然是ffmpeg的东西,证明成功的调用了libffmpeg.so库里的方法了。欣慰啊~~

接下来要做的就是学习ffmpeg库里的各种函数的使用方法,以实现自己想要的功能了。

android 移植ffmpeg后so库的使用的更多相关文章

  1. Cocos2d-X学习——Android移植,使用第三方库.so被删掉问题

    2014-05-26 导语:Cocos2dx在安卓上移植的时候,增加第三方库,却发现新加的so库被删掉了. 正文: 1.我的环境: cocos2d-x 2.2.3, ndk-r9 2.网上找了非常多, ...

  2. Arm-Linux 移植 FFMPEG库 + x264

      背景: ffmpeg 中带有264的解码,没有编码,需要添加x264.libx264是一个自由的H.264编码库,是x264项目的一部分,使用广泛,ffmpeg的H.264实现就是用的libx26 ...

  3. ffmpeg android移植

    CMake语法简介(androidstudio中利用CMake开发NDK): http://blog.csdn.net/u013718120/article/details/62883711FFmpe ...

  4. [原]如何在Android用FFmpeg+SDL2.0解码图像线程

    关于如何在Android上用FFmpeg+SDL2.0解码显示图像参考[原]如何在Android用FFmpeg+SDL2.0解码显示图像 ,关于如何在Android使用FFmpeg+SDL2.0解码声 ...

  5. [原]如何在Android用FFmpeg+SDL2.0解码显示图像

    如何在Android上使用FFmpeg解码图像参考文章[原]如何在Android用FFmpeg解码图像 ,如何在Android上使用SDL2.0来显示图像参考[原]零基础学习SDL开发之在Androi ...

  6. ffmpeg利用libav库把yuv视频流转换为TS串流

    今天到月末了,才发我这个月的第一篇文章,因为这个月前三周一直在看ffmpeg的libavcodec和libavformat两个库源码.实验室要做一个“小传大”的软件,就是android手机或平板电脑的 ...

  7. Android非常有用的开源库介绍整理

    Android开源库 自己一直很喜欢Android开发,就如博客副标题一样,我想做个好的App. 在摸索过程中,GitHub上搜集了很多很棒的Android第三方库,推荐给在苦苦寻找的开发者,而且我会 ...

  8. 编写ios和android共用的c/c++库时 使用iconv的问题(转)

    因为在项目中需要同时维护ios和Android,不同的代码不利于开发的便捷和以后的维护,所以在最近的一个项目中,两种手机应用的通信部分打算使用c/c++库来统一编写,ios调用.a静态库,androi ...

  9. 初学者的Android移植:在Debian上建立一个稳定的构建环境

    介绍 通过在chrooted环境中设置开发环境,避免依赖冲突和沙箱您的Android开发从您的Debian GNU/Linux系统.这是为通配符类别准备的,因为从源代码构建Android似乎没有在其他 ...

随机推荐

  1. PostgreSQL Replication之第九章 与pgpool一起工作(5)

    9.5 检查复制 如果所有的节点都处于开机并运行的状态.我们就可以在集群上运行我们的第一个操作了.在我们的例子中,我们将简单地连接到pgpool并创建一个新的数据库.createdb 是一个命令行工具 ...

  2. linux上将另一个文件内容快速写入正在编辑的文件内

    一.我们看到 www 目录下有两个文件.like.php 内有一行字母,而 loo.php 内什么也没有. 二 .我们来编辑 loo.php. 三.用下面的命令将 like.php 的内容复制到 lo ...

  3. 紫书 例题 10-27 UVa 10214(欧拉函数)

    只看一个象限简化问题,最后答案乘4+4 象限里面枚举x, 在当前这条固定的平行于y轴的直线中 分成长度为x的一段段.符合题目要求的点gcd(x,y) = 1 那么第一段1<= y <= x ...

  4. 减少UIViewController切换的耦合

    我们一般切换UIViewController的时候用的是例如以下代码 #import "UIViewControllerDemo.h" UIViewControllerDemo * ...

  5. windows linux 双系统默认启动windows 的几种方法

    装了双系统后,在开机时总会有想让一个系统默认启动的时候,一般安装完Ubuntu和XP双系统后,开机时默认的是启动Ubuntu系统,可是当想让XP作为默认启动的系统时怎么办呢? 在早期的Ubuntu系统 ...

  6. (2) 我的结果- spec2006中精确的simulation points运行点

    spec06中获取simpoints的环境说明: spec的版本号为spec2006v1.0; 使用ref input with runspec; 100millions为周期生成的simpoints ...

  7. vue25---vue2.0变化

    组件模版: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  8. js插件---图片懒加载echo.js结合 Amaze UI ScrollSpy 使用

    js插件---图片懒加载echo.js结合 Amaze UI ScrollSpy 使用 一.总结 一句话总结:图片懒加载echo.js结合 Amaze UI ScrollSpy 使用的效果就是:懒加载 ...

  9. quartz-misfire 错失、补偿执行

    调度(scheduleJob)或恢复调度(resumeTrigger,resumeJob)后不同的misfire对应的处理规则 misfire产生的条件是:到了该触发执行时上一个执行还未完成,且线程池 ...

  10. Ubuntu源配置

    一.图形界面配置 新手推荐使用图形界面配置: 系统工具 -> 软件和更新-> Ubuntu软件-> 下载自:-> 其他站点  点击 选择最佳服务器(将通过连接测试确定最佳镜像) ...