bravobit / FFmpeg-Android

FFMpeg/FFprobe compiled for Android
https://bravobit.nl/
MIT License
733 stars 172 forks source link

怎么监听指令的执行进度呢,按百分比显示(How to display progress by percent?) #148

Closed XinYiWorld closed 4 years ago

HBiSoft commented 4 years ago

You can get the percentage like this:

@Override
    public void onProgress(String message) {

        Pattern pattern = Pattern.compile("time=([\\d\\w:]{8}[\\w.][\\d]+)");

        if (message.contains("speed")) {
            Matcher matcher = pattern.matcher(message);

            if (matcher.find()) {
                String tempTime = String.valueOf(matcher.group(1));
                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]);

                //Calculate percentage
                long percent = 100 * currentTime / lengthOfVideoInMilliSeconds;

                //Set percentage to TextView
                percentText.setText(percent + " %");

            }
        }
    }
XinYiWorld commented 4 years ago

Thanks for your help,it solved my problem but the progress step is a little large。 I found a resolution to share with you :

1.get the total frames of the video:

-v error -count_frames -select_streams v:0 -show_entries stream=nb_read_frames -of default=nokey=1:noprint_wrappers=1 src.mp4

2.split current frame of the video:

if(message.contains("frame=") && message.contains("fps=") ){ int indexOfFps = message.indexOf("fps="); int curFrame = Integer.parseInt(message.substring(7, indexOfFps).trim()); progressDialog.setProgress((int) (curFrame*100f/ totalFrame[0])); } this is a stupid method,it have to execute 2 ffmpeg commands。