sannies / mp4parser

A Java API to read, write and create MP4 files
Apache License 2.0
2.75k stars 566 forks source link

IndexOutOfBoundsException when trying to mux video and audio #330

Open avanibhatnagar opened 6 years ago

avanibhatnagar commented 6 years ago

I tried to follow the MuxMe.java, my video is h264 codec and audio is aac, but this still doesn't work:

    File vid = new File(vidPath);
    File audio = new File(audioPath);
    H264TrackImpl h264Track = new H264TrackImpl(new FileDataSourceImpl(vid));
    AACTrackImpl aacTrack = new AACTrackImpl(new FileDataSourceImpl(audio));
    Movie movie = new Movie();
    movie.addTrack(h264Track);
    movie.addTrack(aacTrack);
    Mp4Builder builder = new DefaultMp4Builder();
    Container c = builder.build(movie);
    FileChannel fc = new FileOutputStream(new File("C:\\Users\\Avani Bhatnagar\\Documents\\ffmpeg trial\\ffmpeg java class\\images input\\output.mp4")).getChannel();
    c.writeContainer(fc);
    fc.close();
}

This is the error caused at this line: H264TrackImpl h264Track = new H264TrackImpl(new FileDataSourceImpl(vid));

13:28:48.710 [main] DEBUG org.mp4parser.muxer.FileDataSourceImpl - 0 269739 13:28:48.726 [main] WARN org.mp4parser.muxer.tracks.h264.H264TrackImpl - Unknown NAL unit type: 19 13:28:48.741 [main] WARN org.mp4parser.muxer.tracks.h264.H264TrackImpl - Unknown NAL unit type: 22 13:28:48.788 [main] WARN org.mp4parser.muxer.tracks.h264.H264TrackImpl - Unknown NAL unit type: 22 13:28:48.788 [main] WARN org.mp4parser.muxer.tracks.h264.H264TrackImpl - Unknown NAL unit type: 22 13:28:48.806 [main] WARN org.mp4parser.muxer.tracks.h264.H264TrackImpl - Unknown NAL unit type: 22 Exception in thread "main" java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at com.intellij.rt.execution.CommandLineWrapper.main(CommandLineWrapper.java:67) Caused by: java.lang.IndexOutOfBoundsException at java.nio.Buffer.checkIndex(Buffer.java:540) at java.nio.DirectByteBuffer.get(DirectByteBuffer.java:253) at org.mp4parser.muxer.tracks.h264.H264TrackImpl.getNalUnitHeader(H264TrackImpl.java:115) at org.mp4parser.muxer.tracks.h264.H264TrackImpl.readSamples(H264TrackImpl.java:305) at org.mp4parser.muxer.tracks.h264.H264TrackImpl.parse(H264TrackImpl.java:128) at org.mp4parser.muxer.tracks.h264.H264TrackImpl.(H264TrackImpl.java:97) at org.mp4parser.muxer.tracks.h264.H264TrackImpl.(H264TrackImpl.java:102) at org.mp4parser.muxer.tracks.h264.H264TrackImpl.(H264TrackImpl.java:106)

Am I doing something wrong?

avanibhatnagar commented 6 years ago

My vidPath had audio and video, So I did this:

Movie originalAudioAndVideo = MovieCreator.build(vidPath);
    Track nuAudio = new AACTrackImpl(new FileDataSourceImpl(audioPath));
    ArrayList nuTracks = new ArrayList();
    for(Track track: originalAudioAndVideo.getTracks()) {
        if (track.getHandler().equals("vide")) {
            nuTracks.add(track);
        }
    }
    nuTracks.add(nuAudio);
    Movie movie = new Movie();
    movie.setTracks(nuTracks);
    FragmentedMp4Builder mp4builder = new FragmentedMp4Builder();
    Container c = mp4builder.build(movie);
    FileChannel fc = new FileOutputStream(new File("C:\\Users\\Avani Bhatnagar\\Documents\\ffmpeg trial\\ffmpeg java class\\images input\\output.mp4")).getChannel();
    c.writeContainer(fc);
    fc.close();
}

I get this error: (at this line Movie originalAudioAndVideo = MovieCreator.build(vidPath);)

Exception in thread "main" java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at com.intellij.rt.execution.CommandLineWrapper.main(CommandLineWrapper.java:67) Caused by: java.lang.NoSuchMethodError: java.nio.ByteBuffer.rewind()Ljava/nio/ByteBuffer; at org.mp4parser.AbstractBoxParser.parseBox(AbstractBoxParser.java:56) at org.mp4parser.BasicContainer.initContainer(BasicContainer.java:107) at org.mp4parser.IsoFile.(IsoFile.java:57) at org.mp4parser.IsoFile.(IsoFile.java:52) at org.mp4parser.muxer.container.mp4.MovieCreator.build(MovieCreator.java:54) at org.mp4parser.muxer.container.mp4.MovieCreator.build(MovieCreator.java:39) at com.IAmVideoController.addAudioVideoInCorrectCodec(IAmVideoController.java:178) at com.creatics.web.IAmVideoController.main(IAmVideoController.java:78) ... 5 more