抽取FFmpeg执行流程回调

抽取FFmpeg执行流程回调
pull/107/head
xufulong 6 years ago
parent eaf1f3778a
commit 0b4a19b2d0
  1. 7
      app/src/main/java/com/frank/ffmpeg/FFmpegCmd.java
  2. 41
      app/src/main/java/com/frank/ffmpeg/activity/AudioHandleActivity.java
  3. 59
      app/src/main/java/com/frank/ffmpeg/activity/MediaHandleActivity.java
  4. 42
      app/src/main/java/com/frank/ffmpeg/activity/VideoHandleActivity.java
  5. 47
      app/src/main/java/com/frank/ffmpeg/activity/VideoReverseActivity.java
  6. 64
      app/src/main/java/com/frank/ffmpeg/handler/FFmpegHandler.java
  7. 10
      app/src/main/java/com/frank/ffmpeg/listener/OnHandleListener.java

@ -1,11 +1,8 @@
package com.frank.ffmpeg;
public class FFmpegCmd {
import com.frank.ffmpeg.listener.OnHandleListener;
public interface OnHandleListener{
void onBegin();
void onEnd(int result);
}
public class FFmpegCmd {
static{
System.loadLibrary("media-handle");

@ -5,19 +5,21 @@ import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import java.io.File;
import com.frank.ffmpeg.AudioPlayer;
import com.frank.ffmpeg.FFmpegCmd;
import com.frank.ffmpeg.R;
import com.frank.ffmpeg.handler.FFmpegHandler;
import com.frank.ffmpeg.mp3.Mp3Converter;
import com.frank.ffmpeg.util.FFmpegUtil;
import com.frank.ffmpeg.util.FileUtil;
import static com.frank.ffmpeg.handler.FFmpegHandler.MSG_BEGIN;
import static com.frank.ffmpeg.handler.FFmpegHandler.MSG_FINISH;
/**
* 使用ffmpeg处理音频
* Created by frank on 2018/1/23.
@ -25,14 +27,13 @@ import com.frank.ffmpeg.util.FileUtil;
public class AudioHandleActivity extends BaseActivity {
private final static String TAG = AudioHandleActivity.class.getSimpleName();
private final static String PATH = Environment.getExternalStorageDirectory().getPath();
private String appendFile = PATH + File.separator + "test.mp3";
private final static int MSG_BEGIN = 311;
private final static int MSG_FINISH = 312;
private ProgressBar progressAudio;
private LinearLayout layoutAudioHandle;
private int viewId;
private FFmpegHandler ffmpegHandler;
@SuppressLint("HandlerLeak")
private Handler mHandler = new Handler(){
@ -65,6 +66,7 @@ public class AudioHandleActivity extends BaseActivity {
hideActionBar();
initView();
ffmpegHandler = new FFmpegHandler(mHandler);
}
private void initView() {
@ -175,30 +177,17 @@ public class AudioHandleActivity extends BaseActivity {
default:
break;
}
executeFFmpegCmd(commandLine);
if (ffmpegHandler != null) {
ffmpegHandler.executeFFmpegCmd(commandLine);
}
}
/**
* 执行ffmpeg命令行
* @param commandLine commandLine
*/
private void executeFFmpegCmd(final String[] commandLine) {
if(commandLine == null) {
return;
@Override
protected void onDestroy() {
super.onDestroy();
if (mHandler != null) {
mHandler.removeCallbacksAndMessages(null);
}
FFmpegCmd.execute(commandLine, new FFmpegCmd.OnHandleListener() {
@Override
public void onBegin() {
Log.i(TAG, "handle audio onBegin...");
mHandler.obtainMessage(MSG_BEGIN).sendToTarget();
}
@Override
public void onEnd(int result) {
Log.i(TAG, "handle audio onEnd...");
mHandler.obtainMessage(MSG_FINISH).sendToTarget();
}
});
}
}

@ -11,13 +11,17 @@ import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import com.frank.ffmpeg.FFmpegCmd;
import com.frank.ffmpeg.R;
import com.frank.ffmpeg.handler.FFmpegHandler;
import com.frank.ffmpeg.util.FFmpegUtil;
import com.frank.ffmpeg.util.FileUtil;
import java.io.File;
import static com.frank.ffmpeg.handler.FFmpegHandler.MSG_BEGIN;
import static com.frank.ffmpeg.handler.FFmpegHandler.MSG_CONTINUE;
import static com.frank.ffmpeg.handler.FFmpegHandler.MSG_FINISH;
/**
* 使用ffmpeg进行音视频合成与分离
* Created by frank on 2018/1/23.
@ -28,13 +32,11 @@ public class MediaHandleActivity extends BaseActivity {
private static final String PATH = Environment.getExternalStorageDirectory().getPath();
private String videoFile;
private String temp = PATH + File.separator + "temp.mp4";
private boolean isMux;
private final static int MSG_MUX = 100;
private final static int MSG_BEGIN = 101;
private final static int MSG_FINISH = 102;
private ProgressBar progressMedia;
private int viewId;
private LinearLayout layoutMediaHandle;
private FFmpegHandler ffmpegHandler;
@SuppressLint("HandlerLeak")
private Handler mHandler = new Handler(){
@ -42,7 +44,7 @@ public class MediaHandleActivity extends BaseActivity {
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
case MSG_MUX:
case MSG_CONTINUE:
String audioFile = PATH + File.separator + "tiger.mp3";//tiger.mp3
String muxFile = PATH + File.separator + "media-mux.mp4";
@ -67,8 +69,10 @@ public class MediaHandleActivity extends BaseActivity {
int mDuration = Math.min(audioDuration, videoDuration);
//使用纯视频与音频进行合成
String[] commandLine = FFmpegUtil.mediaMux(temp, audioFile, mDuration, muxFile);
executeFFmpegCmd(commandLine);
isMux = false;
if (ffmpegHandler != null) {
ffmpegHandler.isContinue(false);
ffmpegHandler.executeFFmpegCmd(commandLine);
}
} catch (Exception e) {
e.printStackTrace();
}
@ -98,6 +102,7 @@ public class MediaHandleActivity extends BaseActivity {
hideActionBar();
initView();
ffmpegHandler = new FFmpegHandler(mHandler);
}
private void initView() {
@ -141,7 +146,9 @@ public class MediaHandleActivity extends BaseActivity {
//视频文件有音频,先把纯视频文件抽取出来
videoFile = srcFile;
commandLine = FFmpegUtil.extractVideo(srcFile, temp);
isMux = true;
if (ffmpegHandler != null) {
ffmpegHandler.isContinue(true);
}
} catch (Exception e) {
e.printStackTrace();
}
@ -157,34 +164,16 @@ public class MediaHandleActivity extends BaseActivity {
default:
break;
}
executeFFmpegCmd(commandLine);
if (ffmpegHandler != null) {
ffmpegHandler.executeFFmpegCmd(commandLine);
}
}
/**
* 执行ffmpeg命令行
* @param commandLine commandLine
*/
private void executeFFmpegCmd(final String[] commandLine) {
if(commandLine == null){
return;
@Override
protected void onDestroy() {
super.onDestroy();
if (mHandler != null) {
mHandler.removeCallbacksAndMessages(null);
}
FFmpegCmd.execute(commandLine, new FFmpegCmd.OnHandleListener() {
@Override
public void onBegin() {
Log.i(TAG, "handle media onBegin...");
mHandler.obtainMessage(MSG_BEGIN).sendToTarget();
}
@Override
public void onEnd(int result) {
Log.i(TAG, "handle media onEnd...");
if(isMux){
mHandler.obtainMessage(MSG_MUX).sendToTarget();
}else {
mHandler.obtainMessage(MSG_FINISH).sendToTarget();
}
}
});
}
}

@ -6,28 +6,27 @@ import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import com.frank.ffmpeg.FFmpegCmd;
import com.frank.ffmpeg.R;
import com.frank.ffmpeg.format.VideoLayout;
import com.frank.ffmpeg.handler.FFmpegHandler;
import com.frank.ffmpeg.util.FFmpegUtil;
import com.frank.ffmpeg.util.FileUtil;
import java.io.File;
public class VideoHandleActivity extends BaseActivity {
import static com.frank.ffmpeg.handler.FFmpegHandler.MSG_BEGIN;
import static com.frank.ffmpeg.handler.FFmpegHandler.MSG_FINISH;
private static final String TAG = VideoHandleActivity.class.getSimpleName();
private static final int MSG_BEGIN = 101;
private static final int MSG_FINISH = 102;
public class VideoHandleActivity extends BaseActivity {
private static final String PATH = Environment.getExternalStorageDirectory().getPath();
private ProgressBar progressVideo;
private LinearLayout layoutVideoHandle;
private int viewId;
private FFmpegHandler ffmpegHandler;
@SuppressLint("HandlerLeak")
private Handler mHandler = new Handler(){
@ -60,6 +59,7 @@ public class VideoHandleActivity extends BaseActivity {
hideActionBar();
intView();
ffmpegHandler = new FFmpegHandler(mHandler);
}
private void intView() {
@ -227,30 +227,16 @@ public class VideoHandleActivity extends BaseActivity {
default:
break;
}
executeFFmpegCmd(commandLine);
if (ffmpegHandler != null) {
ffmpegHandler.executeFFmpegCmd(commandLine);
}
}
/**
* 执行ffmpeg命令行
* @param commandLine commandLine
*/
private void executeFFmpegCmd(final String[] commandLine) {
if(commandLine == null) {
return;
@Override
protected void onDestroy() {
super.onDestroy();
if (mHandler != null) {
mHandler.removeCallbacksAndMessages(null);
}
FFmpegCmd.execute(commandLine, new FFmpegCmd.OnHandleListener() {
@Override
public void onBegin() {
Log.i(TAG, "handle video onBegin...");
mHandler.obtainMessage(MSG_BEGIN).sendToTarget();
}
@Override
public void onEnd(int result) {
Log.i(TAG, "handle video onEnd...");
mHandler.obtainMessage(MSG_FINISH).sendToTarget();
}
});
}
}

@ -5,16 +5,18 @@ import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.VideoView;
import com.frank.ffmpeg.FFmpegCmd;
import com.frank.ffmpeg.R;
import com.frank.ffmpeg.handler.FFmpegHandler;
import com.frank.ffmpeg.util.FFmpegUtil;
import com.frank.ffmpeg.util.FileUtil;
import java.io.File;
import static com.frank.ffmpeg.handler.FFmpegHandler.MSG_FINISH;
import static com.frank.ffmpeg.handler.FFmpegHandler.MSG_TOAST;
/**
* 先处理视频反序再视频倒播
* Created by frank on 2018/9/12.
@ -22,7 +24,6 @@ import java.io.File;
public class VideoReverseActivity extends BaseActivity {
private final static String TAG = VideoReverseActivity.class.getSimpleName();
private final static String ROOT_PATH = Environment.getExternalStorageDirectory().getPath();
private String VIDEO_NORMAL_PATH = "";
private final static String VIDEO_REVERSE_PATH = ROOT_PATH + File.separator + "reverse.mp4";
@ -30,15 +31,14 @@ public class VideoReverseActivity extends BaseActivity {
private LinearLayout loading;
private VideoView videoNormal;
private VideoView videoReverse;
private FFmpegHandler ffmpegHandler;
private final static int MSG_PLAY = 7777;
private final static int MSG_TOAST = 8888;
@SuppressLint("HandlerLeak")
private Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == MSG_PLAY){
if (msg.what == MSG_FINISH){
changeVisibility();
startPlay();
}else if (msg.what == MSG_TOAST) {
@ -59,6 +59,7 @@ public class VideoReverseActivity extends BaseActivity {
initView();
initPlayer();
mHandler.sendEmptyMessageDelayed(MSG_TOAST, 1000);
ffmpegHandler = new FFmpegHandler(mHandler);
}
private void initView() {
@ -87,28 +88,6 @@ public class VideoReverseActivity extends BaseActivity {
videoReverse.start();
}
/**
* 执行ffmpeg命令行
* @param commandLine commandLine
*/
private void executeFFmpegCmd(final String[] commandLine){
if(commandLine == null){
return;
}
FFmpegCmd.execute(commandLine, new FFmpegCmd.OnHandleListener() {
@Override
public void onBegin() {
Log.i(TAG, "handle video onBegin...");
}
@Override
public void onEnd(int result) {
Log.i(TAG, "handle video onEnd...");
mHandler.sendEmptyMessage(MSG_PLAY);
}
});
}
/**
* 视频反序处理
*/
@ -117,7 +96,9 @@ public class VideoReverseActivity extends BaseActivity {
return;
}
String[] commandLine = FFmpegUtil.reverseVideo(VIDEO_NORMAL_PATH, VIDEO_REVERSE_PATH);
executeFFmpegCmd(commandLine);
if (ffmpegHandler != null) {
ffmpegHandler.executeFFmpegCmd(commandLine);
}
}
@Override
@ -131,4 +112,12 @@ public class VideoReverseActivity extends BaseActivity {
loading.setVisibility(View.VISIBLE);
videoReverse();
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mHandler != null) {
mHandler.removeCallbacksAndMessages(null);
}
}
}

@ -0,0 +1,64 @@
package com.frank.ffmpeg.handler;
import android.os.Handler;
import android.util.Log;
import com.frank.ffmpeg.FFmpegCmd;
import com.frank.ffmpeg.listener.OnHandleListener;
/**
* Handler消息处理器
* Created by frank on 2019/11/11.
*/
public class FFmpegHandler {
private final static String TAG = FFmpegHandler.class.getSimpleName();
public final static int MSG_BEGIN = 9012;
public final static int MSG_FINISH = 1112;
public final static int MSG_CONTINUE = 2012;
public final static int MSG_TOAST = 4562;
private Handler mHandler;
private boolean isContinue = false;
public FFmpegHandler(Handler mHandler) {
this.mHandler = mHandler;
}
public void isContinue(boolean isContinue) {
this.isContinue = isContinue;
}
/**
* 执行ffmpeg命令行
* @param commandLine commandLine
*/
public void executeFFmpegCmd(final String[] commandLine) {
if(commandLine == null) {
return;
}
FFmpegCmd.execute(commandLine, new OnHandleListener() {
@Override
public void onBegin() {
Log.i(TAG, "handle onBegin...");
mHandler.obtainMessage(MSG_BEGIN).sendToTarget();
}
@Override
public void onEnd(int result) {
Log.i(TAG, "handle onEnd...");
if(isContinue) {
mHandler.obtainMessage(MSG_CONTINUE).sendToTarget();
}else {
mHandler.obtainMessage(MSG_FINISH).sendToTarget();
}
}
});
}
}

@ -0,0 +1,10 @@
package com.frank.ffmpeg.listener;
/**
* 流程执行监听器
* Created by frank on 2019/11/11.
*/
public interface OnHandleListener {
void onBegin();
void onEnd(int result);
}
Loading…
Cancel
Save