Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 772
sunil yadav Avatar asked Oct 28 '25 17:10

sunil yadav


2 Answers

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

like image 176
dastan Avatar answered Oct 31 '25 06:10

dastan


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.

like image 40
Pratik Butani Avatar answered Oct 31 '25 06:10

Pratik Butani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!