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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…