From 8d8326260d0c9ebcd6dea221e2b4c5821dd781f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E7=81=BF=E8=BE=89?= <122565648@qq.com> Date: Tue, 24 Mar 2020 23:44:55 +0800 Subject: [PATCH] Simply ffmpeg progress update --- .../ffmpeg/invoker/demo/FFmpegTestActivity.java | 16 +++++++++------- library/src/main/cpp/ffmpeg-invoker.c | 4 ++-- library/src/main/cpp/ffmpeg-invoker.h | 2 +- library/src/main/cpp/ffmpeg/ffmpeg.c | 8 +++++--- library/src/main/cpp/ffmpeg/ffmpeg.h | 2 ++ library/src/main/cpp/ffmpeg/ffmpeg_opt.c | 4 ++++ .../xch168/ffmpeg/invoker/FFmpegInvoker.java | 14 +++++--------- 7 files changed, 28 insertions(+), 22 deletions(-) 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 9293019..3e90590 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 @@ -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() { diff --git a/library/src/main/cpp/ffmpeg-invoker.c b/library/src/main/cpp/ffmpeg-invoker.c index 885fe3b..9a60690 100644 --- a/library/src/main/cpp/ffmpeg-invoker.c +++ b/library/src/main/cpp/ffmpeg-invoker.c @@ -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); } diff --git a/library/src/main/cpp/ffmpeg-invoker.h b/library/src/main/cpp/ffmpeg-invoker.h index 0cafa02..2c6d8b7 100644 --- a/library/src/main/cpp/ffmpeg-invoker.h +++ b/library/src/main/cpp/ffmpeg-invoker.h @@ -29,4 +29,4 @@ Java_com_github_xch168_ffmpeg_invoker_FFmpegInvoker_getAVFilterInfo(JNIEnv *, jc #endif #endif -void ffmpeg_progress(float progress); \ No newline at end of file +void ffmpeg_progress(float percent); \ No newline at end of file diff --git a/library/src/main/cpp/ffmpeg/ffmpeg.c b/library/src/main/cpp/ffmpeg/ffmpeg.c index 536169d..55d3a54 100644 --- a/library/src/main/cpp/ffmpeg/ffmpeg.c +++ b/library/src/main/cpp/ffmpeg/ffmpeg.c @@ -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); diff --git a/library/src/main/cpp/ffmpeg/ffmpeg.h b/library/src/main/cpp/ffmpeg/ffmpeg.h index a62e1be..66fde9c 100644 --- a/library/src/main/cpp/ffmpeg/ffmpeg.h +++ b/library/src/main/cpp/ffmpeg/ffmpeg.h @@ -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[]; diff --git a/library/src/main/cpp/ffmpeg/ffmpeg_opt.c b/library/src/main/cpp/ffmpeg/ffmpeg_opt.c index f5ca18a..5ace866 100644 --- a/library/src/main/cpp/ffmpeg/ffmpeg_opt.c +++ b/library/src/main/cpp/ffmpeg/ffmpeg_opt.c @@ -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); 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 998d4f4..0e4180c 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 @@ -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); } }