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

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

@ -29,4 +29,4 @@ Java_com_github_xch168_ffmpeg_invoker_FFmpegInvoker_getAVFilterInfo(JNIEnv *, jc
#endif #endif
#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; const char *hours_sign;
int ret; int ret;
float t; float t;
float mss; int64_t current_time;
float percent;
if (!print_stats && !is_last_report && !progress_avio) if (!print_stats && !is_last_report && !progress_avio)
return; 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; nb_frames_drop += ost->last_dropped;
} }
current_time = FFABS(pts);
secs = FFABS(pts) / AV_TIME_BASE; secs = FFABS(pts) / AV_TIME_BASE;
us = FFABS(pts) % AV_TIME_BASE; us = FFABS(pts) % AV_TIME_BASE;
mss = secs + ((float) us / AV_TIME_BASE);
mins = secs / 60; mins = secs / 60;
secs %= 60; secs %= 60;
hours = mins / 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; bitrate = pts && total_size >= 0 ? total_size * 8 / (pts / 1000.0) : -1;
speed = t != 0.0 ? (double)pts / AV_TIME_BASE / t : -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="); if (total_size < 0) av_bprintf(&buf, "size=N/A time=");
else av_bprintf(&buf, "size=%8.0fkB time=", total_size / 1024.0); 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 filter_complex_nbthreads;
extern int vstats_version; extern int vstats_version;
extern int64_t duration;
extern const AVIOInterruptCB int_cb; extern const AVIOInterruptCB int_cb;
extern const OptionDef options[]; extern const OptionDef options[];

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

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

Loading…
Cancel
Save