sunil yadav
sunil yadav

Reputation: 49

How to add progress bar to FFMPEG android

I want add a progress bar during FFMPEG execution android.

When i start FFMPEG command then progress bar start with percentage progress.

Upvotes: 4

Views: 7960

Answers (4)

Pratik Butani
Pratik Butani

Reputation: 62411

Here is the solutions with progress value If you are using https://github.com/tanersener/mobile-ffmpeg.

Get duration of video:

int videoLength = MediaPlayer.create(mContext, selectedUri).getDuration();

Config enableStatisticsCallback method:

     Config.enableStatisticsCallback(new StatisticsCallback() {
                public void apply(Statistics newStatistics) {
                    float progress = Float.parseFloat(String.valueOf(newStatistics.getTime())) / videoLength;
                    float progressFinal = progress * 100;
                    Log.d(TAG, "Video Length: " + progressFinal);
                    Log.d(Config.TAG, String.format("frame: %d, time: %d", newStatistics.getVideoFrameNumber(), newStatistics.getTime()));
                    Log.d(Config.TAG, String.format("Quality: %f, time: %f", newStatistics.getVideoQuality(), newStatistics.getVideoFps()));
                    progressDialog.setProgress((int) progressFinal);
                }
            });

     com.arthenica.mobileffmpeg.FFmpeg.executeAsync(command, (executionId, returnCode) -> {
                progressDialog.dismiss();
                if (returnCode == RETURN_CODE_SUCCESS) {
                    Toast.makeText(mContext, "Video Saved in Folder : " + getString(R.string.app_name), Toast.LENGTH_SHORT).show();
                    Log.i(Config.TAG, "Async command execution completed successfully.");
                } else if (returnCode == RETURN_CODE_CANCEL) {
                    Log.i(Config.TAG, "Async command execution cancelled by user.");
                } else {
                    Toast.makeText(mContext, "Something Went Wrong", Toast.LENGTH_SHORT).show();
                    Log.i(Config.TAG, String.format("Async command execution failed with rc=%d.", returnCode));
                }
            });

Hope it will be helpful.

Upvotes: 4

Lenovo lover
Lenovo lover

Reputation: 1

I use it every where:

Pattern timePattern = Pattern.compile("(?<=time=)[\\d:.]*");
Scanner sc = new Scanner(message);

String match = sc.findWithinHorizon(timePattern, 0);
if (match != null) {
  String[] matchSplit = match.split(":");
  if (totalDur != 0) {
    float progress = (Integer.parseInt(matchSplit[0]) * 3600 +
          Integer.parseInt(matchSplit[1]) * 60 +
          Float.parseFloat(matchSplit[2])) / totalDur;
    int showProgress = (int) (progress * 100000);
    progressDialog.setProgress(showProgress);
  }
}

Use it at onProgress method.

Upvotes: 0

dastan
dastan

Reputation: 930

To Calculate ffmpeg progress in percentage

ffmpeg.execute(command, new ExecuteBinaryResponseHandler() {
            @Override
            public void onFailure(String s) {
                Log.d(TAG, "FAILED with output : " + s);
            }

            @Override
            public void onSuccess(String s) {
                Log.d(TAG, "SUCCESS with output : " + s);
            }

            @Override
            public void onProgress(String s) {
                Log.d(TAG, "Started command : ffmpeg " + Arrays.toString(command));
                Log.d(TAG, "progress : " + s);
                Pattern timePattern = Pattern.compile("(?<=time=)[\\d:.]*");
                Scanner sc = new Scanner(s);

                String match = sc.findWithinHorizon(timePattern, 0);
                if (match != null) {
                    String[] matchSplit = match.split(":");
                    if (totalDur != 0) {
                        float progress = (Integer.parseInt(matchSplit[0]) * 3600 +
                                Integer.parseInt(matchSplit[1]) * 60 +
                                Float.parseFloat(matchSplit[2])) / totalDur;
                        float showProgress = (progress * 100);
                        Log.d(TAG, "=======PROGRESS======== " + showProgress);
                    }
                }
            }

            @Override
            public void onStart() {
                Log.d(TAG, "Started command : ffmpeg " + Arrays.toString(command));
                progressDialog.setMessage("Processing...");
                progressDialog.show();
            }

            @Override
            public void onFinish() {
                Log.d(TAG, "Finished command : ffmpeg " + Arrays.toString(command));
                progressDialog.dismiss();
            }
        });

totalDur=25; // for 25 Sec Video

But totalDur will change according to operation like for 2x Slow Video you have to give totalDur=2*25; //50 Sec Video

Upvotes: 9

Ben Denger
Ben Denger

Reputation: 248

I added a ProgressDialog using the video time

final int msec = MediaPlayer.create(this, Uri.fromFile(new File(path))).getDuration();

 final ProgressDialog dialog = new ProgressDialog(this);
        dialog.setMax(msec);
        dialog.setMessage("Progress");
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        dialog.setProgress(0);

then in the OnStart Method of ExecuteBinaryResponseHandler call dialog.show() and update the Progress in the onProgress(String message) Method

@Override
public void onProgress(String message) {
    int start = message.indexOf("time=");
        int end = message.indexOf(" bitrate");
        if (start != -1 && end != -1) {
            String duration = message.substring(start + 5, end);
            if (duration != "") {
                try {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
                    sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
                    dialog.setProgress((int)sdf.parse("1970-01-01 " + duration).getTime());                        
                }catch (ParseException e)
                {
                    e.printStackTrace();
                }
            }
    }
}

dismiss the dialog in the OnFinish() method

@Override
public void onFinish() {
    String s = System.currentTimeMillis() - MainActivity.this.startTime + "";
    dialog.dismiss();
}

Upvotes: 2

Related Questions