Calculating video duration take too much time

huangapple go评论110阅读模式
英文:

Calculating video duration take too much time

问题

从我的ArrayList中,我尝试在Recycler视图中显示视频文件的名称和持续时间,当我仅显示名称时,没有延迟,但是当我尝试显示名称,持续时间时,就会有一些延迟。

这种延迟仅在大约15到20个文件时发生。

那么,我如何更快地计算视频文件的持续时间。

以下是我所做的。

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;
    }
}

以上是我尝试获取文件持续时间和名称的方法。我做错了什么吗?

而且我的ArrayList中只有文件路径,没有索引。

英文:

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(&quot;folderName&quot;);
        setTitle(folderName);

        for (int i = 0; i &lt; 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.

答案1

得分: 0

以下是翻译好的部分:

你可以尝试不为每个文件创建新的FFmpegMediaMetadataRetriever元素,并将其作为参数传递给函数。请在for循环外部创建它:

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;
    }
}
我不确定但认为这可能会有所帮助
英文:

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.

huangapple
  • 本文由 发表于 2020年10月8日 02:32:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/64250268.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定