mp4parser icon indicating copy to clipboard operation
mp4parser copied to clipboard

java.lang.IndexOutOfBoundsException: index=0, limit=0

Open viacheslavokolitiy opened this issue 9 years ago • 27 comments

Hi guys. I downloaded latest version of your library from maven central as gradle dependency. I trying to merge multiple videos into one, and got an exception from library My code

try {
                Movie movie = new Movie();
                Log.e("PATH", Integer.toString(filePaths.size()));
                for(String path : filePaths) {
                    Log.e("PATH", path);
                    H264TrackImpl track = new H264TrackImpl(new FileDataSourceImpl(path));
                    movie.addTrack(track);

                }
                Container out = new DefaultMp4Builder().build(movie);
                FileOutputStream fos = new FileOutputStream(new File("h264_output.mp4"));
                FileChannel fc = fos.getChannel();
                out.writeContainer(fc);
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

And got exception:

 Caused by: java.lang.IndexOutOfBoundsException: index=0, limit=0
            at java.nio.Buffer.checkIndex(Buffer.java:150)
            at java.nio.DirectByteBuffer.get(DirectByteBuffer.java:158)
            at com.googlecode.mp4parser.authoring.tracks.H264TrackImpl.readSamples(H264TrackImpl.java:341)
            at com.googlecode.mp4parser.authoring.tracks.H264TrackImpl.parse(H264TrackImpl.java:112)
            at com.googlecode.mp4parser.authoring.tracks.H264TrackImpl.<init>(H264TrackImpl.java:103)
            at com.myapp.activity.TestCompilationActivity.onCreate(TestCompilationActivity.java:51)
            at android.app.Activity.performCreate(Activity.java:5280)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2322)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2410)
            at android.app.ActivityThread.access$800(ActivityThread.java:155)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1331)
            at android.os.Handler.dispatchMessage(Handler.java:110)
            at android.os.Looper.loop(Looper.java:193)
            at android.app.ActivityThread.main(ActivityThread.java:5388)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:655)
            at dalvik.system.NativeStart.main(Native Method)

Any suggestions ?

viacheslavokolitiy avatar Oct 22 '14 16:10 viacheslavokolitiy

Did you ever figure this one out?

tspecht avatar Dec 14 '14 13:12 tspecht

have the same issue

sashatinkoff avatar Feb 10 '15 07:02 sashatinkoff

Has anyone figured this out ?

svpranay avatar May 24 '15 18:05 svpranay

Plz Send me example file.

pranay [email protected] schrieb am So., 24. Mai 2015 20:51:

Has anyone figured this out ?

— Reply to this email directly or view it on GitHub https://github.com/sannies/mp4parser/issues/19#issuecomment-105047482.

sannies avatar May 24 '15 20:05 sannies

This is my code to combine audio and video. Could this issue be due to the encoding of my video file ? Any pointers would be helpful.

        H264TrackImpl h264Track = new H264TrackImpl(new MemoryDataSourceImpl(videoData));
        AACTrackImpl aacTrack = new AACTrackImpl(new FileDataSourceImpl(audioFile));
        Movie movie = new Movie();
        movie.addTrack(h264Track);
        movie.addTrack(aacTrack);
        Container mp4file = new DefaultMp4Builder().build(movie);
        FileChannel fc = new FileOutputStream(new File(resultFile)).getChannel();
        mp4file.writeContainer(fc);
        fc.close();
05-24 14:55:22.810  19006-19006/com.andromeda.vidsmash W/System.err﹕ Unknown NAL unit type: 16
05-24 14:55:22.812  19006-19006/com.andromeda.vidsmash W/System.err﹕ java.lang.IndexOutOfBoundsException: index=0, limit=0
05-24 14:55:22.823  19006-19006/com.andromeda.vidsmash W/System.err﹕ at java.nio.Buffer.checkIndex(Buffer.java:150)
05-24 14:55:22.823  19006-19006/com.andromeda.vidsmash W/System.err﹕ at java.nio.ByteArrayBuffer.get(ByteArrayBuffer.java:158)
05-24 14:55:22.824  19006-19006/com.andromeda.vidsmash W/System.err﹕ at com.googlecode.mp4parser.authoring.tracks.H264TrackImpl.readSamples(H264TrackImpl.java:297)
05-24 14:55:22.824  19006-19006/com.andromeda.vidsmash W/System.err﹕ at com.googlecode.mp4parser.authoring.tracks.H264TrackImpl.parse(H264TrackImpl.java:108)
05-24 14:55:22.824  19006-19006/com.andromeda.vidsmash W/System.err﹕ at com.googlecode.mp4parser.authoring.tracks.H264TrackImpl.<init>(H264TrackImpl.java:90)
05-24 14:55:22.824  19006-19006/com.andromeda.vidsmash W/System.err﹕ at com.googlecode.mp4parser.authoring.tracks.H264TrackImpl.<init>(H264TrackImpl.java:96)
05-24 14:55:22.824  19006-19006/com.andromeda.vidsmash W/System.err﹕ at com.googlecode.mp4parser.authoring.tracks.H264TrackImpl.<init>(H264TrackImpl.java:100)

