How can I ensure correct playback orientation of recorded video?

感情迁移 提交于 2019-12-06 05:37:06

问题


I'm using the MediaStore.ACTION_VIDEO_CAPTURE intent to capture video and later play it back using a VideoView. I would like to know the orientation of the video that was captured.

I don't want to use the orientation at the time of the intent call because the user may rotate the device prior to hitting the shutter button. I also don't want to implement my own custom video capture.

Is the orientation of the video stored in the saved file and/or returned in the intent result?


回答1:


Is the orientation of the video stored in the saved file and/or returned in the intent result?

The AOSP VideoCamera activity does supply the rotation value of the camera device to the MediaRecorder.setOrientationHint() method. Here's an excerpt of the code in the VideoCamera.initializeRecorder() code related to this:

    // See android.hardware.Camera.Parameters.setRotation for
    // documentation.
    // Note that mOrientation here is the device orientation, which is 
    // the opposite of what getWindowManager().getDefaultDisplay().getRotation() 
    // would return, which is the orientation the graphics need to rotate 
    // in order to render correctly.
    int rotation = 0;
    if (mOrientation != OrientationEventListener.ORIENTATION_UNKNOWN) {
        CameraInfo info = CameraHolder.instance().getCameraInfo()[mCameraId];
        if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
            rotation = (info.orientation - mOrientation + 360) % 360;
        } else {  // back-facing camera
            rotation = (info.orientation + mOrientation) % 360;
        }
    }
    mMediaRecorder.setOrientationHint(rotation);

Here's the documentation for setOrientationHint() explaining the storage of the information:

Sets the orientation hint for output video playback. This method should be called before prepare(). This method will not trigger the source video frame to rotate during video recording, but to add a composition matrix containing the rotation angle in the output video if the output format is OutputFormat.THREE_GPP or OutputFormat.MPEG_4 so that a video player can choose the proper orientation for playback. Note that some video players may choose to ignore the compostion matrix in a video during playback.

Since you are using the framework's VideoView widget to playback the video, it should already handle the information in the composition matrix correctly and you should only need to compare the width and height of the video to decide whether to set a landscape or portrait orientation for your playback activity. One simple way to do this would be to just call ThumbnailUtils.createVideoThumbnail() (which internally uses MediaMetaDataRetriever) and check the resolution of the returned bitmap.




回答2:


Similar to how al. suggested in his answer, I use the MediaMetadataRetriever but with the METADATA_KEY_VIDEO_ROTATION key:

MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
mediaMetadataRetriever.setDataSource( absoluteVideoFilePath );
String orientation = mediaMetadataRetriever.extractMetadata(
         MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION );

I checked capturing video with all 4 different orientations of the front camera, and the metadata is always different. Note: METADATA_KEY_VIDEO_ROTATION needs API Level 17 or above.




回答3:


You can use MediaMetadataRetriever to get the width and height of your video and figure out the orientation prior to loading it.



来源:https://stackoverflow.com/questions/12340069/how-can-i-ensure-correct-playback-orientation-of-recorded-video

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