Attachment Rotation Information in MP4
Are you really changing the orientation of the video track? If you change the orientation of the soundtrack, you will not see any changes.
From my experience it is easier to change the orientation of the whole file (version 1.0.4.2 version):
Movie result = MovieCreator.build("input.mp4"); // do something with the file Container out = new DefaultMp4Builder().build(result); MovieHeaderBox mvhd = Path.getPath(out, "moov/mvhd"); mvhd.setMatrix(Matrix.ROTATE_180); out.writeContainer(new FileOutputStream("result.mp4").getChannel());
if you want to directly change the orientation without going through the Movie object:
IsoFile isoFile = new IsoFile("video.mp4"); MovieHeaderBox mvhd = Path.getPath(isoFile, "/moov/mvhd"); mvhd.setMatrix(Matrix.ROTATE_180); isoFile.writeContainer(new FileOutputStream("result.mp4").getChannel());
Now the result.mp4 file rotates 180 degrees, as you can check by playing the file in a desktop player such as QuickTime or VLC.
Common problems on Android
When you play videos on Android using VideoView, you may notice that the matrix is โโnot counted. I'm not quite sure whether this was done on purpose or not, but the workaround is to use a TextureView that applies the transform.
For this you need
- extract the matrices from the MovieHeaderBox in / moov / mvhd and from the MediaHeaderBox in / moov / trak [0, 1, 2, 3] / tkhd (depending on which track the video contains).
- Combine both matrices using matrix multiplication .
- Call
setScaleX , setScaleY , setPivotX , setPivotY and setRotation with the values โโaccording to the resulting matrix from the previous step.
source share