0

I need to rotate a video by 90 degrees in my Android project. After some googling I stopped at mp4parser which can be integrated with Android. The video is stored on the device and will be saved there also (not sure if with a different name, but this doesn't matter that much). Here is the code I'm using (made with isoviewer-1.0-RC-35), but I can change the library to version 2.0 if needed:

try 
{
    String inputVideoName = "path_to_a_video";

    IsoFile out =  new IsoFile(inputVideoName);
    out.getMovieBox().getMovieHeaderBox().setMatrix(Matrix.ROTATE_90);

    File outputVideoName = new File("path_to_a_saved_processed_video");
    if (!outputVideoName.exists())
        outputVideoName.createNewFile();

    FileOutputStream fos = new FileOutputStream(outputVideoName);
    out.writeContainer(fos.getChannel());
    fos.close();
    out.close();
} 
catch (IOException e) {
    Log.e("test", "some exception", e);
}

The code compiles, runs, saves the video but with the original rotation, so no changes done to it. What's wrong?

Alex
  • 191
  • 1
  • 10
  • 1
    The code looks OK. My guess is that the android player doesn't support rotated video and simply ignores the matrix in the movie header. Try to play the output file with VLC, mplayer or avplay (at least one of these should support the movie header transformation matrix). Better use the newest version of these players. Alternatively you can inspect the output file (either manually with a hex editor or using mp4dump or a similar tool) and check if the output file transformation matrix is what you expect it to be. – Svetlin Mladenov Mar 11 '14 at 09:52
  • Iti getting confusing. The video was made with the device in the other orientation. If I view the videos (both original and rotated) with the vlc client they are rotated, but vlc is the only player to display them rotated. – Alex Mar 11 '14 at 16:32
  • The thing is I trim this video with the ffmpeg library and after this I get the problem with the rotation integrated with the NDK. Does this mean the trim operation doesn't save the headers? I tried to rotate it with ffmpeg, but the version included of the library doesn't support rotation. Also, I couldn't manage to update the library into the project (this is my first contact with the NDK). – Alex Mar 11 '14 at 16:39

1 Answers1

0

Does this help? Also don't you think you should use Isoparser than Isoviewer for this?

package com.googlecode.mp4parser;

import com.googlecode.mp4parser.authoring.Movie;
import com.googlecode.mp4parser.authoring.builder.DefaultMp4Builder;
import com.googlecode.mp4parser.authoring.container.mp4.MovieCreator;
import com.googlecode.mp4parser.util.Matrix;

import java.io.FileOutputStream; 
import java.io.IOException;


public class Rotate {
public static void main(String[] args) throws IOException {

    String f1 = AppendExample.class.getProtectionDomain().getCodeSource().getLocation().getFile() + "/1365070268951.mp4";

    Movie inMovie = MovieCreator.build(f1);
    inMovie.setMatrix(Matrix.ROTATE_90);

    new DefaultMp4Builder().build(inMovie).writeContainer(new FileOutputStream("output.mp4").getChannel());
}
}
Ali Asheer
  • 117
  • 13