From 3ffe907c02e2bd0d10391a9a6c5248a307b0a2a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E7=81=BF=E8=BE=89?= <122565648@qq.com> Date: Wed, 1 Apr 2020 22:07:12 +0800 Subject: [PATCH] Simply ffmpeg command build --- README.md | 15 ++++++-- .../invoker/demo/FFmpegTestActivity.java | 27 +++++++++++---- .../xch168/ffmpeg/invoker/FFmpegCmd.java | 34 +++++++++++++++++++ .../xch168/ffmpeg/invoker/FFmpegInvoker.java | 11 ++---- 4 files changed, 69 insertions(+), 18 deletions(-) create mode 100644 library/src/main/java/com/github/xch168/ffmpeg/invoker/FFmpegCmd.java diff --git a/README.md b/README.md index b7644d9..8be8aeb 100644 --- a/README.md +++ b/README.md @@ -18,8 +18,17 @@ android { ## Usage ```java -String cmd = "ffmpeg -y -ss 1 -t 100 -accurate_seek -i " + videoPath + " -codec copy " + savePath; -FFmpegInvoker.exec(cmd.split(" "), 100, new FFmpegInvoker.Callback() { +// ffmpeg -y -ss 1 -t 100 -accurate_seek -i + videoPath + -codec copy + savePath +// you can use FFmpegCmd to build an ffmpeg command +FFmpegCmd cmd = new FFmpegCmd(); +cmd.append("-ss").append(1); +cmd.append("-t").append(500); +cmd.append("-accurate_seek"); +cmd.append("-i").append(videoPath2); +cmd.append("-codec").append("copy"); +cmd.append(savePath); + +FFmpegInvoker.exec(cmd.build(), new FFmpegInvoker.Callback() { @Override public void onSuccess() { @@ -31,7 +40,7 @@ FFmpegInvoker.exec(cmd.split(" "), 100, new FFmpegInvoker.Callback() { } @Override - public void onProgress(float progress) { + public void onProgress(float percent) { } }); diff --git a/app/src/main/java/com/github/xch168/ffmpeg/invoker/demo/FFmpegTestActivity.java b/app/src/main/java/com/github/xch168/ffmpeg/invoker/demo/FFmpegTestActivity.java index 3e90590..40a7b95 100644 --- a/app/src/main/java/com/github/xch168/ffmpeg/invoker/demo/FFmpegTestActivity.java +++ b/app/src/main/java/com/github/xch168/ffmpeg/invoker/demo/FFmpegTestActivity.java @@ -1,17 +1,17 @@ package com.github.xch168.ffmpeg.invoker.demo; -import androidx.appcompat.app.AppCompatActivity; - import android.app.ProgressDialog; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.os.Environment; import android.os.Looper; -import android.util.Log; import android.view.View; import android.widget.Toast; +import androidx.appcompat.app.AppCompatActivity; + +import com.github.xch168.ffmpeg.invoker.FFmpegCmd; import com.github.xch168.ffmpeg.invoker.FFmpegInvoker; import java.io.File; @@ -86,15 +86,28 @@ public class FFmpegTestActivity extends AppCompatActivity { public void cutVideo(View view) { showProgress(); String savePath = getSaveDir() + "out.mp4"; - String cmd = "ffmpeg -y -ss 1 -t 500 -accurate_seek -i " + videoPath2 + " -codec copy " + savePath; - FFmpegInvoker.exec(cmd.split(" "), mCallback); + FFmpegCmd cmd = new FFmpegCmd(); + cmd.append("-ss").append(1); + cmd.append("-t").append(500); + cmd.append("-accurate_seek"); + cmd.append("-i").append(videoPath2); + cmd.append("-codec").append("copy"); + cmd.append(savePath); + +// String cmd = "ffmpeg -y -ss 1 -t 500 -accurate_seek -i " + videoPath2 + " -codec copy " + savePath; + FFmpegInvoker.exec(cmd.build(), mCallback); } public void extractFrame(View view) { showProgress(); String savePath = getSaveDir() + "out.png"; - String cmd = "ffmpeg -ss 10 -i " + videoPath + " -vframes 1 -y " + savePath; - FFmpegInvoker.exec(cmd.split(" "), mCallback); + FFmpegCmd cmd = new FFmpegCmd(); + cmd.append("-ss").append(10); + cmd.append("-i").append(videoPath); + cmd.append("-vframes").append(1); + cmd.append(savePath); +// String cmd = "ffmpeg -ss 10 -i " + videoPath + " -vframes 1 -y " + savePath; + FFmpegInvoker.exec(cmd.build(), mCallback); } public static String getSaveDir() { diff --git a/library/src/main/java/com/github/xch168/ffmpeg/invoker/FFmpegCmd.java b/library/src/main/java/com/github/xch168/ffmpeg/invoker/FFmpegCmd.java new file mode 100644 index 0000000..b633fb0 --- /dev/null +++ b/library/src/main/java/com/github/xch168/ffmpeg/invoker/FFmpegCmd.java @@ -0,0 +1,34 @@ +package com.github.xch168.ffmpeg.invoker; + +/** + * Created by XuCanHui on 2020/4/1. + */ +public class FFmpegCmd { + private StringBuilder sb; + + public FFmpegCmd() { + sb = new StringBuilder(); + sb.append("ffmpeg"); + append("-y"); + } + + public FFmpegCmd append(String param) { + sb.append(" ").append(param); + return this; + } + + public FFmpegCmd append(long param) { + sb.append(" ").append(param); + return this; + } + + public FFmpegCmd append(float param) { + sb.append(" ").append(param); + return this; + } + + public String build() { + return sb.toString(); + } + +} diff --git a/library/src/main/java/com/github/xch168/ffmpeg/invoker/FFmpegInvoker.java b/library/src/main/java/com/github/xch168/ffmpeg/invoker/FFmpegInvoker.java index 0e4180c..8376e62 100644 --- a/library/src/main/java/com/github/xch168/ffmpeg/invoker/FFmpegInvoker.java +++ b/library/src/main/java/com/github/xch168/ffmpeg/invoker/FFmpegInvoker.java @@ -22,16 +22,11 @@ public class FFmpegInvoker { public static native String getAVFormatInfo(); public static native String getAVFilterInfo(); - public static void exec(String[] cmds, Callback listener) { + public static void exec(String cmd, Callback listener) { sCallback = listener; - StringBuilder cmdBuilder = new StringBuilder(); - for (String cmd : cmds) - { - cmdBuilder.append(" ").append(cmd); - } - - Log.i(TAG, "ffmpeg cmd:" + cmdBuilder.toString()); + String[] cmds = cmd.split(" "); + Log.i(TAG, "ffmpeg cmd:" + cmd); exec(cmds.length, cmds); }