How to determine video width and height on Android
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();
Related videos on Youtube
mao
Student at Perm State University. Software Developer at ICS.
Updated on November 14, 2021Comments
-
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 about 12 yearsSorry, I forgot to write, that I need API verion 7, so there is no MediaMetadataRetriever
-
Jon Shemitz about 12 yearsAt 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) whenRESOLUTION
is not available. -
Lennert almost 11 yearsThanks 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 about 7 years@TigranSarkisian does the app crash in android 6.0?
-
Emre Aydin almost 7 yearsIt works after API 10. But it throws
IllegalArgumentException
if path is invalid or file doesn't exist. -
Vlad almost 6 yearsWorks on android 7.1
-
Kishan Viramgama almost 5 yearswhen 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 almost 5 yearswhen 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 over 4 yearsOnly thing that works for me - that MediaMetadataRetriever doesn't seem to know much about video sizes...
-
Tom3652 about 4 yearsShould use the
parseInt()
method instead ofvalueOf
. Anyway you saved my day ! -
Carlos López Marí over 2 yearsretriever.setDataSource doesn't work