Search code examples
javaandroidandroid-ffmpeg

Calculating video duration take too much time


From my arraylist am trying to display video file name, and duration inRecycler view when i display only name there is no delay, But when i try to display name, duration it take a while.

the delay happen with just 15 to 20 files.

So how do i calculate video file duration faster.

here is what i do.

String folderName = bundle.getString("folderName");
        setTitle(folderName);

        for (int i = 0; i < Constant.allMediaList.size(); i++) {
            if (folderName.equals(new File(String.valueOf(Constant.allMediaList.get(i))).getParentFile().getName())) {
                String fileName = FilenameUtils.getBaseName(Constant.allMediaList.get(i).getAbsolutePath());
                String duration = getDuration(Constant.allMediaList.get(i).getAbsolutePath(), context);
                String path = Constant.allMediaList.get(i).getAbsolutePath();
                videosPath.add(new VideoModel(fileName, duration, path));
            }
        }


public static String getDuration(String absolutePathThumb, Context context) {
    try{
        FFmpegMediaMetadataRetriever retriever = new FFmpegMediaMetadataRetriever();
        retriever.setDataSource(context , Uri.parse(absolutePathThumb));
        String time = retriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_DURATION);
        long timeInMillisec = Long.parseLong(time);
        return convertMillieToHMmSs(timeInMillisec);
    }catch(Exception e){
        e.printStackTrace();
        return null;
    }
}

Above is what am trying to get file duration and name. What am i doing wrong ?

and my ArrayList is just file path, nothing is indexed.


Solution

  • You can try not create new element of FFmpegMediaMetadataRetriever for each file and pass it as parameter in function. Create it outside of for loop:

    public static String getDuration(String absolutePathThumb, Context context, FFmpegMediaMetadataRetriever retriever) {
        try{
            retriever.setDataSource(context , Uri.parse(absolutePathThumb));
            String time = retriever.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_DURATION);
            long timeInMillisec = Long.parseLong(time);
            return convertMillieToHMmSs(timeInMillisec);
        }catch(Exception e){
            e.printStackTrace();
            return null;
        }
    }
    

    I'm not sure but think it could help.