I want add a progress bar during FFMPEG execution android. 
When i start FFMPEG command then progress bar start with percentage progress.
            Asked
            
        
        
            Active
            
        
            Viewed 7,196 times
        
    4
            
            
        
        Amruth
        
- 5,792
 - 2
 - 28
 - 41
 
        sunil yadav
        
- 49
 - 1
 - 3
 
4 Answers
9
            
            
        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
        dastan
        
- 892
 - 10
 - 17
 
- 
                    @Dastan.lqbal What if you don't know the length of the video that will be saved? – HB. May 03 '17 at 05:49
 - 
                    In that case you can grab the total frames from the video, and then you can match – dastan May 03 '17 at 10:55
 - 
                    Can you please edit/extend your answer and give a example of how I can achieve this? – HB. May 05 '17 at 05:30
 - 
                    I also get a error at `Scanner sc = new Scanner(s);` `Cannot resolve symbol "s"` ---- Sorry that was my bad, S is the string name. – HB. May 05 '17 at 05:45
 - 
                    @H.Brooks you can check this answer to get total number of frames http://stackoverflow.com/questions/2017843/fetch-frame-count-with-ffmpeg – dastan May 05 '17 at 09:37
 - 
                    or you can check this code https://github.com/bytedeco/javacv/blob/08cd78d3b91af242f2fd780a18ea092c55dc1d03/src/main/java/org/bytedeco/javacv/FFmpegFrameGrabber.java – dastan May 05 '17 at 09:41
 - 
                    Great, thank you. In my logcat I can see the total time of the video `Duration: 00:00:11.44, start: 0.000000, bitrate: 1681 kb/s` but I don't know how to get that into a string and use your method above. – HB. May 05 '17 at 11:51
 
4
            
            
        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.
        Pratik Butani
        
- 60,504
 - 58
 - 273
 - 437
 
- 
                    How can i get video duration before creating it, that's why video duration not getting. – Gaurav Mandlik Dec 27 '21 at 09:39
 
2
            
            
        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();
}
        Ben Denger
        
- 228
 - 1
 - 11
 
0
            
            
        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.
        vard
        
- 4,057
 - 2
 - 26
 - 46