项目的目录结构如图所示。Java源代码位于src目录,而C代码位于jni目录。
/** * 最简单的基于FFmpeg的转码器-安卓 * Simplest FFmpeg Android Transcoder * * 雷霄骅 Lei Xiaohua * leixiaohua1020@126.com * 马小雨 Ma Xiaoyu * maxiaoyucuc@163.com * 中国传媒大学/数字电视技术 * Communication University of China / Digital TV Technology * http://blog.csdn.net/leixiaohua1020 * * * 本程序是安卓平台下的转码器。它移植于ffmpeg.c命令行工具。 * * This software is a Transcoder in Android. * It is transplanted from ffmpeg.c command line tools. * */ package com.leixiaohua1020.sffmpegandroidtranscoder; import android.os.Bundle; import android.os.Environment; import android.app.Activity; import android.util.Log; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final EditText cmdEdittext= (EditText) this.findViewById(R.id.editText_cmd); Button startButton= (Button) this.findViewById(R.id.button_start); startButton.setOnClickListener(new OnClickListener() { public void onClick(View arg0){ String cmdline=cmdEdittext.getText().toString(); String[] argv=cmdline.split(" "); Integer argc=argv.length; ffmpegcore(argc,argv); } }); } public native int ffmpegcore(int argc,String[] argv); 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("sfftranscoder"); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
cmdutils.c
cmdutils.h
cmdutils_common_opts.h
config.h
ffmpeg.h
ffmpeg_filter.c
ffmpeg_opt.c
在编译ffmpeg.c的时候,除了需要FFmpeg SDK中的头文件之外,还需要从源代码中拷贝下面头文件。
libavformat/ffm.h
libavformat/network.h
libavformat/os_support.h
libavformat/url.h
libavutil/libm.h
ffmpeg_mod.c是修改后的ffmpeg.c文件。ffmpeg.c本身是用于命令行使用的程序,因此在改为类库调用的时候需要对源代码做一些修改。例如去掉一些exit(0),释放一些变量,main()函数改名为ffmpegmain()等。
ffmpeg_jni.c是ffmpeg.c和Android系统之间的接口。该部分的源代码如下所示。
/** * 最简单的基于FFmpeg的转码器-安卓 * Simplest FFmpeg Android Transcoder * * 雷霄骅 Lei Xiaohua * leixiaohua1020@126.com * 马小雨 Ma Xiaoyu * maxiaoyucuc@163.com * 中国传媒大学/数字电视技术 * Communication University of China / Digital TV Technology * http://blog.csdn.net/leixiaohua1020 * * * 本程序是安卓平台下的转码器。它移植于ffmpeg.c命令行工具。 * * This software is a Transcoder in Android. * It is transplanted from ffmpeg.c command line tools. * */ #include <string.h> #include <jni.h> #include <ffmpeg.h> #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 int ffmpegmain(int argc, char **argv); //Output FFmpeg‘s av_log() void custom_log(void *ptr, int level, const char* fmt, va_list vl){ //To TXT file FILE *fp=fopen("/storage/emulated/0/av_log.txt","a+"); if(fp){ vfprintf(fp,fmt,vl); fflush(fp); fclose(fp); } //To Logcat //LOGE(fmt, vl); } JNIEXPORT jint JNICALL Java_com_leixiaohua1020_sffmpegandroidtranscoder_MainActivity_ffmpegcore( JNIEnv * env, jobject thiz, jint cmdnum, jobjectArray cmdline) { //FFmpeg av_log() callback av_log_set_callback(custom_log); int argc=cmdnum; char** argv=(char**)malloc(sizeof(char*)*argc); int i=0; for(i=0;i<argc;i++) { jstring string=(*env)->GetObjectArrayElement(env,cmdline,i); const char* tmp=(*env)->GetStringUTFChars(env,string,0); argv[i]=(char*)malloc(sizeof(char)*1024); strcpy(argv[i],tmp); } ffmpegmain(argc,argv); for(i=0;i<argc;i++){ free(argv[i]); } free(argv); return 0; }
Android.mk文件位于jni/Android.mk,如下所示。
# Android.mk for FFmpeg # # Lei Xiaohua 雷霄骅 # leixiaohua1020@126.com # http://blog.csdn.net/leixiaohua1020 # LOCAL_PATH := $(call my-dir) # FFmpeg library include $(CLEAR_VARS) LOCAL_MODULE := avcodec LOCAL_SRC_FILES := libavcodec-56.so include $(PREBUILT_SHARED_LIBRARY) include $(CLEAR_VARS) LOCAL_MODULE := avdevice LOCAL_SRC_FILES := libavdevice-56.so include $(PREBUILT_SHARED_LIBRARY) include $(CLEAR_VARS) LOCAL_MODULE := avfilter LOCAL_SRC_FILES := libavfilter-5.so include $(PREBUILT_SHARED_LIBRARY) include $(CLEAR_VARS) LOCAL_MODULE := avformat LOCAL_SRC_FILES := libavformat-56.so include $(PREBUILT_SHARED_LIBRARY) include $(CLEAR_VARS) LOCAL_MODULE := avutil LOCAL_SRC_FILES := libavutil-54.so include $(PREBUILT_SHARED_LIBRARY) include $(CLEAR_VARS) LOCAL_MODULE := postproc LOCAL_SRC_FILES := libpostproc-53.so include $(PREBUILT_SHARED_LIBRARY) include $(CLEAR_VARS) LOCAL_MODULE := swresample LOCAL_SRC_FILES := libswresample-1.so include $(PREBUILT_SHARED_LIBRARY) include $(CLEAR_VARS) LOCAL_MODULE := swscale LOCAL_SRC_FILES := libswscale-3.so include $(PREBUILT_SHARED_LIBRARY) # Program include $(CLEAR_VARS) LOCAL_MODULE := sfftranscoder LOCAL_SRC_FILES :=ffmpeg_jni.c ffmpeg_mod.c ffmpeg_opt.c ffmpeg_filter.c cmdutils.c LOCAL_C_INCLUDES += $(LOCAL_PATH)/include LOCAL_LDLIBS := -llog -lz LOCAL_SHARED_LIBRARIES := avcodec avdevice avfilter avformat avutil postproc swresample swscale include $(BUILD_SHARED_LIBRARY)
App在手机上运行后的结果如下图所示。
单击“Start”按钮开始转码,将存储卡中的sintel.mp4转码为sintel.mkv。
Github:https://github.com/leixiaohua1020/simplest_ffmpeg_mobile
开源中国:https://git.oschina.net/leixiaohua1020/simplest_ffmpeg_mobile
[Android]
simplest_android_player: 基于安卓接口的视频播放器
simplest_ffmpeg_android_helloworld: 安卓平台下基于FFmpeg的HelloWorld程序
simplest_ffmpeg_android_decoder: 安卓平台下最简单的基于FFmpeg的视频解码器
simplest_ffmpeg_android_decoder_onelib: 安卓平台下最简单的基于FFmpeg的视频解码器-单库版
simplest_ffmpeg_android_streamer: 安卓平台下最简单的基于FFmpeg的推流器
simplest_ffmpeg_android_transcoder: 安卓平台下移植的FFmpeg命令行工具
simplest_sdl_android_helloworld: 移植SDL到安卓平台的最简单程序
[IOS]
simplest_ios_player: 基于IOS接口的视频播放器
simplest_ffmpeg_ios_helloworld: IOS平台下基于FFmpeg的HelloWorld程序
simplest_ffmpeg_ios_decoder: IOS平台下最简单的基于FFmpeg的视频解码器
simplest_ffmpeg_ios_streamer: IOS平台下最简单的基于FFmpeg的推流器
simplest_ffmpeg_ios_transcoder: IOS平台下移植的ffmpeg.c命令行工具
simplest_sdl_ios_helloworld: 移植SDL到IOS平台的最简单程序
版权声明:本文为博主原创文章,未经博主允许不得转载。
最简单的基于FFmpeg的移动端例子:Android 转码器
原文地址:http://blog.csdn.net/leixiaohua1020/article/details/47056365