1

Hi I've been having a lot of trouble with this, I have 2 Files, both Mp4 format, read them into FileInputStreams, then into ByteArrayOutputStreams. I then try to append the two byte arrays by using another ByteArrayOutputStream [finalOutputStream] and write()'ing the two. Finally I use FileOutputStream to write(finalOutputStream.toByteArray()), flush, close. When i look for the video on my phone, there is a "Final" video that should have the 2 combined videos, with a size that looks like the two part's sizes added together.. but when i watch the video, it is only the second part -_- ... it is like the second part overwrites the first, but somehow the size increases?... heres some code..

    File fileOne = new File(fileLongName);
    File fileTwo = new File(mediaStorageDir.getPath() + File.separator +"VID_TUTPART_"+ (foo-1) + ".mp4");
    FileInputStream fisOne = new FileInputStream(fileOne);
    FileInputStream fisTwo = new FileInputStream(fileTwo);

    int bufferSize = 1024;
    //FileOne
    byte[] buffer = new byte[bufferSize];
    ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();

    //FileTwo
    byte[] bufferTwo = new byte[bufferSize];
    ByteArrayOutputStream byteBufferTwo = new ByteArrayOutputStream();

    int len = 0;
    //FileOne to bytebuffer
    while ((len = fisOne.read(buffer)) != -1) {
      byteBuffer.write(buffer, 0, len);
    }
    //FileTwo to bytebuffer
    while ((len = fisTwo.read(bufferTwo)) != -1) {
      byteBufferTwo.write(buffer, 0, len);
    }
    byte[] byteArrayOne = byteBuffer.toByteArray();
    byte[] byteArrayTwo = byteBuffer.toByteArray();

    ByteArrayOutputStream finalOutputStream = new ByteArrayOutputStream( );
    finalOutputStream.write( byteArrayOne );
    finalOutputStream.write( byteArrayTwo );

    int counterFileNumber = 0;
    while(new File(mediaStorageDir.getPath() + File.separator +"VID_TO_TUTFIN_"+ counterFileNumber + ".mp4").exists()){
        counterFileNumber++;
    }

    String outputFileNameString = mediaStorageDir.getPath() + File.separator +"VID_TO_TUTFIN_"+ counterFileNumber + ".mp4";
    File outputFile = new File(outputFileNameString);
    FileOutputStream fos = new FileOutputStream(outputFile);
    fos.write(finalOutputStream.toByteArray());
    fos.flush();
    fos.close();
jp093121
  • 1,752
  • 19
  • 13

2 Answers2

2

@Benoit With your help came up with this.. hope it helps someone else

    File fileOne = new File(mediaStorageDir.getPath() + File.separator +"theNameOfMyFirstVideo.mp4");
    File fileTwo = new File(mediaStorageDir.getPath() + File.separator +"theNameOfMySecondVideo.mp4");
    FileInputStream fisOne = new FileInputStream(fileOne);
    FileInputStream fisTwo = new FileInputStream(fileTwo);      

    Movie video = MovieCreator.build(Channels.newChannel(fisOne));
    Movie videoTwo = MovieCreator.build(Channels.newChannel(fisTwo));

    List<Track> videoTracks = video.getTracks();
    Track testOneVideoTrack = videoTracks.get(0);
    video.setTracks(new LinkedList<Track>());

    List<Track> videoTwoTracks = videoTwo.getTracks();
    Track testTwoVideoTrack = videoTwoTracks.get(0);

    video.addTrack(new AppendTrack(testTwoVideoTrack,testOneVideoTrack));

    int counterFileNumber = 0;
    while(new File(mediaStorageDir.getPath() + File.separator +"VID_TO_TUTFIN_"+ counterFileNumber + ".mp4").exists()){
        counterFileNumber++;
    }

    IsoFile out = new DefaultMp4Builder().build(video);
    String outputFileNameString = mediaStorageDir.getPath() + File.separator +"VID_TO_TUTFIN_"+ counterFileNumber + ".mp4";
    FileOutputStream fos = new FileOutputStream(new File(String.format(outputFileNameString)));
    out.getBox(fos.getChannel());
    fos.close();

When i was debugging, i looked at the List videoTracks and saw with the first element [0] there was something about "vide" and the element [1] had "soun" so started working with that..

