一、推荐开源项目
最近需要在 Android 中进行音视频数据转码 , 音频混音 , 音频编辑边裁 等操作 , 如果能在 Android 系统中执行 FFMPEG 指令 , 基本就可以晚上需求 ;
推荐一个 GitHub 上的项目 : https://github.com/WritingMinds/ffmpeg-android-java
该项目中 FFmpegAndroid 是 Android Library 核心依赖库 , 在自己的项目中 , 引入该依赖库即可进行 FFMPEG 命令执行 ;
app Module 仅仅是一个示例项目 , 展示 FFmpegAndroid 依赖库如何使用 ;
在 FFmpegAndroid 项目中的 ffmpeg-android-java-0.3.2\FFmpegAndroid\assets\armeabi-v7a\ffmpeg 是 FFMPEG 可执行文件 , 可以在 ARM 架构的 Android 系统中执行 ;
在 ffmpeg-android-java-0.3.2\FFmpegAndroid\assets\x86\ffmpeg 是可以在 x86 架构的 Android 系统中可执行的文件 ;
这个 ffmpeg 可执行文件是该应用的核心 ;
基于最后一个可运行版本进行调试 ,
这个项目在 2016 20162016 年停止维护了 , 运行后一堆报错 , 引用了远古版本的 ButterKnife 和 Dagger 依赖库 , 更新了最新的 com.github.dcendents:android-maven-gradle-plugin 插件 , 然后添加了 google() 库支持 , 项目运行起来了 ;
参考 :
【错误记录】编译安卓项目报错 ( AndroidMavenPlugin 错误 )
【错误记录】安卓编译错误 ( Could not find xxx.tools.build:aapt2 )
运行该项目 , 执行
-version
1
命令 , 打印出该 FFMPEG 的版本 , 3.0.1 的版本 , 有点老 ;
二、Android 中执行 FFMPEG 指令
参考 http://writingminds.github.io/ffmpeg-android-java/ 博客中的使用介绍 ;
1、导入依赖
直接引用项目 :
repositories { flatDir { dirs 'libs' } } dependencies { compile(name:'FFmpegAndroid', ext:'aar') }
添加 Gradle 依赖库 :
compile 'com.writingminds:FFmpegAndroid:0.3.2'
Maven 依赖库 :
<dependency> <groupId>com.writingminds</groupId> <artifactId>FFmpegAndroid</artifactId> <version>0.3.2</version> </dependency>
2、Java 代码编写
首先 , 初始化 FFMPEG 实例 ;
FFmpeg ffmpeg = FFmpeg.getInstance(context);
1
然后 , 加载 ffmpeg 可执行文件 , 该操作是将可执行文件从 assets 目录中拷贝到 Android 应用的内置存储空间 ;
try { ffmpeg.loadBinary(new LoadBinaryResponseHandler() { @Override public void onStart() {} @Override public void onFailure() {} @Override public void onSuccess() {} @Override public void onFinish() {} }); } catch (FFmpegNotSupportedException e) { // Handle if FFmpeg is not supported by device }
最后 , 执行 FFMPEG 命令 ;
try { // to execute "ffmpeg -version" command you just need to pass "-version" ffmpeg.execute(cmd, new ExecuteBinaryResponseHandler() { @Override public void onStart() {} @Override public void onProgress(String message) {} @Override public void onFailure(String message) {} @Override public void onSuccess(String message) {} @Override public void onFinish() {} }); } catch (FFmpegCommandAlreadyRunningException e) { // Handle if FFmpeg is already running }
3、使用时的代码示例
ffmpeg-android-java 项目中 app 的主界面代码 , 有上述 3 33 个完整的使用步骤 ; package com.github.hiteshsondhi88.sampleffmpeg; import android.app.Activity; import android.app.AlertDialog; import android.app.ProgressDialog; import android.content.DialogInterface; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; import com.github.hiteshsondhi88.libffmpeg.ExecuteBinaryResponseHandler; import com.github.hiteshsondhi88.libffmpeg.FFmpeg; import com.github.hiteshsondhi88.libffmpeg.LoadBinaryResponseHandler; import com.github.hiteshsondhi88.libffmpeg.exceptions.FFmpegCommandAlreadyRunningException; import com.github.hiteshsondhi88.libffmpeg.exceptions.FFmpegNotSupportedException; public class Home extends Activity implements View.OnClickListener { private static final String TAG = Home.class.getSimpleName(); FFmpeg ffmpeg; EditText commandEditText; LinearLayout outputLayout; Button runButton; private ProgressDialog progressDialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_home); // 1. 获取 FFMPEG 实例 ffmpeg = FFmpeg.getInstance(this); commandEditText = (EditText) findViewById(R.id.command); outputLayout = (LinearLayout) findViewById(R.id.command_output); runButton = (Button) findViewById(R.id.run_command); loadFFMpegBinary(); initUI(); } private void initUI() { runButton.setOnClickListener(this); progressDialog = new ProgressDialog(this); progressDialog.setTitle(null); } // 2. 加载 ffmpeg 可执行文件 private void loadFFMpegBinary() { try { ffmpeg.loadBinary(new LoadBinaryResponseHandler() { @Override public void onFailure() { showUnsupportedExceptionDialog(); } }); } catch (FFmpegNotSupportedException e) { showUnsupportedExceptionDialog(); } } // 3. 执行命令 private void execFFmpegBinary(final String[] command) { try { ffmpeg.execute(command, new ExecuteBinaryResponseHandler() { @Override public void onFailure(String s) { addTextViewToLayout("FAILED with output : "+s); } @Override public void onSuccess(String s) { addTextViewToLayout("SUCCESS with output : "+s); } @Override public void onProgress(String s) { Log.d(TAG, "Started command : ffmpeg "+command); addTextViewToLayout("progress : "+s); progressDialog.setMessage("Processing\n"+s); } @Override public void onStart() { outputLayout.removeAllViews(); Log.d(TAG, "Started command : ffmpeg " + command); progressDialog.setMessage("Processing..."); progressDialog.show(); } @Override public void onFinish() { Log.d(TAG, "Finished command : ffmpeg "+command); progressDialog.dismiss(); } }); } catch (FFmpegCommandAlreadyRunningException e) { // do nothing for now } } private void addTextViewToLayout(String text) { TextView textView = new TextView(Home.this); textView.setText(text); outputLayout.addView(textView); } private void showUnsupportedExceptionDialog() { new AlertDialog.Builder(Home.this) .setIcon(android.R.drawable.ic_dialog_alert) .setTitle(getString(R.string.device_not_supported)) .setMessage(getString(R.string.device_not_supported_message)) .setCancelable(false) .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Home.this.finish(); } }) .create() .show(); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.run_command: String cmd = commandEditText.getText().toString(); String[] command = cmd.split(" "); if (command.length != 0) { execFFmpegBinary(command); } else { Toast.makeText(Home.this, getString(R.string.empty_command_toast), Toast.LENGTH_LONG).show(); } break; } } }


