`
tq09931
  • 浏览: 1513388 次
  • 性别: Icon_minigender_1
  • 来自: 天津
社区版块
存档分类
最新评论

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

# Copyright (C) 2009 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
PATH_TO_FFMPEG_SOURCE:=$(LOCAL_PATH)/ffmpeg
LOCAL_C_INCLUDES += $(PATH_TO_FFMPEG_SOURCE)
LOCAL_LDLIBS := -lffmpeg
LOCAL_MODULE    := hello-jni
LOCAL_SRC_FILES := hello-jni.c

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

/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
#include <string.h>
#include <stdio.h>
#include <android/log.h>
#include <stdlib.h> 
#include <jni.h>
#include <ffmpeg/libavcodec/avcodec.h>
/* This is a trivial JNI example where we use a native method
 * to return a new VM String. See the corresponding Java source
 * file located at:
 *
 *   apps/samples/hello-jni/project/src/com/example/HelloJni/HelloJni.java
 */
jstring
Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,
                                                  jobject thiz )
{
	char str[25];
	sprintf(str, "%d", avcodec_version()); 


    return (*env)->NewStringUTF(env, str);
}

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

 

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

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

static {
       System.loadLibrary("ffmpeg");
        System.loadLibrary("hello-jni");
    }

     HelloJni.java

 

 

/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.example.hellojni;

import android.app.Activity;
import android.widget.TextView;
import android.os.Bundle;


public class HelloJni extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        /* Create a TextView and set its content.
         * the text is retrieved by calling a native
         * function.
         */
        TextView  tv = new TextView(this);
        tv.setText( "1111" );
        //System.out.println();
        setContentView(tv);
        tv.setText(String.valueOf(stringFromJNI()));
    }

    /* A native method that is implemented by the
     * 'hello-jni' native library, which is packaged
     * with this application.
     */
    public native String  stringFromJNI();

    /* This is another native method declaration that is *not*
     * implemented by 'hello-jni'. This is simply to show that
     * you can declare as many native methods in your Java code
     * as you want, their implementation is searched in the
     * currently loaded native libraries only the first time
     * you call them.
     *
     * Trying to call this function will result in a
     * java.lang.UnsatisfiedLinkError exception !
     */
    public native String  unimplementedStringFromJNI();

    /* this is used to load the 'hello-jni' library on application
     * startup. The library has already been unpacked into
     * /data/data/com.example.HelloJni/lib/libhello-jni.so at
     * installation time by the package manager.
     */
    static {
    	  System.loadLibrary("ffmpeg");
        System.loadLibrary("hello-jni");
    }
}

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

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

分享到:
评论
6 楼 x22819 2014-03-26  
你好,我在Windows 7用android-ndk-r8b编译ffmpeg-1.2.6

编译成功后
将Source code放到Eclipse Project中的JNI目录
将libs放到Eclipse Project中的libs目录
将obj放到Eclipse Project中的obj目录

参考你这边设定

在JNI目录下创建SoftAp.C (SoftAp为Project名称)
jstring
Java_com_softap_SoftAP_stringFromJNI( JNIEnv* env,jobject thiz )
{
    char str[25];
    sprintf(str, "%d", avcodec_version());

    return (*env)->NewStringUTF(env, str);
}

Android.mk档案中只修改
LOCAL_MODULE    := SoftAP 
LOCAL_SRC_FILES := SoftAP.c 

但是下ndk-build指令出现此讯息
C:\Users\Wun\workspace\SoftAP>ndk-build
make: *** No rule to make target `/SoftAP.c', needed by `obj/local/armeabi/objs/

