Reorient MP4Parser Videos

I am trying to rotate a landscape video in portrait using MP4Parser (or any other method, if you know one) that currently plays with TrackHeaderBox but cannot change the orientation at all, has anyone used this before can notice the error i can make? any help goes a long way thanks

IsoFile out = new DefaultMp4Builder().build(result); // test double[] m = null; m = new double[] { 0.0, 1.0, 0.0, -1.0, 0.0, 0.0, 0.0, 0.0, 1.0 }; TrackBox tb = out.getMovieBox().getBoxes(TrackBox.class).get(0); TrackHeaderBox box = tb.getTrackHeaderBox(); box.setMatrix(m); 
+3
source share
1 answer

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.
0
source

Source: https://habr.com/ru/post/955280/


All Articles