android 移植ffmpeg后so庫的使用


    今天折騰了一天,可算是有所收獲,成功的用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

C代碼   收藏代碼
  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.   
  17. include $(CLEAR_VARS)  
  18. PATH_TO_FFMPEG_SOURCE:=$(LOCAL_PATH)/ffmpeg  
  19. LOCAL_C_INCLUDES += $(PATH_TO_FFMPEG_SOURCE)  
  20. LOCAL_LDLIBS := -lffmpeg  
  21. LOCAL_MODULE    := hello-jni  
  22. LOCAL_SRC_FILES := hello-jni.c  
  23.   
  24. 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

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.   
  36.   
  37.     return (*env)->NewStringUTF(env, str);  
  38. }  

 #include <ffmpeg/libavcodec/avcodec.h>
這行是因為下面要用到avcodec_version()這個函數。

 

    改完這兩個文件以后就可以編譯了~~用ndk-build命令編譯完后在工程的libs/armeabi目錄底下就會有一個libhello-jni.so文件了!(兩行眼淚啊~終於編譯成功了)

    編譯完成后就可以進行測試了,記得將libffmpeg.so也拷到armeabi目錄底下,並在java代碼中寫上

Java代碼   收藏代碼
  1. static {  
  2.        System.loadLibrary("ffmpeg");  
  3.        System.loadLibrary("hello-jni");  
  4.     }  

     HelloJni.java

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.   
  18. import android.app.Activity;  
  19. import android.widget.TextView;  
  20. import android.os.Bundle;  
  21.   
  22.   
  23. public class HelloJni extends Activity  
  24. {  
  25.     /** Called when the activity is first created. */  
  26.     @Override  
  27.     public void onCreate(Bundle savedInstanceState)  
  28.     {  
  29.         super.onCreate(savedInstanceState);  
  30.   
  31.         /* Create a TextView and set its content. 
  32.          * the text is retrieved by calling a native 
  33.          * function. 
  34.          */  
  35.         TextView  tv = new TextView(this);  
  36.         tv.setText( "1111" );  
  37.         //System.out.println();  
  38.         setContentView(tv);  
  39.         tv.setText(String.valueOf(stringFromJNI()));  
  40.     }  
  41.   
  42.     /* A native method that is implemented by the 
  43.      * 'hello-jni' native library, which is packaged 
  44.      * with this application. 
  45.      */  
  46.     public native String  stringFromJNI();  
  47.   
  48.     /* This is another native method declaration that is *not* 
  49.      * implemented by 'hello-jni'. This is simply to show that 
  50.      * you can declare as many native methods in your Java code 
  51.      * as you want, their implementation is searched in the 
  52.      * currently loaded native libraries only the first time 
  53.      * you call them. 
  54.      * 
  55.      * Trying to call this function will result in a 
  56.      * java.lang.UnsatisfiedLinkError exception ! 
  57.      */  
  58.     public native String  unimplementedStringFromJNI();  
  59.   
  60.     /* this is used to load the 'hello-jni' library on application 
  61.      * startup. The library has already been unpacked into 
  62.      * /data/data/com.example.HelloJni/lib/libhello-jni.so at 
  63.      * installation time by the package manager. 
  64.      */  
  65.     static {  
  66.           System.loadLibrary("ffmpeg");  
  67.         System.loadLibrary("hello-jni");  
  68.     }  
  69. }  

   到此就完成了,將程序裝到手機可看到打印出“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

C代碼   收藏代碼
  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.   
  17. include $(CLEAR_VARS)  
  18. PATH_TO_FFMPEG_SOURCE:=$(LOCAL_PATH)/ffmpeg  
  19. LOCAL_C_INCLUDES += $(PATH_TO_FFMPEG_SOURCE)  
  20. LOCAL_LDLIBS := -lffmpeg  
  21. LOCAL_MODULE    := hello-jni  
  22. LOCAL_SRC_FILES := hello-jni.c  
  23.   
  24. 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

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.   
  36.   
  37.     return (*env)->NewStringUTF(env, str);  
  38. }  

 #include <ffmpeg/libavcodec/avcodec.h>
這行是因為下面要用到avcodec_version()這個函數。

 

    改完這兩個文件以后就可以編譯了~~用ndk-build命令編譯完后在工程的libs/armeabi目錄底下就會有一個libhello-jni.so文件了!(兩行眼淚啊~終於編譯成功了)

    編譯完成后就可以進行測試了,記得將libffmpeg.so也拷到armeabi目錄底下,並在java代碼中寫上

Java代碼   收藏代碼
  1. static {  
  2.        System.loadLibrary("ffmpeg");  
  3.         System.loadLibrary("hello-jni");  
  4.     }  

     HelloJni.java

 

 

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.   
  18. import android.app.Activity;  
  19. import android.widget.TextView;  
  20. import android.os.Bundle;  
  21.   
  22.   
  23. public class HelloJni extends Activity  
  24. {  
  25.     /** Called when the activity is first created. */  
  26.     @Override  
  27.     public void onCreate(Bundle savedInstanceState)  
  28.     {  
  29.         super.onCreate(savedInstanceState);  
  30.   
  31.         /* Create a TextView and set its content. 
  32.          * the text is retrieved by calling a native 
  33.          * function. 
  34.          */  
  35.         TextView  tv = new TextView(this);  
  36.         tv.setText( "1111" );  
  37.         //System.out.println();  
  38.         setContentView(tv);  
  39.         tv.setText(String.valueOf(stringFromJNI()));  
  40.     }  
  41.   
  42.     /* A native method that is implemented by the 
  43.      * 'hello-jni' native library, which is packaged 
  44.      * with this application. 
  45.      */  
  46.     public native String  stringFromJNI();  
  47.   
  48.     /* This is another native method declaration that is *not* 
  49.      * implemented by 'hello-jni'. This is simply to show that 
  50.      * you can declare as many native methods in your Java code 
  51.      * as you want, their implementation is searched in the 
  52.      * currently loaded native libraries only the first time 
  53.      * you call them. 
  54.      * 
  55.      * Trying to call this function will result in a 
  56.      * java.lang.UnsatisfiedLinkError exception ! 
  57.      */  
  58.     public native String  unimplementedStringFromJNI();  
  59.   
  60.     /* this is used to load the 'hello-jni' library on application 
  61.      * startup. The library has already been unpacked into 
  62.      * /data/data/com.example.HelloJni/lib/libhello-jni.so at 
  63.      * installation time by the package manager. 
  64.      */  
  65.     static {  
  66.           System.loadLibrary("ffmpeg");  
  67.         System.loadLibrary("hello-jni");  
  68.     }  
  69. }  

   到此就完成了,將程序裝到手機可看到打印出“3426306”,google搜索“ffmpeg 3426306”得知果然是ffmpeg的東西,證明成功的調用了libffmpeg.so庫里的方法了。欣慰啊~~

    接下來要做的就是學習ffmpeg庫里的各種函數的使用方法,以實現自己想要的功能了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM