Detect orientation of a recorded video in android

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-18 02:41:29

问题


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.


回答1:


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



回答2:


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



回答3:


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


来源:https://stackoverflow.com/questions/19487063/detect-orientation-of-a-recorded-video-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!