svpranay avatar May 24 '15 22:05 svpranay

Encoding could be an issue. But it's not very likely in my eyes. Did you compile my code on your own? If yes: Did you use the aspectJ compiler to add the aspects?

pranay [email protected] schrieb am Mo., 25. Mai 2015 00:00:

This is my code to combine audio and video. Could this issue be due to the encoding of my video file ? Any pointers would be helpful.

    H264TrackImpl h264Track = new H264TrackImpl(new MemoryDataSourceImpl(videoData));
    AACTrackImpl aacTrack = new AACTrackImpl(new FileDataSourceImpl(audioFile));
    Movie movie = new Movie();
    movie.addTrack(h264Track);
    movie.addTrack(aacTrack);
    Container mp4file = new DefaultMp4Builder().build(movie);
    FileChannel fc = new FileOutputStream(new File(resultFile)).getChannel();
    mp4file.writeContainer(fc);
    fc.close();

05-24 14:55:22.810 19006-19006/com.andromeda.vidsmash W/System.err﹕ Unknown NAL unit type: 16 05-24 14:55:22.812 19006-19006/com.andromeda.vidsmash W/System.err﹕ java.lang.IndexOutOfBoundsException: index=0, limit=0 05-24 14:55:22.823 19006-19006/com.andromeda.vidsmash W/System.err﹕ at java.nio.Buffer.checkIndex(Buffer.java:150) 05-24 14:55:22.823 19006-19006/com.andromeda.vidsmash W/System.err﹕ at java.nio.ByteArrayBuffer.get(ByteArrayBuffer.java:158) 05-24 14:55:22.824 19006-19006/com.andromeda.vidsmash W/System.err﹕ at com.googlecode.mp4parser.authoring.tracks.H264TrackImpl.readSamples(H264TrackImpl.java:297) 05-24 14:55:22.824 19006-19006/com.andromeda.vidsmash W/System.err﹕ at com.googlecode.mp4parser.authoring.tracks.H264TrackImpl.parse(H264TrackImpl.java:108) 05-24 14:55:22.824 19006-19006/com.andromeda.vidsmash W/System.err﹕ at com.googlecode.mp4parser.authoring.tracks.H264TrackImpl.(H264TrackImpl.java:90) 05-24 14:55:22.824 19006-19006/com.andromeda.vidsmash W/System.err﹕ at com.googlecode.mp4parser.authoring.tracks.H264TrackImpl.(H264TrackImpl.java:96) 05-24 14:55:22.824 19006-19006/com.andromeda.vidsmash W/System.err﹕ at com.googlecode.mp4parser.authoring.tracks.H264TrackImpl.(H264TrackImpl.java:100)

— Reply to this email directly or view it on GitHub https://github.com/sannies/mp4parser/issues/19#issuecomment-105064955.

sannies avatar May 25 '15 05:05 sannies

I didn't compile the code. I included it as a maven dependency. Reg. the origin of my video source, I used the below command on a mp4 video i had and i used that as input to the above code. Any pointers would be helpful to resolve this issue.

ffmpeg -i video.mp4 -c:a copy -c:v libx264 -profile:v baseline -level 1 video.mp4

svpranay avatar May 25 '15 06:05 svpranay

Hmm - ok, then it's definitively the video. I don't want to chase ghosts - so please send me an example file! My ffmpeg might be a different revision than your ffmpeg. I'm running ffmpeg on windows, you might run it on Mac (or linux or or or) . The input video might cause ffmpeg to produce different output as well.

2015-05-25 8:17 GMT+02:00 pranay [email protected]:

I didn't compile the code. I included it as a maven dependency. Reg. the origin of my video source, I used the below command on a mp4 video i had and i used that as input to the above code. Any pointers would be helpful to resolve this issue.

ffmpeg -i video.mp4 -c:a copy -c:v libx264 -profile:v baseline -level 1 video.mp4

— Reply to this email directly or view it on GitHub https://github.com/sannies/mp4parser/issues/19#issuecomment-105138456.

sannies avatar May 25 '15 06:05 sannies

This is the video.. https://www.dropbox.com/s/7uye009cuz3kuaf/darkknightnoaudio.mp4?dl=0

