How to add progress bar to FFMPEG android

2019-02-15 17:45发布

问题:

I want add a progress bar during FFMPEG execution android.

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

回答1:

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


回答2:

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