Why doesn’t ExtractMpegFramesTest work with rotated input files?
I started decoding/encoding video for the app and it seems that BigFlake’s site is the definitive reference to the working video code. I copied the ExtractMpegFramesTest_egl14.java Go into my project and modify it to use a different file path but nothing else. It correctly extracts frames from the pre-recorded video I copied into the device. However, when trying to use it with a video recorded from the device’s camera, the frame I get is just horizontal:
After testing different recorded videos, I realized that the problem was with the rotated video. The device’s camera encodes the frames from the frame buffer and then adds a rotation label to the video. This can be extracted from the MediaFormat
object:
int rotation = format.getInteger("rotation-degrees");
Unfortunately, swapping the width/height of a saved image will not change anything, the image will still be corrupted. What must be done to the save code to properly handle rotated frames?
Solution
As source code comment pointed out by another answer implies, OpenGL The image needs to be transformed. The original Invert
Boolean value helps with sources that are not rotated or rotated 180 degrees. But the other directions (90 and 270) will show the flip image, so they must also be flipped.
Here are the changes I made to properly handle rotation:
- Read the
rotation-degrees
attribute from the source media, if available. - Modify the
CodecOutputSurface
constructor to accept numeric rotation parameters stored inthe mRotation
instance variable. - Modify the original
invert
code to be used when rotated to 0 or 180. - Use a different matrix transformation when rotated to 90 or 270.
You can get the full modified version from the following gist as well as view individual changes as described above.