Simply ffmpeg progress update

master
徐灿辉 5 years ago
parent 46177bb73b
commit 8d8326260d
  1. 16
      app/src/main/java/com/github/xch168/ffmpeg/invoker/demo/FFmpegTestActivity.java
  2. 4
      library/src/main/cpp/ffmpeg-invoker.c
  3. 2
      library/src/main/cpp/ffmpeg-invoker.h
  4. 8
      library/src/main/cpp/ffmpeg/ffmpeg.c
  5. 2
      library/src/main/cpp/ffmpeg/ffmpeg.h
  6. 4
      library/src/main/cpp/ffmpeg/ffmpeg_opt.c
  7. 14
      library/src/main/java/com/github/xch168/ffmpeg/invoker/FFmpegInvoker.java

@ -8,6 +8,7 @@ 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;
@ -19,6 +20,7 @@ public class FFmpegTestActivity extends AppCompatActivity {
private ProgressDialog mProgressDialog;
private String videoPath = "/storage/emulated/0/DCIM/Camera/fb639313f7f3d58cc793f20095439c88.mp4";
private String videoPath2 = "/storage/emulated/0/4399YouPai/Record/LYL_20200316_155722.mp4";
private FFmpegInvoker.Callback mCallback = new FFmpegInvoker.Callback() {
@Override
@ -38,8 +40,8 @@ public class FFmpegTestActivity extends AppCompatActivity {
}
@Override
public void onProgress(float progress) {
updateProgress((int) progress);
public void onProgress(float percent) {
updateProgress(percent);
}
};
@ -77,22 +79,22 @@ public class FFmpegTestActivity extends AppCompatActivity {
}
}
private void updateProgress(int percent) {
mProgressDialog.setProgress(percent);
private void updateProgress(float percent) {
mProgressDialog.setProgress((int) (percent * mProgressDialog.getMax()));
}
public void cutVideo(View view) {
showProgress();
String savePath = getSaveDir() + "out.mp4";
String cmd = "ffmpeg -y -ss 1 -t 300 -accurate_seek -i " + videoPath + " -codec copy " + savePath;
FFmpegInvoker.exec(cmd.split(" "), 100, mCallback);
String cmd = "ffmpeg -y -ss 1 -t 500 -accurate_seek -i " + videoPath2 + " -codec copy " + savePath;
FFmpegInvoker.exec(cmd.split(" "), 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(" "), 100, mCallback);
FFmpegInvoker.exec(cmd.split(" "), mCallback);
}
public static String getSaveDir() {

@ -56,10 +56,10 @@ static void ffmpeg_callback(int ret) {
(*jvm)->DetachCurrentThread(jvm);
}
void ffmpeg_progress(float progress) {
void ffmpeg_progress(float percent) {
JNIEnv *env;
(*jvm)->AttachCurrentThread(jvm, (void **) &env, NULL);
callJavaMethodProgress(env, m_clazz,progress);
callJavaMethodProgress(env, m_clazz,percent);
(*jvm)->DetachCurrentThread(jvm);
}

@ -29,4 +29,4 @@ Java_com_github_xch168_ffmpeg_invoker_FFmpegInvoker_getAVFilterInfo(JNIEnv *, jc
#endif
#endif
void ffmpeg_progress(float progress);
void ffmpeg_progress(float percent);

@ -1671,7 +1671,8 @@ static void print_report(int is_last_report, int64_t timer_start, int64_t cur_ti
const char *hours_sign;
int ret;
float t;
float mss;
int64_t current_time;
float percent;
if (!print_stats && !is_last_report && !progress_avio)
return;
@ -1771,9 +1772,9 @@ static void print_report(int is_last_report, int64_t timer_start, int64_t cur_ti
nb_frames_drop += ost->last_dropped;
}
current_time = FFABS(pts);
secs = FFABS(pts) / AV_TIME_BASE;
us = FFABS(pts) % AV_TIME_BASE;
mss = secs + ((float) us / AV_TIME_BASE);
mins = secs / 60;
secs %= 60;
hours = mins / 60;
@ -1783,7 +1784,8 @@ static void print_report(int is_last_report, int64_t timer_start, int64_t cur_ti
bitrate = pts && total_size >= 0 ? total_size * 8 / (pts / 1000.0) : -1;
speed = t != 0.0 ? (double)pts / AV_TIME_BASE / t : -1;
ffmpeg_progress(mss);
percent = duration > 0 ? (float)current_time / duration : 0;
ffmpeg_progress(percent);
if (total_size < 0) av_bprintf(&buf, "size=N/A time=");
else av_bprintf(&buf, "size=%8.0fkB time=", total_size / 1024.0);

@ -611,6 +611,8 @@ extern int filter_nbthreads;
extern int filter_complex_nbthreads;
extern int vstats_version;
extern int64_t duration;
extern const AVIOInterruptCB int_cb;
extern const OptionDef options[];

@ -111,6 +111,8 @@ int filter_nbthreads = 0;
int filter_complex_nbthreads = 0;
int vstats_version = 2;
int64_t duration = 0;
static int intra_only = 0;
static int file_overwrite = 0;
@ -1103,11 +1105,13 @@ static int open_input_file(OptionsContext *o, const char *filename)
/* open the input file with generic avformat function */
err = avformat_open_input(&ic, filename, file_iformat, &o->g->format_opts);
if (err < 0) {
duration = 0;
print_error(filename, err);
if (err == AVERROR_PROTOCOL_NOT_FOUND)
av_log(NULL, AV_LOG_ERROR, "Did you mean file:%s?\n", filename);
exit_program(1);
}
duration = ic->duration + (ic->duration <= INT64_MAX - 5000 ? 5000 : 0);
if (scan_all_pmts_set)
av_dict_set(&o->g->format_opts, "scan_all_pmts", NULL, AV_DICT_MATCH_CASE);
remove_avoptions(&o->g->format_opts, o->g->codec_opts);

@ -13,7 +13,6 @@ public class FFmpegInvoker {
}
private static Callback sCallback;
private static long sDuration;
public static native int exec(int argc, String[] argv);
public static native void exit();
@ -23,9 +22,8 @@ public class FFmpegInvoker {
public static native String getAVFormatInfo();
public static native String getAVFilterInfo();
public static void exec(String[] cmds, long duration, Callback listener) {
public static void exec(String[] cmds, Callback listener) {
sCallback = listener;
sDuration = duration;
StringBuilder cmdBuilder = new StringBuilder();
for (String cmd : cmds)
@ -44,7 +42,7 @@ public class FFmpegInvoker {
public static void onExecuted(int ret) {
if (sCallback != null) {
if (ret == 0) {
sCallback.onProgress(sDuration);
sCallback.onProgress(1);
sCallback.onSuccess();
} else {
sCallback.onFailure();
@ -55,17 +53,15 @@ public class FFmpegInvoker {
/**
* FFmpeg执行进度回调由C代码调用
*/
public static void onProgress(float progress) {
public static void onProgress(float percent) {
if (sCallback != null) {
if (sDuration != 0) {
sCallback.onProgress(progress / (sDuration / 1000) * 0.95f);
}
sCallback.onProgress(percent);
}
}
public interface Callback {
void onSuccess();
void onFailure();
void onProgress(float progress);
void onProgress(float percent);
}
}

Loading…
Cancel
Save