Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
391 views
in Technique[技术] by (71.8m points)

java - MP4Parser change video orientation

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);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.
  • CallsetScaleX, setScaleY, setPivotX,setPivotY and setRotation with values according to the resulting matrix from the step before.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...