Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.0k views
in Technique[技术] by (71.8m points)

get video duration from URL in android app

I'm working on an app where the user can see all the video's information and title that are stored on a SERVER. I'm almost done with it except that no matter how I code it, I cannot get the video duration from a given URL. Lets take this demo video from somewhere on the internet: CLICK HERE FOR VIDEO PATH I want the app to get the video duration without the need to open the video itself.

The Code that I'm trying to use on android is this:

MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    retriever.setDataSource("https://12-lvl3-pdl.vimeocdn.com/01/1386/0/6932347/10573836.mp4?expires=1461047937&token=037972137fdfc4c2d9902");
    String time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
    long timeInmillisec = Long.parseLong( time );
    long duration = timeInmillisec / 1000;
    long hours = duration / 3600;
    long minutes = (duration - hours * 3600) / 60;
    long seconds = duration - (hours * 3600 + minutes * 60);
    Toast.makeText(context,Long.toString(timeInmillisec),Toast.LENGTH_SHORT).show();

But the result that i'm getting is an: java.lang.IllegalArgumentException at line 2 which is " retriever.setDataSource() ". Can anyone help me find what i'm doing wrong or does android provide another way to get the required information?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...