21

I want to make my custom media player and requires orientation info of video (for detecting it is recorded from front or back camera). for jpeg images i can use ExifInterface.TAG_ORIENTATION but for video how i can find this information.

I tried to grab frame from video file and convert it into jpeg but again it always provides orientation 0 in all cases.

Please help me. Thanks in advance.

Vasily Kabunov
  • 6,511
  • 13
  • 49
  • 53
ultimate
  • 717
  • 2
  • 10
  • 20

3 Answers3

23

Api level 17 later, we can extract the orientation of video: MediaMetadataRetriever

MediaMetadataRetriever m = new MediaMetadataRetriever();

m.setDataSource(path);
Bitmap thumbnail = m.getFrameAtTime();
//
if (Build.VERSION.SDK_INT >= 17) {
    String s = m.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION);

    Log.e("Rotation", s);
}
Ramesh Akula
  • 5,720
  • 4
  • 43
  • 67
  • 2
    I tried using `String height = m.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT); String width = m.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH);` but all my videos are coming out the same size, whether they are in landscape or portrait. So this answer is very good. We must base the orientation check on the rotation. Thank you! – Azurespot Apr 23 '15 at 07:20
  • 2
    for me s is coming 0 any solution? – Narendra Sep 08 '16 at 05:40
  • 1
    0 is the rotation of your video, it can be `0`, `90`, `180` or `270`. People using this should know that the rotation tag might not always be available. – HB. Nov 27 '19 at 07:16
3

FFmpegMediaMetadataRetriever can do this and it works with API 7+:

FFmpegMediaMetadataRetriever fmmr = new FFmpegMediaMetadataRetriever();
fmmr.setDataSource(path);
String rotation = fmmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION);
Log.e("Rotation", rotation);
fmmr.release();
William Seemann
  • 3,440
  • 10
  • 44
  • 78
  • I know this is a few years old, but for what it's worth - the FFmpegMediaMetadataRetriever library is massive - unless using build flavors, this will add about 18MB to your app. Unless you absolutely need the orientation of a video on API 16 or less, I would recommend not using this. – Kevin Z Jul 05 '16 at 20:20
  • The library also offers prebuilt AAR's for each architecture, you can make individual APK's. – William Seemann Jul 05 '16 at 20:25
  • @WilliamSeemann It would be great if your library could provide the correct width and height. – HB. Jan 16 '20 at 07:45
2

After too much effort i came to know that media player provides height and width of video file from which we can fin out rotation of video.

MediaPlayer mp = new MediaPlayer();
  try {
      mp.setDataSource(viewSource);
      mp.prepare();
      mp.setOnVideoSizeChangedListener(new OnVideoSizeChangedListener() {
          @Override
          public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {

              int orientation = -1;

              if(width < height){
                  orientation = 0;
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);}
              else{
                  orientation = 1;
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);}

          }
      });
  } catch (IllegalArgumentException e) {
      e.printStackTrace();
  } catch (SecurityException e) {
      e.printStackTrace();
  } catch (IllegalStateException e) {
      e.printStackTrace();
  } catch (IOException e) {
      e.printStackTrace();
  }
ultimate
  • 717
  • 2
  • 10
  • 20