1

I'm making a project that the single objective is to transform 2 .mp4 files in a single one. For this I'm transforming the files into 2 byte[] and making it into a single one, but when I make the BufferedOutputStream on it it only keeps the content of one file, while occupying the same space in memory as the two combine. There is the code I'm using:

public byte[] whole(){
        Array<byte[]> fs = filme();
        Array<Byte> bb = new Array<Byte>();
        for(byte[] b : fs){
            for(int i = 0; i<b.length; i++){
                bb.add(b[i]);
            }
        }
        byte[] whl = new byte[bb.size];

        for(int i = 0; i<whl.length; i++){

              whl[i] = bb.get(i);
    }

    return whl;
}

this part of the code transforms an List of byte Arrays into an single byte array. If you test and split the byte array into two files with the same length as the two initial ones, it makes the same two beginning files, exactly as they were before.

public FilmFile transform(byte[] go, String path) throws IOException{
    FileOutputStream out = new FileOutputStream(path);
    bos = new BufferedOutputStream(out);
    bos.write(go);
    FilmFile f = new FilmFile(path);
    return f;

}

this code transforms the previously mentioned byte array that combined the two files into an single mp4 file, but it's instead occupying the space of the two and only displaying onde video. Some informations, to get info about the movie i'm using JCODEC and the project is in gradle. Here's the FilmFile code: https://docs.google.com/document/d/1IHdG3gDeRmKXwRT2LJbsC5zI7K5kP4kuNAwMjCh7wII/edit?usp=sharing Thanks for your help, and sorry the bad english.

Itchydon
  • 2,572
  • 6
  • 19
  • 33
farbache
  • 21
  • 2
  • 2
    MP4 is a [complex file format](https://stackoverflow.com/questions/29565068/mp4-file-format-specification). You cannot just concatenate the files. – Andreas Aug 20 '17 at 19:55
  • And how can i concatenate two movie files into one using java? – farbache Aug 20 '17 at 19:56
  • See [Concatenate two mp4 files using ffmpeg](https://stackoverflow.com/q/7333232/5221149) – Andreas Aug 20 '17 at 19:56
  • Duplicate of [How to concat mp4 files?](https://stackoverflow.com/q/23129561/5221149) – Andreas Aug 20 '17 at 19:58
  • thanks for your help. – farbache Aug 20 '17 at 19:59
  • @farbache it'll be easier to use a tool like FFmpeg. Otherwise there's a lot of editing bytes to update the MP4 metadata. – VC.One Aug 20 '17 at 20:00
  • thanks every one, i'll try ffmpeg. – farbache Aug 20 '17 at 20:09
  • sorry, but some of you know how to use MP4Parser? – farbache Aug 20 '17 at 21:37
  • @farbache you can use `FFmpeg.exe` with Java as an external **process** (or whatever O.S file your FFmpeg app is installed as). You can try MP4Parser and ask a new question when stuck (don't expect tutorials, we'll try to help fix your code attempt). The most important thing is to understand MP4 header format (open an MP4 file with a hex editor and study the `moov` atom contents). An MP4 decoder checks metadata once only at file start, so you need info from 2nd vid's metadata to exist within 1st video's meta... – VC.One Aug 21 '17 at 09:54
  • This means editing the `STSS` atom to account for the new (2nd vid) keyframe offsets in bytes starting positions, also you must edit `STSZ` for the sample (frame) sizes in bytes length etc, there's updating `STTS` (for timestamps) and also `STCO` (for chunk offsets). – VC.One Aug 21 '17 at 10:02
  • Probably others am not thinking of (you'll know when you check an MP4 file via hex editor) and reference contents against the [**MP4 header manual**](https://developer.apple.com/library/content/documentation/QuickTime/QTFF/QTFFChap2/qtff2.html#//apple_ref/doc/uid/TP40000939-CH204-BBCBDIDH).. Also check [**MP4 - Configure Time**](https://stackoverflow.com/a/18552833/2057709) for ideas of how a decoder will seek to frames. – VC.One Aug 21 '17 at 10:11

0 Answers0