Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to show progress bar with percentage. #100

Open
Vijayadhas opened this issue May 17, 2016 · 11 comments
Open

How to show progress bar with percentage. #100

Vijayadhas opened this issue May 17, 2016 · 11 comments

Comments

@Vijayadhas
Copy link

Please let me know how to show the progress in percentage(10%, 20% completed like this). We can calculate the percentage from this line in FFmpegExecuteAsyncTask .

try {
                String line;
                BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
                while ((line = reader.readLine()) != null) {
                    if (isCancelled()) {
                        return;
                    }

                    output += line+"\n";
                    publishProgress(line);//This line is important to get the process.
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

But Here I can not get the percentage in this line(publishProgress(line)). Please let me know how to show the progress with percentage. Please help me to solve this. Thanks in advance.

@AmruthPillai
Copy link

I would like to know how this can be done as well. Any leads?

@bananavoid
Copy link

+1

1 similar comment
@Silence17136
Copy link

+1

@Aman-B
Copy link

Aman-B commented May 31, 2017

Hey guys, I just formatted the string and extracted the time of video compressed and I came up with this method which returns the percentProgress in float data type. It's a hack but I hope it helps :

private float calculateProgress(String s) {
            String test = s.trim();
            int indexOftime= test.indexOf("time=");
            int beginOftime=indexOftime+5;

            String hrs=test.substring(beginOftime,beginOftime+2);
            String mins=test.substring(beginOftime+3,beginOftime+5);
            String sec= test.substring(beginOftime+6,beginOftime+8);
            String milisec=test.substring(beginOftime+9,beginOftime+11);
            Log.i("here",""+ "hrs: "+hrs + "mins: "+mins+"sec: "+sec +" milisec: "+milisec);



        float compressVideoLengthInSec= Float.parseFloat(hrs)*3600
                + Float.parseFloat(mins)*60
                +Float.parseFloat(sec)
                +Float.parseFloat(milisec)/1000;


        float percentProgress = (float)compressVideoLengthInSec/videoLengthInSec*100;
        Log.i("percent pro ",""+percentProgress + " "+videoLengthInSec + " "+compressVideoLengthInSec);
        return percentProgress;

    }

@ghost
Copy link

ghost commented Jun 12, 2017

Or you can use my function

long videoLengthInSec;

private void getVideoLength() {
     MediaPlayer mp = MediaPlayer.create(MainActivity.this, Uri.parse(basePath + "input.mp4"));
                    videoLengthInSec = TimeUnit.MILLISECONDS.toSeconds(mp.getDuration());
                    mp.release();
                    Log.d(TAG, "onStart: VideoLeng -> " + videoLengthInSec);
}

Pattern pattern = Pattern.compile("time=([\\d\\w:]+)");
private long getProgress(String message) {
                    if (message.contains("speed")) {
                        Matcher matcher = pattern.matcher(message);
                        matcher.find();
                        String tempTime = String.valueOf(matcher.group(1));
                        Log.d(TAG, "getProgress: tempTime " + tempTime);
                        String[] arrayTime = tempTime.split(":");
                        long currentTime =
                                TimeUnit.HOURS.toSeconds(Long.parseLong(arrayTime[0]))
                                + TimeUnit.MINUTES.toSeconds(Long.parseLong(arrayTime[1]))
                                + Long.parseLong(arrayTime[2]);

                        long percent = 100 * currentTime/videoLengthInSec;

                        Log.d(TAG, "currentTime -> " + currentTime + "s % -> " + percent);

                        return percent;
                    }
                    return 0;
                }

@Cdik
Copy link

Cdik commented Nov 3, 2017

Thanks @kimcy929.

I adapted your method with milliseconds to make the progress more smooth if you work with short videos (like in my case, less or equal to 3sec.). Otherwise, you only see 33%, 66%, 99%.

long videoLengthInMillis;

    private void getVideoLength(String basePath) {
        MediaPlayer mp = MediaPlayer.create(ctx, Uri.parse(basePath));
        videoLengthInMillis = TimeUnit.MILLISECONDS.toMillis(mp.getDuration());
        mp.release();
        Log.d(TAG, "onStart: VideoLeng -> " + videoLengthInMillis);
    }

    Pattern pattern = Pattern.compile("time=([\\d\\w:]{8}[\\w.][\\d]+)");
    private long getProgress(String message) {
        if (message.contains("speed")) {
            Matcher matcher = pattern.matcher(message);
            matcher.find();
            String tempTime = String.valueOf(matcher.group(1));
            Log.d(TAG, "getProgress: tempTime " + tempTime);
            String[] arrayTime = tempTime.split("[:|.]");
            long currentTime =
                    TimeUnit.HOURS.toMillis(Long.parseLong(arrayTime[0]))
                            + TimeUnit.MINUTES.toMillis(Long.parseLong(arrayTime[1]))
                            + TimeUnit.SECONDS.toMillis(Long.parseLong(arrayTime[2]))
                            + Long.parseLong(arrayTime[3]);

            long percent = 100 * currentTime/videoLengthInMillis;

            Log.d(TAG, "currentTime -> " + currentTime + "s % -> " + percent);

            return percent;
        }
        return 0;
    }

@bhavikmehta5491
Copy link

bhavikmehta5491 commented May 19, 2018

Creating MediaPlayer Instance is heavy and consumes a lot of memory

We can use MetadataRetriever for this

long videoLengthInMillis;
private void getVideoLength(Uri uri) {
     MediaMetadataRetriever retriever = new MediaMetadataRetriever();
//use one of overloaded setDataSource() functions to set your data source
                    retriever.setDataSource(getContext(), uri));
                    String time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
                    videoLengthInMillis= Long.parseLong(time);
                    
}

Pattern pattern = Pattern.compile("time=([\\d\\w:]+)");
private long getProgress(String message) {
        if (message.contains("speed")) {
            Matcher matcher = pattern.matcher(message);
            matcher.find();
            String tempTime = String.valueOf(matcher.group(1));
            Log.d(TAG, "getProgress: tempTime " + tempTime);
            String[] arrayTime = tempTime.split("[:|.]");
            long currentTime =
                    TimeUnit.HOURS.toMillis(Long.parseLong(arrayTime[0]))
                            + TimeUnit.MINUTES.toMillis(Long.parseLong(arrayTime[1]))
                            + TimeUnit.SECONDS.toMillis(Long.parseLong(arrayTime[2]))
                            + Long.parseLong(arrayTime[3]);

            long percent = 100 * currentTime/videoLengthInMillis;

            Log.d(TAG, "currentTime -> " + currentTime + "s % -> " + percent);

            return percent;
        }
        return 0;
    }


@paddykily
Copy link

You can also retrieve the duration from the message

long videoLengthInMillis;
Pattern durationPattern = Pattern.compile("Duration: ([\\d\\w:]{8}[\\w.][\\d]+)");
Pattern timePattern = Pattern.compile("time=([\\d\\w:]{8}[\\w.][\\d]+)");

public void onProgress(String message) {
        if (message.contains("Duration")){
            videoLengthInMillis += getTimeInMillis(message, durationPattern);
        }else if (message.contains("speed")) {
            long currentTime = getTimeInMillis(message, timePattern);
            long percent = 100 * currentTime/videoLengthInMillis;
            Log.d(TAG, "currentTime -> " + currentTime + "s % -> " + percent);
            if (percent < 100){
                progressDialog.setProgress((int) percent);
            }
        }
    }

private Long getTimeInMillis(String message, Pattern pattern){
        Matcher matcher = pattern.matcher(message);
        matcher.find();
        String time = String.valueOf(matcher.group(1));
        String[] arrayTime = time.split("[:.]");
        sLogger.info("time: " + time);
        Log.d(TAG, "time -> " + time);
        return TimeUnit.HOURS.toMillis(Long.parseLong(arrayTime[0]))
                + TimeUnit.MINUTES.toMillis(Long.parseLong(arrayTime[1]))
                + TimeUnit.SECONDS.toMillis(Long.parseLong(arrayTime[2]))
                + Long.parseLong(arrayTime[3]);
    }

@HBiSoft
Copy link

HBiSoft commented May 16, 2019

@Cdik I tried your answer, but the problem is that FFmpeg updates the progress every 0.5 second. So, even when using milliseconds, the progress is still 33 - 66 - 99 for example, instead of 1 - 2 - 3....

Do you have any solution for this?

@Cdik
Copy link

Cdik commented May 16, 2019

Hi @HBiSoft , it has been a long time I haven't used this library but I don't recall having 33-66-99 when using millis. I had something more smooth, like 11-22-33-....

There's maybe something wrong in your implementation. Can you show your code where you deal with the progress?

@HBiSoft
Copy link

HBiSoft commented May 16, 2019

@Cdik Thank you for replying. Yes mine isn't that bad, I was maybe over exaggerating. But when saving short videos it does "jump" quite a bit. I was hoping to get a smooth progression of percentages (more accurate), but seems like this isn't possible, especially with shorter videos.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants