How to Build Android Applications Based on FFmpeg by An Example
This is a follow up post of the previous blog How to Build FFmpeg for Android. You can read the previous tutorial first, or refer back to it when you feel necessary.
This blog covers how to build a simple Android app based on FFmpeg library. The app will detect the input video file’s resolution and codec information through interface provided by FFmpeg.
Blow is a screenshot of the app,
Figure 1. Screen shot of the sample android app based on FFmpeg
0. Create a new Android Project FFmpegTest.
When you’re asked to select targeted platform, select 2.2 as it’s the platform used in previous blog. But you’re free to change it.
Also create a folder named jni under the root directory “FFmpegTest” of this project.
1. Download the ffmpeg Source Code and Extract it to jni Folder
Follow the previous blog How to Build FFmpeg for Android to build the library.
2. Write the Native Code that use FFmpeg’s libavcodec library.
You can copy and paste the code below and save it as ffmpeg-test-jni.c under FFmpegTest/jni/ directory. Note that the code below is not completed, you can download the entire code at the end of the post.
/*for android logs*/
#define LOG_TAG "FFmpegTest"
#define LOG_LEVEL 10
#define LOGI(level, ...) if (level <= LOG_LEVEL) {__android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__);}
#define LOGE(level, ...) if (level <= LOG_LEVEL) {__android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__);}
char *gFileName; //the file name of the video
AVFormatContext *gFormatCtx;
int gVideoStreamIndex; //video stream index
AVCodecContext *gVideoCodecCtx;
static void get_video_info(char *prFilename);
static void get_video_info(char *prFilename) {
AVCodec *lVideoCodec;
int lError;
/*register the codec*/
extern AVCodec ff_h264_decoder;
avcodec_register(&ff_h264_decoder);
/*register demux*/
extern AVInputFormat ff_mov_demuxer;
av_register_input_format(&ff_mov_demuxer);
/*register the protocol*/
extern URLProtocol ff_file_protocol;
av_register_protocol2(&ff_file_protocol, sizeof(ff_file_protocol));
/*open the video file*/
if ((lError = av_open_input_file(&gFormatCtx, gFileName, NULL, 0, NULL)) !=0 ) {
LOGE(1, "Error open video file: %d", lError);
return; //open file failed
}
/*retrieve stream information*/
if ((lError = av_find_stream_info(gFormatCtx)) < 0) {
LOGE(1, "Error find stream information: %d", lError);
return;
}
/*find the video stream and its decoder*/
gVideoStreamIndex = av_find_best_stream(gFormatCtx, AVMEDIA_TYPE_VIDEO, -1, -1, &lVideoCodec, 0);
if (gVideoStreamIndex == AVERROR_STREAM_NOT_FOUND) {
LOGE(1, "Error: cannot find a video stream");
return;
} else {
LOGI(10, "video codec: %s", lVideoCodec->name);
}
if (gVideoStreamIndex == AVERROR_DECODER_NOT_FOUND) {
LOGE(1, "Error: video stream found, but no decoder is found!");
return;
}
/*open the codec*/
gVideoCodecCtx = gFormatCtx->streams[gVideoStreamIndex]->codec;
LOGI(10, "open codec: (%d, %d)", gVideoCodecCtx->height, gVideoCodecCtx->width);
if (avcodec_open(gVideoCodecCtx, lVideoCodec) < 0) {
LOGE(1, "Error: cannot open the video codec!");
return;
}
}
JNIEXPORT void JNICALL Java_roman10_ffmpegTest_VideoBrowser_naInit(JNIEnv *pEnv, jobject pObj, jstring pFileName) {
int l_mbH, l_mbW;
/*get the video file name*/
gFileName = (char *)(*pEnv)->GetStringUTFChars(pEnv, pFileName, NULL);
if (gFileName == NULL) {
LOGE(1, "Error: cannot get the video file name!");
return;
}
LOGI(10, "video file name is %s", gFileName);
get_video_info(gFileName);
}
JNIEXPORT jstring JNICALL Java_roman10_ffmpegTest_VideoBrowser_naGetVideoCodecName(JNIEnv *pEnv, jobject pObj) {
char* lCodecName = gVideoCodecCtx->codec->name;
return (*pEnv)->NewStringUTF(pEnv, lCodecName);
}
If you’re not familiar with Java JNI, you may need to read about JNI first in order to understand the code. But this is not the focus of this tutorial hence not covered.
3. Build the Native Code.
Create a file named Android.mk under jni directory and copy paste the content below,
LOCAL_PATH := $(call my-dir)
#declare the prebuilt library
include $(CLEAR_VARS)
LOCAL_MODULE := ffmpeg-prebuilt
LOCAL_SRC_FILES := ffmpeg-0.8/android/armv7-a/libffmpeg.so
LOCAL_EXPORT_C_INCLUDES := ffmpeg-0.8/android/armv7-a/include
LOCAL_EXPORT_LDLIBS := ffmpeg-0.8/android/armv7-a/libffmpeg.so
LOCAL_PRELINK_MODULE := true
include $(PREBUILT_SHARED_LIBRARY)
#the ffmpeg-test-jni library
include $(CLEAR_VARS)
LOCAL_ALLOW_UNDEFINED_SYMBOLS=false
LOCAL_MODULE := ffmpeg-test-jni
LOCAL_SRC_FILES := ffmpeg-test-jni.c
LOCAL_C_INCLUDES := $(LOCAL_PATH)/ffmpeg-0.8/android/armv7-a/include
LOCAL_SHARED_LIBRARY := ffmpeg-prebuilt
LOCAL_LDLIBS := -llog -ljnigraphics -lz -lm $(LOCAL_PATH)/ffmpeg-0.8/android/armv7-a/libffmpeg.so
include $(BUILD_SHARED_LIBRARY)
Create another file named Application.mk under jni directory and copy paste the content below,
# The ARMv7 is significanly faster due to the use of the hardware FPU
APP_ABI := armeabi-v7a
APP_PLATFORM := android-8
For more information about what Android.mk and Application.mk do, you can refer to the documentation comes with android NDK.
Your folder structure should be something like below after you finish all steps above,
Figure 2. Folder structure of project FFmpegTest and jni
4. Call the Native Code from FFmpegTest Java Code.
You’ll need to load the libraries and declare the jni functions. The code below is extracted from the app source code, which is provided for download at the end of this tutorial.
/*this part communicates with native code through jni (java native interface)*/
//load the native library
static {
System.loadLibrary("ffmpeg");
System.loadLibrary("ffmpeg-test-jni");
}
//declare the jni functions
private static native void naInit(String _videoFileName);
private static native int[] naGetVideoResolution();
private static native String naGetVideoCodecName();
private static native String naGetVideoFormatName();
private static native void naClose();
private void showVideoInfo(final File _file) {
String videoFilename = _file.getAbsolutePath();
naInit(videoFilename);
int[] prVideoRes = naGetVideoResolution();
String prVideoCodecName = naGetVideoCodecName();
String prVideoFormatName = naGetVideoFormatName();
naClose();
String displayText = "Video: " + videoFilename + "\n";
displayText += "Video Resolution: " + prVideoRes[0] + "x" + prVideoRes[1] + "\n";
displayText += "Video Codec: " + prVideoCodecName + "\n";
displayText += "Video Format: " + prVideoFormatName + "\n";
text_titlebar_text.setText(displayText);
}
5. You can Download the Entire Source Code from here.
Note: the code is tested on Ubuntu 10.04 and Android ndk-5b, but it should work on other platforms (except for Windows, which I’m not sure about.)
How to Build Android Applications Based on FFmpeg by An Example的更多相关文章
- cordova build android Command failed with exit code EACCES
问题: 执行cordova build android 出现输出如下,编译不成功. ANDROID_HOME=/Users/huangenai/Library/Android/sdkJAVA_HOME ...
- Android开发学习之路--Android Studio cmake编译ffmpeg
最新的android studio2.2引入了cmake可以很好地实现ndk的编写.这里使用最新的方式,对于以前的android下的ndk编译什么的可以参考之前的文章:Android开发学习之路– ...
- 为 Android 编译并集成 FFmpeg 的尝试与踩坑
前言与环境说明 随着 FFmpeg.NDK 与 Android Studio 的不断迭代,本文可能也会像我参考过的过期文章一样失效(很遗憾),但希望本文中提到的问题排查以及步骤说明能够帮到你,如果发现 ...
- ionic build Android错误记录未解决
1.try itcordova -v cordova create testing cd testing cordova plugin add cordova-plugin-sim cordova p ...
- ionic build android error when download gradle
这里我遇到一个问题,当用 ionic build android 的时候,无数次build,无数次失败的时候,我真想骂一句,NND的GNF,我又想起武大的臭鸡蛋,是的,该丢,发明这种东西的人,难道不 ...
- 原创: How to build a query based on Definition Updates installed
In SCCM 2012 R2, you can use following class. Use SMS_CombinedDeviceResources.EPAntivirusSignatureLa ...
- MAC 环境 ionic build android 命令在"Downloading http://services.gradle.org/distributions/gradle-2.13-all.zip"卡住问题
如题: 环境 node: 4.5.0,npm:2.15.9,cordova :6.3.1, ionic:2.1.0 在ionic build android 命令执行时,会去这个网址下载 gradel ...
- 5 Best Automation Tools for Testing Android Applications
Posted In | Automation Testing, Mobile Testing, Software Testing Tools Nowadays automated tests ar ...
- Android 环境下编译FFmpeg
Android 环境下编译FFmpeg 开发环境:Ubuntu 12.04.2 LTS , android-sdk-linux, android-ndk-r8e 一 .X264 编译 1. X2 ...
随机推荐
- 测试一个C段网络的联通性
#!/bin/bashPATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/binexport PATHnetwork ...
- Case When Exists SQL
The Case-When-Exists expression in Oracle is really handy. Here's an example of how to use it in a s ...
- LotusPhp中配置文件组件LtConfig详解
LotusPhp中配置文件组件LtConfig是约定的一个重要组成部分,适用于多个场景,多数的LotusPhp组件如数据库,缓存,RBAC,表单验证等都需要用到配置组件,LtConfig配置组件也是L ...
- PHP加密解密函数
<?php/***功能:对字符串进行加密处理*参数一:需要加密的内容*参数二:密钥*/function passport_encrypt($str,$key){ //加密函数 srand((do ...
- java8新特性笔记
1.forEach(),遍历数据结构中的元素,括号内可以带一个闭包的方法 2.双冒号用法:forEach(this::doSchedule),如果运行环境是闭包,java允许使用双冒号的写法来直接调用 ...
- php的swoole扩展中onclose和onconnect接口不被调用的问题
在用swoole扩展写在线聊天例子的时候遇到一个问题,查了不少资料,现在记录于此. 通过看swoole_server的接口文档,回调注册接口on中倒是有明确的注释: * swoole_server-& ...
- 开机一会,出现长时间闪屏,并且跳出SendRpt error
通过谷歌,发现任务管理器中的Report sending utility 是属于TortoiseSVN 的,所以卸载svn ,然后重启就ok了
- jquery mobile最棘手的一个问题
大多数jquery mobile开发的妹子们都碰到过这个问题: 如何调用loading效果 这里给出一段代码,赶紧练手吧. //显示loading function showLoading(){ ...
- Java jdk环境变量配置
首先安装jdk,现在已经是jdk 8了,也不知道能不能好好用了...
- C/C++ 对常见字符串库函数的实现
在c中的string.h头文件中存在很多对字符串进行操作的函数,利用这些函数可以方便的对字符串进行操作.下面将对常见的字符串函数进行解释和实现. strcpy 函数原型:char* _strcpy(c ...