Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top