svpranay avatar May 25 '15 06:05 svpranay

Hi Pranay, the video you sent me is already an MP4 that contains audio and video. So basically it's the result of the code you were psoting 8 hours ago. The H264TrackImpl is used to parse raw H264 Annex B stream, the AACTrackIMpl is used for raw AAC streams. Use MovieCreator to create a Movie object from the MP4 and then select the video track by calling getTracks().get(0) if you have a video-only MP4 file!

I'm not 100% sure what you are trying to do!

2015-05-25 8:28 GMT+02:00 pranay [email protected]:

This is the video.. https://www.dropbox.com/s/7uye009cuz3kuaf/darkknightnoaudio.mp4?dl=0

— Reply to this email directly or view it on GitHub https://github.com/sannies/mp4parser/issues/19#issuecomment-105139484.

sannies avatar May 25 '15 06:05 sannies

Thank you so much for helping me out. I'll correct my code.

I'm just trying to merge a given video file (after removing the original audio) and add my custom audio to it.

svpranay avatar May 25 '15 06:05 svpranay

Hello sir, I am using MP4Parser for my project.I have one video file (audio+video) in .mp4 format and another audio file in .mp3 format . I have to merge two files (.mp4 and .mp3)such that audio of that video file should be muted and new audio should be there in place of that. Please help in this issue....I will be very thankful to you...

uditkumawat avatar May 28 '15 12:05 uditkumawat

Sir can't we use .mp3 file directly for audio merging

uditkumawat avatar May 28 '15 14:05 uditkumawat

Mp3 in MP4 containers doesn't work very well. Please try to use AAC instead. What is your actual problem. What did you already try?

udit [email protected] schrieb am Do., 28. Mai 2015 16:31:

Sir can't we use .mp3 file directly for audio merging

— Reply to this email directly or view it on GitHub https://github.com/sannies/mp4parser/issues/19#issuecomment-106356108.

sannies avatar May 28 '15 15:05 sannies

Sir I have merge both .mp4 files but now I have to merge audio.mp3 and video.mp4 file... please suggest me the solution

On Thu, May 28, 2015 at 8:42 PM, Sebastian Annies [email protected] wrote:

Mp3 in MP4 containers doesn't work very well. Please try to use AAC instead. What is your actual problem. What did you already try?

udit [email protected] schrieb am Do., 28. Mai 2015 16:31:

Sir can't we use .mp3 file directly for audio merging

— Reply to this email directly or view it on GitHub https://github.com/sannies/mp4parser/issues/19#issuecomment-106356108.

— Reply to this email directly or view it on GitHub https://github.com/sannies/mp4parser/issues/19#issuecomment-106393689.

With Regards, Udit Kumawat

Fourth year Graduate Student (B. Tech In IT + MTech)Department of Integrated Post Graduation (IPG)Indian Institute of Information Technology and Management, GwaliorMobile: +91-8989826236 in.linkedin.com/in/uditkumawat94/ http://in.linkedin.com/in/uditkumawat94/

uditkumawat avatar May 29 '15 05:05 uditkumawat

Hi, MP3 in MP4 containers is not working well. Don't do it is my advice. You might want to send the mp3 through Android's MediaCodec to get an AAC. Grüße, Sebastian

udit [email protected] schrieb am Fr., 29. Mai 2015 07:49:

Sir I have merge both .mp4 files but now I have to merge audio.mp3 and video.mp4 file... please suggest me the solution

On Thu, May 28, 2015 at 8:42 PM, Sebastian Annies < [email protected]> wrote:

Mp3 in MP4 containers doesn't work very well. Please try to use AAC instead. What is your actual problem. What did you already try?

udit [email protected] schrieb am Do., 28. Mai 2015 16:31:

Sir can't we use .mp3 file directly for audio merging

— Reply to this email directly or view it on GitHub <https://github.com/sannies/mp4parser/issues/19#issuecomment-106356108 .

— Reply to this email directly or view it on GitHub https://github.com/sannies/mp4parser/issues/19#issuecomment-106393689.

With Regards, Udit Kumawat

Fourth year Graduate Student (B. Tech In IT + MTech)Department of Integrated Post Graduation (IPG)Indian Institute of Information Technology and Management, GwaliorMobile: +91-8989826236 in.linkedin.com/in/uditkumawat94/ http://in.linkedin.com/in/uditkumawat94/

— Reply to this email directly or view it on GitHub https://github.com/sannies/mp4parser/issues/19#issuecomment-106697341.

sannies avatar May 29 '15 06:05 sannies

