Simply ffmpeg command build

master
徐灿辉 5 years ago
parent c174c66302
commit 3ffe907c02
  1. 15
      README.md
  2. 27
      app/src/main/java/com/github/xch168/ffmpeg/invoker/demo/FFmpegTestActivity.java
  3. 34
      library/src/main/java/com/github/xch168/ffmpeg/invoker/FFmpegCmd.java
  4. 11
      library/src/main/java/com/github/xch168/ffmpeg/invoker/FFmpegInvoker.java

@ -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) {
}
});

@ -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() {

@ -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();
}
}

@ -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);
}

Loading…
Cancel
Save