How to determine video width and height on Android

34,267

Solution 1

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();

Solution 2

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");

Solution 3

This worked for me

videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(final MediaPlayer mp) {
                int width = mp.getVideoWidth();
                int height = mp.getVideoHeight();
            }
        });

Solution 4

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();
        }

Solution 5

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();
Share:
34,267

Related videos on Youtube

mao
Author by

mao

Student at Perm State University. Software Developer at ICS.

Updated on November 14, 2021

Comments

  • mao
    mao over 2 years

    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

  • mao
    mao about 12 years
    Sorry, I forgot to write, that I need API verion 7, so there is no MediaMetadataRetriever
  • Jon Shemitz
    Jon Shemitz about 12 years
    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.
  • Lennert
    Lennert almost 11 years
    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?
  • Karthik K M
    Karthik K M about 7 years
    @TigranSarkisian does the app crash in android 6.0?
  • Emre Aydin
    Emre Aydin almost 7 years
    It works after API 10. But it throws IllegalArgumentException if path is invalid or file doesn't exist.
  • Vlad
    Vlad almost 6 years
    Works on android 7.1
  • Kishan Viramgama
    Kishan Viramgama almost 5 years
    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.
  • Kishan Viramgama
    Kishan Viramgama almost 5 years
    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.
  • slott
    slott over 4 years
    Only thing that works for me - that MediaMetadataRetriever doesn't seem to know much about video sizes...
  • Tom3652
    Tom3652 about 4 years
    Should use the parseInt() method instead of valueOf. Anyway you saved my day !
  • Carlos López Marí
    Carlos López Marí over 2 years
    retriever.setDataSource doesn't work