Hi! I need to merge video (mp4) and audio (aac), into 1 file (mp4). I did like this:

    public boolean mux(String videoFile, String audioFile, String outputFile) throws IOException {
    H264TrackImpl h264Track = new H264TrackImpl(new FileDataSourceImpl(videoFile));
    AACTrackImpl aacTrack = new AACTrackImpl(new FileDataSourceImpl(audioFile));

    Movie movie = new Movie();
    movie.addTrack(h264Track);
    movie.addTrack(aacTrack);

    Container mp4file = new DefaultMp4Builder().build(movie);

    FileChannel fc = new FileOutputStream(new File(outputFile)).getChannel();
    mp4file.writeContainer(fc);
    fc.close();

    return true;

where videoFile, audioFile and outputFile are paths like "/storage/emulated/0/DCIM/derzkayaaac.aac" etc. my multimedia files: http://dropmefiles.com/JTQlh

I got an error during execution : Caused by: java.lang.IndexOutOfBoundsException: index=0, limit=0 at java.nio.Buffer.checkIndex(Buffer.java:150) at java.nio.DirectByteBuffer.get(DirectByteBuffer.java:171) at com.googlecode.mp4parser.authoring.tracks.H264TrackImpl.readSamples(H264TrackImpl.java:297) at com.googlecode.mp4parser.authoring.tracks.H264TrackImpl.parse(H264TrackImpl.java:108) at com.googlecode.mp4parser.authoring.tracks.H264TrackImpl.(H264TrackImpl.java:90) at com.googlecode.mp4parser.authoring.tracks.H264TrackImpl.(H264TrackImpl.java:96) at com.googlecode.mp4parser.authoring.tracks.H264TrackImpl.(H264TrackImpl.java:100) at com.dev.illiakaliuzhnyi.derzkaya.Mp4ParserAudioMuxer.mux(Mp4ParserAudioMuxer.java:33) please help me out! Thank you, have a nice day!

illiaKalu avatar Jun 10 '15 15:06 illiaKalu

Hi, the H264TrackImpl is used to read raw h264 files. If the h264 is already in a MP4 container you should use the MovieCreator class to build a Movie object.You can then add the AACTrackImpl to the existing tracks and write the result to disk. BR, Sebastian

2015-06-10 17:29 GMT+02:00 illiaKalu [email protected]:

Hi! I need to merge video (mp4) and audio (aac), into 1 file (mp4). I did like this:

public boolean mux(String videoFile, String audioFile, String outputFile) throws IOException {

H264TrackImpl h264Track = new H264TrackImpl(new FileDataSourceImpl(videoFile));
AACTrackImpl aacTrack = new AACTrackImpl(new FileDataSourceImpl(audioFile));

Movie movie = new Movie();
movie.addTrack(h264Track);
movie.addTrack(aacTrack);

Container mp4file = new DefaultMp4Builder().build(movie);

FileChannel fc = new FileOutputStream(new File(outputFile)).getChannel();
mp4file.writeContainer(fc);
fc.close();

return true;

my multimedia files: http://dropmefiles.com/fT3Wr where videoFile, audioFile and outputFile are paths like "/storage/emulated/0/DCIM/derzkayaaac.aac" etc. I got an error during execution : Caused by: java.lang.IndexOutOfBoundsException: index=0, limit=0 at java.nio.Buffer.checkIndex(Buffer.java:150) at java.nio.DirectByteBuffer.get(DirectByteBuffer.java:171) at com.googlecode.mp4parser.authoring.tracks.H264TrackImpl.readSamples(H264TrackImpl.java:297) at com.googlecode.mp4parser.authoring.tracks.H264TrackImpl.parse(H264TrackImpl.java:108) at com.googlecode.mp4parser.authoring.tracks.H264TrackImpl.(H264TrackImpl.java:90) at com.googlecode.mp4parser.authoring.tracks.H264TrackImpl.(H264TrackImpl.java:96) at com.googlecode.mp4parser.authoring.tracks.H264TrackImpl.(H264TrackImpl.java:100) at com.dev.illiakaliuzhnyi.derzkaya.Mp4ParserAudioMuxer.mux(Mp4ParserAudioMuxer.java:33) please help me out! Thank you, have a nice day!

— Reply to this email directly or view it on GitHub https://github.com/sannies/mp4parser/issues/19#issuecomment-110796472.

sannies avatar Jun 13 '15 14:06 sannies

can you show it with an example ? actually i am also facing the same issue with same code, i understand that H264TrackImpl requires .h264 file format and we have mp4, still i am unable to figure out how can i remove the earlier audio from mp4 and then add my own mp3 or aac audio to that video.

amit-bst avatar Jul 13 '15 07:07 amit-bst

Movie originalAudioAndVideo = MovieCreator.build("videoAndAudio.mp4"); Track nuAudio = new AACTrackImpl( new FileDataSourceImpl("nuAudio.aac"));

List<Track> nuTracks = new ArrayList<Track>();

for (Track track : originalAudioAndVideo.getTracks() ) { if (track.getHandler().equals("vide")) { nuTracks.add(track); } } nuTracks.add(nuAudio);

Movie nuMovie = new Movie(); nuMovie.setTracks(nuTracks);

FragmentedMp4Builder mp4builder = new FragmentedMp4Builder(); Container c = mp4builder.build(nuMovie); c.writeContainer(Channels.newChannel(new FileOutputStream("out.mp4")));

2015-07-13 9:28 GMT+02:00 amit-bst [email protected]:

can you show it with an example ? actually i am also facing the same issue with same code, i understand that H264TrackImpl requires .h264 file format and we have mp4, still i am unable to figure out how can i remove the earlier audio from mp4 and then add my own mp3 or aac audio to that video.

— Reply to this email directly or view it on GitHub https://github.com/sannies/mp4parser/issues/19#issuecomment-120837391.

sannies avatar Jul 13 '15 09:07 sannies

Thanks sannies, I tried this sample with the following files but the out video is not functional it sticks sometimes and no sound is there in this video. Here is the link to file https://www.dropbox.com/sh/n7r8jn7lfxf4zzw/AADpthlihBc630x61NimejoEa?dl=0

amit-bst avatar Jul 13 '15 11:07 amit-bst

Hi, I am merging mp3 into mp4. It works fine on small length mp3 like 10 seconds, 20 seconds but when length exceeded like 53 seconds or 1:03 seconds, it gives IndexOutOfBound Exception. I think its on get(location). Can you please provide me the solution. Track audioTrack = audio.getTracks().get(0); video.addTrack(audioTrack);

Asphanyaar avatar Aug 09 '15 21:08 Asphanyaar

So there is a list of tracks and when you try to access the first element you get an IndexOutOfBoundsException. What does that tell you? Perhaps that the list doesn't contain any element?

2015-08-09 23:54 GMT+02:00 Asphanyaar [email protected]:

Hi, I am merging mp3 into mp4. It works fine on small length mp3 like 10 seconds, 20 seconds but when length exceeded like 53 seconds or 1:03 seconds, it gives IndexOutOfBound Exception. I think its on get(location). Can you please provide me the solution. Track audioTrack = audio.getTracks().get(0); video.addTrack(audioTrack);

— Reply to this email directly or view it on GitHub https://github.com/sannies/mp4parser/issues/19#issuecomment-129250827.

sannies avatar Aug 10 '15 15:08 sannies

@sannies I tried to follow your instructions in the comment from August 13, 2015. But I get an NoSuchMethod error at this line: Movie originalAudioAndVideo = MovieCreator.build("videoAndAudio.mp4");

avanibhatnagar avatar Aug 13 '18 18:08 avanibhatnagar

`String outputVideo = new File(Utils.getDefaultStorage(AudioStatusActivity.this), "video" + new Random().nextInt() + ".mp4").getAbsolutePath(); String pathtoVideo = new File(Utils.getDefaultStorage(AudioStatusActivity.this) + "/" + "Videos" + "/" + "ffaec7.mp4").getAbsolutePath();

Movie originalAudioAndVideo = MovieCreator.build(pathtoVideo); Track nuAudio = new AACTrackImpl( new FileDataSourceImpl(newAudioPath)); List nuTracks = new ArrayList();

for (Track track : originalAudioAndVideo.getTracks() ) { if (track.getHandler().equals("vide")) { nuTracks.add(track); } } nuTracks.add(nuAudio);

Movie nuMovie = new Movie(); nuMovie.setTracks(nuTracks);

FragmentedMp4Builder mp4builder = new FragmentedMp4Builder(); Container c = mp4builder.build(nuMovie); c.writeContainer(Channels.newChannel(new FileOutputStream(outputVideo)));`

still not working....

GauravCreed avatar Jan 02 '19 13:01 GauravCreed

Same issue here

talesbarretoss avatar Jun 25 '19 13:06 talesbarretoss

Hi. I'm having the same issue. While trying to convert from h264 to mp4 I get the IndexOutOfBoundsException. I know it does have something to do with the file and it's encoding, but still, ffmpeg is able to converti it succesfully (throwing some errors along the way). Is there a chance I can provide an h264 file so that you can take a look at it? Thanks in advance.

Best regards

hauptkonto avatar Oct 23 '19 20:10 hauptkonto