Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the duration, resolution of a video file programmatically in Android?

Tags:

android

I hope to get the duration, resolution of a video file of a video (.mp4), how can I do that programmatically in Android ? My minSdkVersion is 21 .

BTW, I can get the file size of a video using the following code.

String recVideoPath = Environment.getExternalStorageDirectory() + videoRecordedFileName;

File file = new File(recVideoPath);
long fileVideo = file.length();

Thanks!

like image 937
HelloCW Avatar asked Oct 17 '25 14:10

HelloCW


1 Answers

To get duration and resolution MediaMetadataRetriever could be used like:

MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(uriOfFile);
long duration = Long.parseLong(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION))
int width = Integer.valueOf(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH));
int height = Integer.valueOf(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT));
retriever.release();

uriOfFile is a string with the url to you mp4.

like image 52
Maantje Avatar answered Oct 20 '25 06:10

Maantje



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!