该如何解决呢?
SoftAP/SoftAP.o'.  Stop.
5 楼 dengzhangtao 2013-12-06  
我说你说了这么多,就不能把so库放上来么
4 楼 laozhangxiaoY 2013-06-18  
成功了,非常感谢!!
3 楼 yuyingsuifeng 2012-04-12  
我的按照你的步骤做,在运行的时候提示下面的错误,不知楼主碰过这种情况没?
04-12 16:07:12.009: D/dalvikvm(1673): Trying to load lib /data/data/sdi.ffmpeg.tset/lib/libffmpeg.so 0x40514468
04-12 16:07:12.009: D/dalvikvm(1673): Added shared lib /data/data/sdi.ffmpeg.tset/lib/libffmpeg.so 0x40514468
04-12 16:07:12.009: D/dalvikvm(1673): No JNI_OnLoad found in /data/data/sdi.ffmpeg.tset/lib/libffmpeg.so 0x40514468, skipping init
04-12 16:07:12.138: W/dalvikvm(1673): Exception Ljava/lang/UnsatisfiedLinkError; thrown while initializing Lsdi/ffmpeg/tset/Main;
04-12 16:07:12.138: W/dalvikvm(1673): Class init failed in newInstance call (Lsdi/ffmpeg/tset/Main;)
04-12 16:07:12.138: D/AndroidRuntime(1673): Shutting down VM
04-12 16:07:12.138: W/dalvikvm(1673): threadid=1: thread exiting with uncaught exception (group=0x40015560)
04-12 16:07:12.138: E/AndroidRuntime(1673): FATAL EXCEPTION: main
04-12 16:07:12.138: E/AndroidRuntime(1673): java.lang.ExceptionInInitializerError
04-12 16:07:12.138: E/AndroidRuntime(1673): at java.lang.Class.newInstanceImpl(Native Method)
04-12 16:07:12.138: E/AndroidRuntime(1673): at java.lang.Class.newInstance(Class.java:1409)
04-12 16:07:12.138: E/AndroidRuntime(1673): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
04-12 16:07:12.138: E/AndroidRuntime(1673): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
04-12 16:07:12.138: E/AndroidRuntime(1673): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
04-12 16:07:12.138: E/AndroidRuntime(1673): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-12 16:07:12.138: E/AndroidRuntime(1673): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
04-12 16:07:12.138: E/AndroidRuntime(1673): at android.os.Handler.dispatchMessage(Handler.java:99)
04-12 16:07:12.138: E/AndroidRuntime(1673): at android.os.Looper.loop(Looper.java:123)
04-12 16:07:12.138: E/AndroidRuntime(1673): at android.app.ActivityThread.main(ActivityThread.java:3683)
04-12 16:07:12.138: E/AndroidRuntime(1673): at java.lang.reflect.Method.invokeNative(Native Method)
04-12 16:07:12.138: E/AndroidRuntime(1673): at java.lang.reflect.Method.invoke(Method.java:507)
04-12 16:07:12.138: E/AndroidRuntime(1673): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-12 16:07:12.138: E/AndroidRuntime(1673): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-12 16:07:12.138: E/AndroidRuntime(1673): at dalvik.system.NativeStart.main(Native Method)
04-12 16:07:12.138: E/AndroidRuntime(1673): Caused by: java.lang.UnsatisfiedLinkError: Couldn't load hello-jni: findLibrary returned null
04-12 16:07:12.138: E/AndroidRuntime(1673): at java.lang.Runtime.loadLibrary(Runtime.java:429)
04-12 16:07:12.138: E/AndroidRuntime(1673): at java.lang.System.loadLibrary(System.java:554)
04-12 16:07:12.138: E/AndroidRuntime(1673): at sdi.ffmpeg.tset.Main.<clinit>(Main.java:21)
04-12 16:07:12.138: E/AndroidRuntime(1673): ... 15 more
2 楼 mzloon 2011-12-30  
http://www.iteye.chttp://www.iteye.com/images/smiles/icon_sad.gif       om/images/smiles/icon_surprised.gif
1 楼 kkandkkg 2011-06-05  
请问楼主如何使用在Android中使用FFMPEG命令行呢?
例如:ffmpeg -i video_origine.avi -acodec libmp3lame -ab 56K -ar 44100 -b 200K -r 15 -s 320x240 -f flv video_finale.flv这一类的命令行。
如果在jni中使用的话,如何调用呢

相关推荐

Global site tag (gtag.js) - Google Analytics