28

I have a video file and I want to get width and height of video. I don't want to play it, just to get size. I've tried to use MediaPlayer:

MediaPlayer mp = new MediaPlayer();
mp.setDataSource(uriString);
mp.prepare();
int width = mp.getVideoWidth();
int height = mp.getVideoHeight();

but it returns 0 for width and height, because VideoSizeChangedEvent didn't fire yet. How can I get width and height of video?

UPD: I need API version 7

8 Answers 8

46

This works in API level 10 and up:

MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource("/path/to/video.mp4");
int width = Integer.valueOf(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH));
int height = Integer.valueOf(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT));
retriever.release();
5
  • @TigranSarkisian does the app crash in android 6.0? Apr 27, 2017 at 4:07
  • 1
    It works after API 10. But it throws IllegalArgumentException if path is invalid or file doesn't exist.
    – Emre Aydin
    Jun 23, 2017 at 10:24
  • 1
    Works on android 7.1
    – Vlad
    Jul 11, 2018 at 21:38
  • 1
    Should use the parseInt() method instead of valueOf. Anyway you saved my day !
    – Tom3652
    Apr 5, 2020 at 10:36
  • retriever.setDataSource doesn't work Sep 21, 2021 at 16:20
22

In some cases, we are unable to read the metadata. To ensure that we get the width and height, it is best to create a Bitmap using MediaMetadataRetriever, then get the width and height from the created Bitmap, as shown below:

public int getVideoWidthOrHeight(File file, String widthOrHeight) {
    MediaMetadataRetriever retriever = null;
    Bitmap bmp = null;
    FileInputStream inputStream = null;
    int mWidthHeight = 0;                                                                                                                
    try {
        retriever = new  MediaMetadataRetriever();
        inputStream = new FileInputStream(file.getAbsolutePath());
        retriever.setDataSource(inputStream.getFD());
        bmp = retriever.getFrameAtTime();
        if (widthOrHeight.equals("width")){
            mWidthHeight = bmp.getWidth();
        }else {
            mWidthHeight = bmp.getHeight();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (RuntimeException e) {
        e.printStackTrace();
    } finally{
       if (retriever != null){
           retriever.release()
       }if (inputStream != null){
           inputStream.close()
       }
    }  
    return mWidthHeight;
}

You can call the above method like this:

// Get the video width
int mVideoWidth = getVideoWidthOrHeight(someFile, "width");
// Get the video height
int mVideoHeight = getVideoWidthOrHeight(someFile, "height");
2
  • Sorry, I forgot to write, that I need API verion 7, so there is no MediaMetadataRetriever
    – mao
    Jan 31, 2012 at 10:13
  • Thanks for that code, however I can't figure out how to get the appropriate string for a file in the res/raw folder of my project, any suggestions?
    – Lennert
    Jun 13, 2013 at 13:45
14

This worked for me

videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(final MediaPlayer mp) {
                int width = mp.getVideoWidth();
                int height = mp.getVideoHeight();
            }
        });
2
  • when getting height and width of videos from some of the url application gets hang and also if we cannot get height and width from video app gets hang. Jun 22, 2019 at 10:20
  • 1
    Only thing that works for me - that MediaMetadataRetriever doesn't seem to know much about video sizes...
    – slott
    Aug 7, 2019 at 9:28
7

API level 7 solution:

// get video dimensions
MediaPlayer mp = new MediaPlayer();
        try {
            mp.setDataSource(filename);
            mp.prepare();
            mp.setOnVideoSizeChangedListener(new OnVideoSizeChangedListener() {
                @Override
                public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {

                    int orient = -1;

                    if(width < height)
                        orient = 1;
                    else
                        orient = 0;

                }
            });
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
1
  • when getting height and width of videos from some of the url application gets hang and also if we cannot get height and width from video app gets hang. Jun 22, 2019 at 10:20
1

This set of utilities to work with the Size abstraction in Android.

It contains an class SizeFromVideoFile.java You can use it like this:

ISize size = new SizeFromVideoFile(videoFilePath);
size.width();
size.hight();
0
private static final Uri MOVIE_URI = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
private static final String[] MOVIE_PJT = new String[] {
        MediaStore.Video.Media._ID, MediaStore.Video.Media.DATA,
        MediaStore.Video.Media.TITLE, MediaStore.Video.Media.DATE_TAKEN,
        MediaStore.Video.Media.MIME_TYPE, MediaStore.Video.Media.DURATION,
        MediaStore.Video.Media.SIZE, MediaStore.Video.Media.RESOLUTION };

try this to query from system content provider. It will return some useful info of the Videos(local files)

1
  • At least some of the time, RESOLUTION is null. (I'm not yet sure when it's non-null, but I ran into this question looking for a way to get resolution (on Gingerbread and up, alas) when RESOLUTION is not available. Feb 15, 2012 at 1:11
0

Kotlin Version 🚨

data class VideoSize(val width: Int, val height: Int)

Extention Function

fun Uri.getVideoSize(): VideoSize {
        val retriever = MediaMetadataRetriever()
        retriever.setDataSource(appContext, this)
        val width =
            retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH)?.toInt() ?: 0
        val height =
            retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT)?.toInt() ?: 0
        retriever.release()
        return VideoSize(width, height)
    }
0

kotlin version used in my app referring to user493244's answer

     /**
     * determine video width and height
     *
     *
     * Refer to [stackoverflow](https://stackoverflow.com/questions/9077436/how-to-determine-video-width-and-height-on-android)
     * answer of  [user493244](https://stackoverflow.com/users/493244/user493244)
     *
     * @param file string with file pathname to use
     * @param widthOrHeight string with size required (width or height)
     * @return mWidthHeight int with video width or height
     */
    fun getVideoWidthOrHeight(file: String?, widthOrHeight: String): Int {
        var retriever: MediaMetadataRetriever? = null
        val bmp: Bitmap?
        //
        var mWidthHeight = 0
        try {
            retriever = MediaMetadataRetriever()
            //
            retriever.setDataSource(file)
            bmp = retriever.frameAtTime
            mWidthHeight = if (widthOrHeight == getString(R.string.width)) {
                bmp!!.width
            } else {
                bmp!!.height
            }
        } catch (e: RuntimeException) {
            e.printStackTrace()
        } finally {
            if (retriever != null) {
                try {
                    retriever.release()
                } catch (e: IOException) {
                    e.printStackTrace()
                }
            }
        }
        return mWidthHeight
    }    

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.