3

Is there any Open Source MP4 muxer/writer java project(source written in java, no native code) available?

I have H.264 & AAC raw/elementary streams stored seperate files. I want to mux them and get the .mp4 video file containing both Audio & Video.

Thanks in advance for your valuable suggestions.

mrsatish
  • 429
  • 2
  • 7
  • 15

2 Answers2

3

Use the isoparser library it is as far as I know the only pure Java tool that can mux AAC and H264. It also provides an example for precisely the use case you are describing.

Full disclosure: I am the creator of the library.

 AACTrackImpl audio= new AACTrackImpl(new FileInputStream("sample.aac").getChannel());
 H264TrackImpl video = new H264TrackImpl(new FileInputStream("sample.h264"));
 Movie m = new Movie();
 m.addTrack(video);
 m.addTrack(audio);

 IsoFile out = new DefaultMp4Builder().build(m);
 FileOutputStream fos = new FileOutputStream(new File("output.mp4"));
 out.getBox(fos.getChannel());
 fos.close();
Sebastian Annies
  • 2,438
  • 1
  • 20
  • 38
  • Thanks a lot. I tried using it for android but faced an issue, which I have posted on google discussion groups (https://groups.google.com/forum/?hl=en&fromgroups=#!topic/mp4parser-discussion/a9RbWVs5Kgs). – mrsatish Jan 29 '13 at 05:08
  • Sebastian Annies, your project will be highly useful for Android4.1+ as no mp4 filewriter APIs are present for raw media encoders supported by Jellibean onwards. (http://stackoverflow.com/questions/14493627/mp4-filewriter-in-android-4-1) ... – mrsatish Jan 29 '13 at 06:01
1

Previous sample from isoparser cannot be compiled with latest sources. I`m using folliwing code to mux row h264(from android MediaCodec) into .mp4 container

    H264TrackImpl video;
    try {
        video = new H264TrackImpl(new FileDataSourceImpl("/home/aod/tmp/rec_1392309584754.h264"), "eng", 9, 1);
        Movie m = new Movie();
        m.addTrack(video);

        BasicContainer out = (BasicContainer) new DefaultMp4Builder().build(m);
        FileOutputStream fos = new FileOutputStream(new File("/home/aod/tmp/output.mp4"));
        out.writeContainer(fos.getChannel());
        fos.flush();                        
        fos.close();
    } catch ( IOException e) {
        e.printStackTrace();
    }
Tolik Odukha
  • 815
  • 1
  • 8
  • 12
  • I used the same method, but the problem is that h264 plays too fast. And after muxing it into mp4, it still plays too fast. Have you encountered this problem? – Nazar Merza Feb 13 '14 at 19:42
  • I did. http://stackoverflow.com/questions/38738890/what-governs-playback-speed-when-encoding-with-androids-mediacodec-mp4parser – FuzzyAmi Aug 03 '16 at 09:19