Rotate Video Using Mp4parser

I need to rotate the video to customize some of my needs. I will explain the details in the following list.

I am creating an application like Vine. I have to record video segments and then combine all the parts into only one file. I do this without problems in an Android application using the mp4 parser library with the latest version 1.0-RC-26, using the example provided on their website: here

An example of adding a video works fine if all the videos have the same orientation, but I found some problems with recording video from the front camera, so the quick solution was to set the video recording to 270. The bad part of this solution is that a segment with this orientation is displayed with incorrect orientation on the combined video.

My possible solution for this is to rotate the video to apply what I need in different situations, but I don't have a working example with my code. Searching the Internet I found solutions like this one here . The problem with this code is that it is incompatible with the latest version (it gives a compilation error). I also tried to understand the logic of the library, but I have no results. For example, I experimented using the setMatrix a Movie object, but it just doesn'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"); } 

Has anyone turned a video with the latest version of the Mp4 parser? English is not my first language, so I apologize for any grammatical error.

+6
source share
1 answer
 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 the video.

+4
source

Source: https://habr.com/ru/post/955274/


All Articles