Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android MediaMetadataRetriever wrong video height and width

I want to retrieve height and width of video, I am using MediaMetadataRetriever class for this. It is working correct for most of the case, but for few case height and width are interchanged.

I think this might be happening because of orientation change.

My current code:

MediaMetadataRetriever metaRetriever = new MediaMetadataRetriever();
        metaRetriever.setDataSource(videoPath);
        videoHeight = metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT);
        videoWidth = metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH);

How can i get correct values? Thank you

like image 454
user1590595 Avatar asked Aug 25 '17 10:08

user1590595


1 Answers

I've been trying to figure this out myself for the last day or so, I eventually had to solve it experimentally.

            File file = new File(path);
            if (file.exists()) {
                MediaMetadataRetriever retriever = new MediaMetadataRetriever();
                retriever.setDataSource(file.getAbsolutePath());
                String metaRotation = retriever.extractMetadata(METADATA_KEY_VIDEO_ROTATION);
                int rotation = metaRotation == null ? 0 : Integer.parseInt(metaRotation);
                Log.i("Test", "Rotation = " + rotation);
            }

If the rotation is 90 or 270 the width and height will be transposed.

like image 80
Keith Loughnane Avatar answered Nov 03 '22 23:11

Keith Loughnane