jp093121
  • 1,752
  • 19
  • 13
  • Did you have any issues with the rotation of your output file? mine comes out rotated in landscape and I need it to stay in portrait – Edmund Rojas Jun 16 '13 at 01:19
  • @Edmund Rojas have you find solution to your problem,i am facing same problem. – Ravi Bhandari Oct 28 '14 at 13:10
  • @rb16 I think the rotation differs from device to device, I've seen photos/videos come out rotated on some, and come out perfect on others. – jp093121 Oct 28 '14 at 14:03
  • But my recorded video are of same rotation and when i play these are looking good.after merging output video is rotate to 90 degree.i have try it on more then 5 devices and facing same problem on all. – Ravi Bhandari Oct 28 '14 at 14:46
  • @rb16 I haven't worked on this in a while but I believe when I did this, I had 2 landscape videos and it came out landscape. If I were you I would record a bunch of different scenarios and find where the problem lies. Landscape + Landscape = ? | Landscape + Portait = ? | Portrait + Portrait = ? – jp093121 Oct 29 '14 at 19:12
  • When i merge two landscape video they are merge perfectly,when i merge one landscape and one portrait then while running merged video landscape part was running perfectly but portrait was rotate by -90degree.when i merge two portrait video then one part is roate to -90degree and second part is rotate to +90degree. – Ravi Bhandari Oct 30 '14 at 04:56
1

If you simply make a append the video together it won't work, you also need to rewrite the header.

I did this recently by using mp4parser

Then you can follow the sample

MovieCreator mc = new MovieCreator();
Movie video = mc.build(Channels.newChannel(AppendExample.class.getResourceAsStream("/count-video.mp4")));
Movie audio = mc.build(Channels.newChannel(AppendExample.class.getResourceAsStream("/count-english-audio.mp4")));


List<Track> videoTracks = video.getTracks();
video.setTracks(new LinkedList<Track>());

List<Track> audioTracks = audio.getTracks();


for (Track videoTrack : videoTracks) {
   video.addTrack(new AppendTrack(videoTrack, videoTrack));
}
for (Track audioTrack : audioTracks) {
   video.addTrack(new AppendTrack(audioTrack, audioTrack));
}

IsoFile out = new DefaultMp4Builder().build(video);
FileOutputStream fos = new FileOutputStream(new File(String.format("output.mp4")));
out.getBox(fos.getChannel());
fos.close();
Benoit
  • 4,549
  • 3
  • 28
  • 45
  • Thanks for the fast response! I tried that before but may have messed it up, pretty new to Android. 1.)Do i need all of the .jar files on that website? 2.)Will those libraries be trimmed during the packaging of the .apk? 3.) I have one file (mp4) that contains both video and audio, then another that contains both video and audio.. do I have to split each file into each?.. Trying again.. I'll update – jp093121 Apr 29 '13 at 03:24
  • Almost have it, when I tried your code (got isoviewer-2.0-RC-23.jar and smooth-streaming-fragmenter-1.0-RC-23.jar [[ran into another problem with [2013-04-28 23:56:09 - Dex Loader] Unable to execute dex: Multiple dex files define Lcom/coremedia/iso/AbstractBoxParser$1; ]] and figured it out here : http://stackoverflow.com/a/16271158/1967928 with my answer.. :) ] changed out audio for videoTwo..etc..etc.. now my final video is the second video appended together with.. the second video.. its the second video twice in a row.. will update more -_- – jp093121 Apr 29 '13 at 04:38
  • I only used this : com.googlecode.mp4parser isoparser 1.0-RC-22 – Benoit Apr 29 '13 at 10:00
  • Btw I had some memory issue with this lib with big videos – Benoit Apr 29 '13 at 22:09
  • It's also possible to merge video's with ffmpeg but then you need to make your own native lib, which is a second challenge. – Benoit May 02 '13 at 09:21
  • This thread might be usefull too : http://stackoverflow.com/questions/11205299/android-sdk-cut-trim-video-file – Benoit May 02 '13 at 09:22
  • @Benoit thanks for your answer.from where to download isoviewer-2.0-RC-23.jar i search but not found.Can you Please provide me link for it. – Ravi Bhandari Oct 28 '14 at 13:06
  • @Benoit I am facing issue when merge video,after merging output video rotate to 90 degree.How can i fix this issue – Ravi Bhandari Oct 28 '14 at 13:07