get video duration from URL in android app

大兔子大兔子 提交于 2019-12-01 07:36:32

Maybe you are looking for the FFmpegMediaMetadataRetriever

FFmpegMediaMetadataRetriever class provides a unified interface for the retrieving frame and metadata from an input media file.

By using METADATA_KEY_DURATION constant of FFmpegMediaMetadataRetriever you can get the duration of your video.It will return the string to you then you can convert it into LONG to get TIME.

Here is the code you should use:

FFmpegMediaMetadataRetriever mFFmpegMediaMetadataRetriever = new MediaMetadataRetriever();
mFFmpegMediaMetadataRetriever .setDataSource("Your video url");
String mVideoDuration =  mFFmpegMediaMetadataRetriever .extractMetadata(FFmpegMediaMetadataRetriever .METADATA_KEY_DURATION);
long mTimeInMilliseconds= Long.parseLong(mVideoDuration);

if above still not work then use

MediaMetadataRetriever retriever = new MediaMetadataRetriever();
if (Build.VERSION.SDK_INT >= 14)
 retriever.setDataSource("Your video url", new HashMap<String, String>());
else
 retriever.setDataSource("Your video url");

From your code.

Hope it will help you. Best of luck.

This is working for me ,

 MediaMetadataRetriever retriever = new MediaMetadataRetriever();
  retriever.setDataSource("YourVideofileUrl", new HashMap<String, String>());
  String time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
  long timeInMillisec = Long.parseLong(time);
  retriever.release();
  String duration=convertMillieToHMmSs(timeInMillisec); //use this duration

and

 public static String convertMillieToHMmSs(long millie) {
    long seconds = (millie / 1000);
    long second = seconds % 60;
    long minute = (seconds / 60) % 60;
    long hour = (seconds / (60 * 60)) % 24;

    String result = "";
    if (hour > 0) {
        return String.format("%02d:%02d:%02d", hour, minute, second);
    }
    else {
        return String.format("%02d:%02d" , minute, second);
    }

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!