5

i am developing an app to merge the n number of videos using mp4parser.The videos which are to be merged are taken in both front camera and back camera. if i merge these videos into single , it is merging all videos fine, but the alternative videos which are taken via front camera are merged as inverted. what can i do. please any one help me.

this is my code to merge videos:

try {


String f1,f2,f3;


f1 = Environment.getExternalStorageDirectory() + "/DCIM/testvideo1.mp4";// video took via back camera


f2 = Environment.getExternalStorageDirectory() + "/DCIM/testvideo2.mp4";// video took via front camera


f3 = Environment.getExternalStorageDirectory() + "/DCIM/testvideo3.mp4";// video took via front camera


        Movie[] inMovies = new Movie[] {
          MovieCreator.build(f1),
          MovieCreator.build(f2),
          MovieCreator.build(f3) 
         };
            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()])));
            }
            BasicContainer out =  (BasicContainer) new DefaultMp4Builder().build(result);
            WritableByteChannel fc = new RandomAccessFile(
            String.format(Environment.getExternalStorageDirectory()+ "/DCIM/CombinedVideo.mp4"), "rw").getChannel();
            out.writeContainer(fc);                  
            fc.close();

        } catch (Exception e) {
            Log.d("Rvg", "exeption" + e);
            Toast.makeText(getApplicationContext(), "" + e, Toast.LENGTH_LONG)
                    .show();
        }
Arslan Anwar
  • 18,746
  • 19
  • 76
  • 105
Grace Venkat
  • 189
  • 1
  • 10

1 Answers1

0

Explanation:

This is caused due to an orientation issue explained here. In short, it means that because of the different rotation metadata of videos recorded with the front camera and the back camera, when merging the videos using mp4parser every second video will be upside down.

How to solve this issue:

I have managed to solve this issue in a two different ways.

1. Re-encoding video - using FFmpeg I managed to merge the videos in the correct orientation.

How to use, and my suggestion for using FFmpeg in Android

Cons:

The main problem with this solution is that it requires re-encoding the videos into the merged video, and this process can take a pretty long time.

Also this method is lossy due to the re-encoding

Pro:

This method can work regardless even if the videos resolution, frame rate, and bitrate are not the same.

2. Using mp4parser/demuxer method - in order to use mp4parser or FFmpeg demuxer method to merge the videos in the correct orientation, the videos must be provided with the same orientation - or with no rotation metadata. Using CameraView takeVideoSnapshot() allows to record a video with no rotation metadata, so when merging it with mp4parser it is oriented correctly.

Con:

The main problem with this solution is that for the output to be with correct orientation and not fail, the input videos must have the same resolution, frame rate, bit rate and also rotation metadata(for correct orientation).

Pros:

This method requires no re-encoding so it is very very fast.

Also it is lossless.

Raz Leshem
  • 191
  • 1
  • 7