Rotate video with Mp4parser
Asked Answered
R

1

7

I need to rotate a video to adjust some of my needs. I'll explain the details on the following list.

I'm creating a Vine like app. I have to record video segments and then merge all the parts into just one file. I'm doing this without issue on an Android app using mp4 parser library with last version 1.0-RC-26 using the example provided on their website: here

The append video example works fine if all the videos have the same orientation but I discovered some issues recording video from the front camera so the quick solution was to set the video orientation recording on 270. The bad part on this solution is that the segment with this orientation appear with the wrong orientation on the merged video.

My possible solution to this is to rotate the video to apply what I need in different situations but I'm not having a working example with my code. Searching the internet I found solutions like this one here. The problem with this code is that is not compatible with the last version (It gives an compilation error) . I tried too to understand the logic of the library but I'm not having results. For example I experimented using the setMatrix instruction on the Movie object but It simply don't work.

public static void mergeVideo(int SegmentNumber) throws Exception {

    Log.d("PM", "Merge process started");
     Movie[] inMovies = new Movie[SegmentNumber]   ;
     //long[] Matrix = new long[SegmentNumber];

     for (int i = 1 ; i <= SegmentNumber; i++){
         File file =  new File(getCompleteFilePath(i));
         if (file.exists()){
             FileInputStream fis = new FileInputStream(getCompleteFilePath(i));
             //Set rotation I tried to experiment with this instruction but is not working
             inMovies [i-1].setMatrix(Matrix.ROTATE_90);
             inMovies [i-1] = MovieCreator.build(fis.getChannel());

             Log.d("PM", "Video " + i  + " merged" );
         }

         //fis.close();
     }


        List<Track> videoTracks = new LinkedList<Track>();
        List<Track> audioTracks = new LinkedList<Track>();

        for (Movie m : inMovies) {
            for (Track t : m.getTracks()) {
                if (t.getHandler().equals("soun")) {
                    audioTracks.add(t);
                }
                if (t.getHandler().equals("vide")) {
                    videoTracks.add(t);
                }
            }
        }

        Movie result = new Movie();

        if (audioTracks.size() > 0) {
            result.addTrack(new AppendTrack(audioTracks.toArray(new Track[audioTracks.size()])));
        }
        if (videoTracks.size() > 0) {
            result.addTrack(new AppendTrack(videoTracks.toArray(new Track[videoTracks.size()])));
        }

        Container out = new DefaultMp4Builder().build(result);

        //out.getMovieBox().getMovieHeaderBox().setMatrix(Matrix.ROTATE_180); //set orientation, default merged video have wrong orientation
        // Create a media file name
        //
        String filename =  getCompleteMergedVideoFilePath()  ;

        FileChannel fc = new RandomAccessFile(String.format(filename), "rw").getChannel();
        out.writeContainer(fc);
        fc.close();


        //don't leave until the file is on his place
        File file = new File (filename);
        do {
            if (! file.exists()){
                Log.d("PM", "Result file not ready");
            }
       } while (! file.exists() );
       //
        Log.d("PM", "Merge process finished");
}

Have someone rotated video with the very last version of Mp4 parser? English is not my native language so I apologize any grammar error.

Rossi answered 4/10, 2013 at 20:18 Comment(0)
I
4
for (int i = 1; i <= SegmentNumber; i++) {

    IsoFile isoFile = new IsoFile(getCompleteFilePath(i));
    Movie m = new Movie();

    List<TrackBox> trackBoxes = isoFile.getMovieBox().getBoxes(
    TrackBox.class);

    for (TrackBox trackBox : trackBoxes) {
        trackBox.getTrackHeaderBox().setMatrix(Matrix.ROTATE_90);
        m.addTrack(new Mp4TrackImpl(trackBox));
    }

    inMovies[i - 1] = m;
}

This is what I did to rotate a video.

Ichthyolite answered 15/10, 2013 at 23:19 Comment(9)
Somehow the isoFile.getMovieBox() always returns null for me, is it related maybe to the file format? Iso-type wise..?Dele
It works! Thank you Kelly! This is the one and definitive answer.Rossi
Hi, I got success to rotate but there are some problem, If I am trying to rotate first Movie to ROTATE_90 and second Movie to ROTATE_270 then ...second Movie is not ROTATE_270 but its rotote to ROTATE_90. Have you any solution for that...?Respectable
Facing same issue as mention on this link: #23765389Respectable
I have a video which was not created using a mp4parser, I am trying to use this just to rotate the video. And I am lost in translation !! Any ideas on how to implement it with normal video ?Scheme
Not Working.Plz helpJacquesjacquet
Same issue video rotate front and back camera not working.Plz helpJacquesjacquet
Same issue here with front and back camera. The second file is always upside down. Anyone?Lanielanier
What is IsoFile in this code? pls help with link if possible. ThanksBriarroot

© 2022 - 2024 — McMap. All rights reserved.