Is it possible to get the resolution of a video in Android?
Asked Answered
G

4

19

I'm looking for a way to get the resolution of any given video in Android. It doesn't have to work with other formats than the ones supported in Android, but it'd be great if it did. If you're unsure of the supported formats in Android, please refer to this page:

http://developer.android.com/guide/appendix/media-formats.html

I think it's possible to do what I want using the MediaPlayer class, but that seems incredibly stupid and inefficient. Also, I'd like a way that's relatively fast.

I'm targeting Android 3.0+, if that makes any difference. Honeycomb also supports .mkv files, although it's not officially supported until Android 4.0.

Gailgaile answered 3/11, 2011 at 19:50 Comment(0)
G
54

You can use the MediaMetadataRetriever to retrieve resolution information about a video file. You'd use the extractMetadata() method and using the METADATA_KEY_VIDEO_HEIGHT and METADATA_KEY_VIDEO_WIDTH constants. So you'd do something like this:

MediaMetadataRetriever metaRetriever = new MediaMetadataRetriever();
metaRetriever.setDataSource(/* file descriptor or file path goes here */);
String height = metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT);
String width = metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH);
Gastrology answered 3/11, 2011 at 19:54 Comment(5)
That seems absolutely brilliant! Can't believe I've never heard of that class before. Thanks a lot! :)Gailgaile
Is there any easy way for Pre-Honeycomb?Favoritism
MediaMetadataRetriever requires API level 14Powys
You're wrong. MediaMetadataRetriever as added in API 10. If you want to retrieve metadata prior to API 10 you can use FFmpegMediaMetadataRetriever.Saritasarkaria
does it return in String type? int would be betterAccessory
P
7

Trick for API 10+:

MediaMetadataRetriever mRetriever = new MediaMetadataRetriever();
mRetriever.setDataSource("video_file_path");
Bitmap frame = mRetriever.getFrameAtTime();

int width = frame.getWidth();
int height = frame.getHeight();

We are simple taking a random frame from our video and detecting its width and height.

Perlite answered 12/12, 2014 at 5:40 Comment(0)
A
2

If you have Video Content URI then you can also use content provider query

Like

    Cursor mediaCursor = getContentResolver().query(
                        videoContentURI,
                        {MediaStore.Video.Media.RESOLUTION}, null, null, null);
        if (mediaCursor != null) {
                    if (mediaCursor.moveToFirst()) {
        //The resolution of the video file, formatted as "XxY"
        String resolution=mediaCursor.getString(mediaCursor
                                  .getColumnIndex(MediaStore.Video.Media.RESOLUTION));
    }
}

You can check following post to get ContentURI from video Physical Path

Amick answered 26/8, 2017 at 7:53 Comment(0)
A
1

Kotlin Extension Solution

Here is the way to fetch media file dimensions in Kotlin

val (width, height) = myFile.getMediaDimensions(context)

fun File.getMediaDimensions(context: Context): Pair<Int, Int>? {
    if (!exists()) return null
    val retriever = MediaMetadataRetriever()
    retriever.setDataSource(context, Uri.parse(absolutePath))

    val width = retriever.extractMetadata(METADATA_KEY_VIDEO_WIDTH).toIntOrNull() ?: return null
    val height = retriever.extractMetadata(METADATA_KEY_VIDEO_HEIGHT).toIntOrNull() ?: return null

    retriever.release()
    return Pair(width, height)
}

If you want to make it safer (Uri.parse could throw exception), use this combination. The others are generally just useful as well :)

fun String?.asUri(): Uri? {
    try {
        return Uri.parse(this)
    } catch (e: Exception) {
    }
    return null
}

val File.uri get() = absolutePath.asUri()

fun File.getMediaDimensions(context: Context): Pair<Int, Int>? {
    if (!exists()) return 0
    val retriever = MediaMetadataRetriever()
    retriever.setDataSource(context, uri)

    val width = retriever.extractMetadata(METADATA_KEY_VIDEO_WIDTH).toIntOrNull() ?: return null
    val height = retriever.extractMetadata(METADATA_KEY_VIDEO_HEIGHT).toIntOrNull() ?: return null

    retriever.release()
    return Pair(width, height)
}

Not necessary here, but generally helpful additional Uri extensions

val Uri?.exists get() = if (this == null) false else asFile().exists()

fun Uri.asFile(): File = File(toString())
Armagnac answered 24/5, 2020 at 1:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.