Im attempting to rotate a video that is in landscape into portrait using MP4Parser (or any other method if you know of one) currently playing with the TrackHeaderBox but unable to get the orientation to change at all, has anyone used this before that can spot the mistake I may of made? any help will go 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);
Embedding Information about Rotation in the MP4
Are you really changing the orientation of the video track? If you change the orientation of the audio track you will not see any change.
From my experience it's easier to change the orientation of the whole file (1.0.4.2 API 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());
alternatively if you want to change the orientation directly without going via a 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());
The file result.mp4
is now rotated by 180 degrees as you can verify by playing back the file in a desktop player such as QuickTime or VLC.
Typical Problems on Android
When you playback the video on Android with the help of the VideoView you might notice that the matrix is not taken into account. I'm not entirely sure if this is done on purpose or not but the workaround is to use a TextureView
that applies the transformation.
In order to do so you have to
- extract the matrices from the MovieHeaderBox at /moov/mvhd and from MediaHeaderBox at /moov/trak[0, 1, 2, 3]/tkhd (depending on which trak contains the video).
- Combine both matrices via matrix multiplication.
- Call
setScaleX
, setScaleY
, setPivotX
,setPivotY
and setRotation
with values according to the resulting matrix from the step before.