'How to get FFMPEG to show 100% finished in Java?

            public void run() {
                Scanner sc = new Scanner(process.getErrorStream());
                
                Pattern durPattern = Pattern.compile("(?<=Duration: )[^,]*");
                String dur = sc.findWithinHorizon(durPattern, 0);
                if (dur == null)
                  throw new RuntimeException("Could not parse duration.");
                String[] hms = dur.split(":");
                double totalSecs = Integer.parseInt(hms[0]) * 3600
                                 + Integer.parseInt(hms[1]) *   60
                                 + Double.parseDouble(hms[2]);
                System.out.println("Total duration: " + totalSecs + " seconds.");

                Pattern timePattern = Pattern.compile("(?<=time=)[^ ]*");
                String match;
                String[] matchSplit;
                while (null != (match = sc.findWithinHorizon(timePattern, 0))) {
                    matchSplit = match.split(":");
                    int hour = Integer.parseInt(matchSplit[0]) * 3600;
                    int minute = Integer.parseInt(matchSplit[1]) * 60;
                    double second = Double.parseDouble(matchSplit[2]);
                    double progress = (hour + minute + second) / totalSecs;
                    System.out.printf("Progress: %.2f%%%n", progress * 100);
                }
            }
        }.start();

Output:

Total duration: 9.94 seconds.
Progress: 3.72%
Progress: 33.10%
Progress: 51.11%
Progress: 64.89%
Progress: 80.08%
Progress: 98.99%
Progress: 99.50%

Looking at the CLI, sometimes the progress duration will never hit the total duration so the percentage will never hit 100%. The only way I can see it reaching 100% is if we hardcode it in, like when it reaches 99.50+% then just print 100% but what if the speed is only going up by 0.01% every second? Can't think of any consistent way of achieving this. Thoughts?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source