Android Studio FFmpeg simple to use (command line)

Source: Internet
Author: User

Compiling ffmpeg

Android Studio New project, Tick on

Throw the compiled libffmpeg.so library under the Src/main/jnilibs/armeabi (mostly I only compiled arm's ffmpeg library)

New File Com.jni.FFmpegCmd

 Package Com.jni;  Public class ffmpegcmd {    static  {        system.loadlibrary ("FFmpeg");        System.loadlibrary ("Ffmpeg-cmd");    }      Public Static native int run (string[] commands);}

Create a Ffmpeg-cmd.cpp file under CPP (that is, a directory with Native-lib.cpp)

#include <jni.h>#include"ffmpeg.h"#include"android_log.h"extern "C"jniexport jint jnicalljava_com_jni_ffmpegcmd_run (jnienv*env, Jobject, jobjectarray commands) {    intARGC = env->getarraylength (commands); Char*ARGV[ARGC];  for(inti =0; i < argc; i++) {jstring js= (jstring) env->getobjectarrayelement (commands, i); Argv[i]= (Char*) Env->getstringutfchars (JS,0); } logd ("============ start execution of command line ==========="); returnMain (argc, argv);}

Android_log.h

#ifdef android#include<android/log.h>#ifndef Log_tag#defineMy_tag "MyTag"#defineAv_tag "Avlog"#endif#defineLOGE (format, ...) __android_log_print (Android_log_error, My_tag, format, # #__VA_ARGS__)#defineLOGD (format, ...) __android_log_print (Android_log_debug, My_tag, format, # #__VA_ARGS__)#defineXLOGD (...) __android_log_print (android_log_info,av_tag,__va_args__)#defineXloge (...) __android_log_print (android_log_error,av_tag,__va_args__)#else#defineLOGE (format, ...) printf (my_tag format "\ n", # #__VA_ARGS__)#defineLOGD (format, ...) printf (my_tag format "\ n", # #__VA_ARGS__)#defineXloge (format, ...) fprintf (stdout, Av_tag ":" format "\ n", # #__VA_ARGS__)#defineXlogi (format, ...) fprintf (stderr, Av_tag ":" format "\ n", # #__VA_ARGS__)#endif

Copy ffmpeg source file cmdutils.c cmdutils.h ffmpeg.cffmpeg_filter.cffmpeg_opt.c ffmpeg_hw.c to the CPP directory

(ffmpeg_hw.c files are not required for versions prior to ffmpeg-3.4, but need to cmdutils_common_opts.h files )

FFmpeg compiled folder include Lib is also copied to this directory

Modify Ffmpeg.h at the end of the add int main (int argc, char **argv);

Modify FFMPEG.C

Added at the end of the main method (heard to prevent the FFmpeg command flashback in order to repeatedly execute)

0==0 = 0=0  =0;
Modify Cmdutils.h
Modified the former  void exit_program (int  ret  ) Av_noreturn; modified int Exit_program (  int  ret); modified former   void show_help_children (const Avclass *classint flags); // do not modify the words compile always error unclear what causes modified  void show_help_children (constint flags);

Modify CMDUTILS.C

before modification void exit_program (int  ret) {    if  (program_exit)        program_exit (ret);    Exit (ret);} Modified int exit_program (int  ret) {    if  (program_exit)        Program_exit (ret);     return ret;}

CMakeLists.txt

Cmake_minimum_required (VERSION3.4.1) add_library (ffmpeg-cmd SHARED src/main/cpp/cmdutils.c src/main/cpp/ffmpeg.c src/main/cpp/ffmpeg_filter.c src/main/cpp/ffmpeg_opt.c src/main/cpp/ffmpeg_hw.c src/main/cpp/ffmpeg-cmd.cpp) find_library (log-Lib log android) include_directories (src/main/cpp/include SRC/main/cpp/Lib E:/ffmpeg/4.0/build/ffmpeg-4.0) add_library (ffmpeg SHARED imported) set_target_properties (FFmpeg properties imported_location E:/ffmpeg/4.0/build/android/arm/libffmpeg.so) target_link_libraries (ffmpeg-cmd ffmpeg android ${log-lib})

Build.gradle (Module:app)

The main need to deal with the red part, the other parts are automatically generated

Finally, test it in Mainactivity.java.

 Public classMainactivityextendsappcompatactivity {PrivateExecutorservice es =Executors.newsinglethreadexecutor (); String Path=environment.getexternalstoragedirectory (). GetPath (); PrivateProgressDialog ProgressDialog; PrivateTextView TV; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); TV=Findviewbyid (R.id.sample_text); Tv.settext (Test); Tv.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {ProgressDialog= Progressdialog.show (mainactivity. This, "", "in Process"); NewThread (NewRunnable () {@Override Public voidrun () {String str= "Ffmpeg-i%s-vcodec copy-f mpegts-y%s"//mp4 ts-y Overwrite the file if it has the same name. Do not add-y will indicate whether a file with the same name is overwritten, and if executed again, it will flash back String commandstr= String.Format (str,path+ "/data/qq.mp4", path+ "/data/qq.ts"); string[] Commands= Commandstr.split ("\\s+");  for(String command:commands) {log.d (mainactivity.class. GetName (), "command=" +command); }                        Final intsum =Ffmpegcmd.run (commands); NewHandler (Looper.getmainlooper ()). Post (NewRunnable () {@Override Public voidrun () {if(sum = = 0){                                    if(ProgressDialog! =NULL) {Progressdialog.dismiss (); } tv.settext ("Processing Success"); }Else{Tv.settext ("Processing Failed");                    }                            }                        });            }}). Start ();    }        }); }}

Android Studio FFmpeg simple to use (command line)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.