This document records a video transcoding device based on FFmpeg on an Android platform. The transcoding is actually ported from the FFMPEG.C source code in the FFmpeg project. For FFMPEG.C source code can refer to the article "FFMPEG.C function structure simple analysis (Drawing)", here will not repeat the record.
Source
The directory structure of the project. The Java source code is in the SRC directory, and the C code is in the JNI directory.
The Java-side code for the Android program is located in Src\com\leixiaohua1020\sffmpegandroidtranscoder\mainactivity.java, as shown below.
/** * Simplest FFmpeg-based transcoding device-Android * simplest FFmpeg android transcoder * * Lei hua Lei Xiaohua * [email protected] * ma xiaoyu ma Xi Aoyu * [email protected] * Communication University/Digital TV Technology * Communication University of China/digital TV technology * Http://blog.cs dn.net/leixiaohua1020 * * * This program is a transcoding device under the Android platform. It is ported to the FFMPEG.C command-line tool. * * This software are 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 A Ction Bar if it is present. Getmenuinflater (). Inflate (R.menu.main, menu); return true; } }
The C-language side source code contains multiple files. The following files are copied from the FFmpeg source code:
CMDUTILS.C
Cmdutils.h
Cmdutils_common_opts.h
Config.h
Ffmpeg.h
Ffmpeg_filter.c
ffmpeg_opt.c
When compiling ffmpeg.c, you need to copy the following header files from the source code in addition to the header files in the FFmpeg SDK.
libavformat/ffm.h
Libavformat/network.h
Libavformat/os_support.h
Libavformat/url.h
Libavutil/libm.h
FFMPEG_MOD.C is the modified ffmpeg.c file. FFMPEG.C itself is a program used by the command line, so you need to make some changes to the source code when you change to a class library call. For example, remove some exit (0), release some variables, and the main () function is renamed Ffmpegmain ().
FFMPEG_JNI.C is the interface between the FFMPEG.C and Android systems. The source code for this section is shown below.
/** * Simplest FFmpeg-based transcoding-Android * simplest FFmpeg android transcoder * * Lei hua Lei Xiaohua * [email protected] * ma xiaoyu ma Xia Oyu * [email protected] * Communication University/Digital TV Technology * Communication University of China/digital TV technology * HTTP://BLOG.CSD n.net/leixiaohua1020 * * * This program is a transcoding device under the Android platform. It is ported to the FFMPEG.C command-line tool. * * This software are 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__) #endifint ffmpegmain (int argc, char **argv);//output FFmpeg ' s av_log () void cus Tom_log (void *ptr, int lEvel, const char* FMT, va_list VL) {//to TXT filefile *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;}
The Android.mk file is located in Jni/android.mk, as shown below.
# android.mk for ffmpeg## Lei Xiaohua lei hua # [email protected]# http://blog.csdn.net/leixiaohua1020# Local_path: = $ (c All My-dir) # FFmpeg Libraryinclude $ (clear_vars) Local_module: = avcodeclocal_src_files: = Libavcodec-56.soinclude $ ( prebuilt_shared_library) include $ (clear_vars) Local_module: = avdevicelocal_src_files: = Libavdevice-56.soinclude $ ( prebuilt_shared_library) include $ (clear_vars) Local_module: = avfilterlocal_src_files: = Libavfilter-5.soinclude $ ( prebuilt_shared_library) include $ (clear_vars) Local_module: = avformatlocal_src_files: = Libavformat-56.soinclude $ ( prebuilt_shared_library) include $ (clear_vars) Local_module: = avutillocal_src_files: = Libavutil-54.soinclude $ ( prebuilt_shared_library) include $ (clear_vars) Local_module: = postproclocal_src_files: = Libpostproc-53.soinclude $ ( prebuilt_shared_library) include $ (clear_vars) Local_module: = swresamplelocal_src_files: = Libswresample-1.soinclude $ (prebuilt_shared_library) include $ (clear_vars) Local_module: = swscalelocal_sRc_files: = Libswscale-3.soinclude $ (prebuilt_shared_library) # Programinclude $ (clear_vars) LOCAL_MODULE: = Sfftranscoderlocal_src_files: =ffmpeg_jni.c ffmpeg_mod.c ffmpeg_opt.c ffmpeg_filter.c Cmdutils.cLOCAL_C_INCLUDES + = $ (Local_path)/includelocal_ldlibs: =-llog-lzlocal_shared_libraries: = Avcodec avdevice avfilter avformat avutil Postproc swresample Swscaleinclude $ (build_shared_library)
Run results
The app runs on the phone as shown in the results.
Click the "Start" button to start transcoding and transcode the Sintel.mp4 in the memory card to sintel.mkv.
Download
simplest ffmpeg Mobile
Project Home
Github:https://github.com/leixiaohua1020/simplest_ffmpeg_mobile
Open source China: https://git.oschina.net/leixiaohua1020/simplest_ffmpeg_mobile
CSDN Project: http://download.csdn.net/detail/leixiaohua1020/8924391
This solution contains various examples of using FFMPEG to process multimedia on the mobile side:
[Android]
Simplest_android_player: Android interface-based video player
Simplest_ffmpeg_android_helloworld: FFmpeg-based HelloWorld program under Android platform
Simplest_ffmpeg_android_decoder: The simplest ffmpeg-based video decoder on the Android platform
Simplest_ffmpeg_android_decoder_onelib: The simplest ffmpeg-based video decoder on the Android Platform-Library edition
Simplest_ffmpeg_android_streamer: The simplest ffmpeg-based push-to-flow device under the Android platform
Simplest_ffmpeg_android_transcoder: FFmpeg command-line tool ported under Android platform
Simplest_sdl_android_helloworld: The simplest program for porting SDL to the Android platform
[IOS]
Simplest_ios_player: Video player based on iOS interface
HelloWorld program based on FFmpeg under the Simplest_ffmpeg_ios_helloworld:ios platform
The simplest ffmpeg-based video decoder under the Simplest_ffmpeg_ios_decoder:ios platform
The simplest ffmpeg based on the Simplest_ffmpeg_ios_streamer:ios platform
FFMPEG.C command-line tool ported under Simplest_ffmpeg_ios_transcoder:ios Platform
Simplest_sdl_ios_helloworld: The simplest program to migrate SDL to the iOS platform
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Simplest FFmpeg Mobile-based example: Android transcoding