mp4parser icon indicating copy to clipboard operation
mp4parser copied to clipboard

Fatal Exception: java.lang.OutOfMemoryError

Open yesusok opened this issue 5 years ago • 6 comments

MoveCreator.build(sourceFile); When invoked, the following problem occurs.

This happens when you trim an mp4 file recorded with the du recorder app. I do not know the reason.

i used isoparser 1.1.22. - device vivo 1716 (android os 8.1)

2018-12-17 11:20:53.996 14546-15174/? E/UncaughtException: java.lang.OutOfMemoryError: Failed to allocate a 2065855864 byte allocation with 12582912 free bytes and 508MB until OOM, max allowed footprint 16479016, growth limit 536870912 at java.nio.HeapByteBuffer.(HeapByteBuffer.java:54) at java.nio.HeapByteBuffer.(HeapByteBuffer.java:49) at java.nio.ByteBuffer.allocate(ByteBuffer.java:261) at com.googlecode.mp4parser.AbstractBox.a(AbstractBox.java:110) at com.coremedia.iso.AbstractBoxParser.a(AbstractBoxParser.java:107) at com.googlecode.mp4parser.BasicContainer.akY(BasicContainer.java:185) at com.googlecode.mp4parser.BasicContainer.hasNext(BasicContainer.java:161) at com.googlecode.mp4parser.util.LazyList.aqe(LazyList.java:30) at com.googlecode.mp4parser.util.LazyList.size(LazyList.java:77) at com.googlecode.mp4parser.BasicContainer.m(BasicContainer.java:80) at com.googlecode.mp4parser.authoring.samples.DefaultMp4SampleList.(DefaultMp4SampleList.java:36) at com.coremedia.iso.boxes.mdat.SampleList.(SampleList.java:33) at com.googlecode.mp4parser.authoring.Mp4TrackImpl.(Mp4TrackImpl.java:64) at com.googlecode.mp4parser.authoring.container.mp4.MovieCreator.a(MovieCreator.java:57) at com.googlecode.mp4parser.authoring.container.mp4.MovieCreator.lO(MovieCreator.java:38)

yesusok avatar Dec 17 '18 02:12 yesusok

Introduced in 1.1.8, resolved in 1.9.37: https://github.com/sannies/mp4parser/pull/288

starfall-system avatar Jan 23 '19 17:01 starfall-system

still got the issue...

Device: Xiaomi Redmi Note 5 (4 / 64), OS Android Oreo 8.1

Context: Trying to use trim example - ShortenExample.java

lib version: implementation 'com.github.sannies.mp4parser:examples:mp4parser-project-1.9.37'

src video info: file size: 50 mb length: 20 sec width: 1920 (btw this is vertical video) height: 1080 bitrate: 19826 kbps frames: 30 frames/sec audio: 94kbps, channels 2, sample rate - 48 kHz

Error:

java.lang.OutOfMemoryError: Failed to allocate a 747345552 byte allocation with 8710584 free bytes and 247MB until OOM, max allowed footprint 17421168, growth limit 268435456
        at java.nio.HeapByteBuffer.<init>(HeapByteBuffer.java:54)
        at java.nio.HeapByteBuffer.<init>(HeapByteBuffer.java:49)
        at java.nio.ByteBuffer.allocate(ByteBuffer.java:261)
        at org.mp4parser.support.AbstractBox.parse(AbstractBox.java:96)
        at org.mp4parser.AbstractBoxParser.parseBox(AbstractBoxParser.java:115)
        at org.mp4parser.BasicContainer.initContainer(BasicContainer.java:107)
        at org.mp4parser.IsoFile.<init>(IsoFile.java:57)
        at org.mp4parser.IsoFile.<init>(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)

explicit code:

private void genVideoUsingMp4Parser(@NonNull File src, @NonNull File dst,
                                               long startMs, long endMs, @NonNull OnTrimVideoListener callback) throws IOException {
        //Movie movie = new MovieCreator().build(new RandomAccessFile("/home/sannies/suckerpunch-distantplanet_h1080p/suckerpunch-distantplanet_h1080p.mov", "r").getChannel());
        Movie movie = MovieCreator.build(src.getAbsolutePath());

        List<Track> tracks = movie.getTracks();
        movie.setTracks(new LinkedList<Track>());
        // remove all tracks we will create new tracks from the old

        double startTime1 = 3;
        double endTime1 = 7;
        double startTime2 = 10;
        double endTime2 = 15;

        boolean timeCorrected = false;

        // Here we try to find a track that has sync samples. Since we can only start decoding
        // at such a sample we SHOULD make sure that the start of the new fragment is exactly
        // such a frame
        for (Track track : tracks) {
            if (track.getSyncSamples() != null && track.getSyncSamples().length > 0) {
                if (timeCorrected) {
                    // This exception here could be a false positive in case we have multiple tracks
                    // with sync samples at exactly the same positions. E.g. a single movie containing
                    // multiple qualities of the same video (Microsoft Smooth Streaming file)

                    throw new RuntimeException("The startTime has already been corrected by another track with SyncSample. Not Supported.");
                }
                startTime1 = correctTimeToSyncSample(track, startTime1, false);
                endTime1 = correctTimeToSyncSample(track, endTime1, true);
                startTime2 = correctTimeToSyncSample(track, startTime2, false);
                endTime2 = correctTimeToSyncSample(track, endTime2, true);
                timeCorrected = true;
            }
        }

        for (Track track : tracks) {
            long currentSample = 0;
            double currentTime = 0;
            double lastTime = -1;
            long startSample1 = -1;
            long endSample1 = -1;
            long startSample2 = -1;
            long endSample2 = -1;

            for (int i = 0; i < track.getSampleDurations().length; i++) {
                long delta = track.getSampleDurations()[i];


                if (currentTime > lastTime && currentTime <= startTime1) {
                    // current sample is still before the new starttime
                    startSample1 = currentSample;
                }
                if (currentTime > lastTime && currentTime <= endTime1) {
                    // current sample is after the new start time and still before the new endtime
                    endSample1 = currentSample;
                }
                if (currentTime > lastTime && currentTime <= startTime2) {
                    // current sample is still before the new starttime
                    startSample2 = currentSample;
                }
                if (currentTime > lastTime && currentTime <= endTime2) {
                    // current sample is after the new start time and still before the new endtime
                    endSample2 = currentSample;
                }
                lastTime = currentTime;
                currentTime += (double) delta / (double) track.getTrackMetaData().getTimescale();
                currentSample++;
            }
            movie.addTrack(new AppendTrack(new ClippedTrack(track, startSample1, endSample1), new ClippedTrack(track, startSample2, endSample2)));
        }
        long start1 = System.currentTimeMillis();
        Container out = new DefaultMp4Builder().build(movie);
        long start2 = System.currentTimeMillis();
        FileOutputStream fos = new FileOutputStream(String.format("output-%f-%f--%f-%f.mp4", startTime1, endTime1, startTime2, endTime2));
        FileChannel fc = fos.getChannel();
        out.writeContainer(fc);

        fc.close();
        fos.close();
        long start3 = System.currentTimeMillis();
        System.err.println("Building IsoFile took : " + (start2 - start1) + "ms");
        System.err.println("Writing IsoFile took  : " + (start3 - start2) + "ms");
        System.err.println("Writing IsoFile speed : " + (new File(String.format("output-%f-%f--%f-%f.mp4", startTime1, endTime1, startTime2, endTime2)).length() / (start3 - start2) / 1000) + "MB/s");

        callback.onSuccess(dst);


    }

    private double correctTimeToSyncSample(Track track, double cutHere, boolean next) {
        double[] timeOfSyncSamples = new double[track.getSyncSamples().length];
        long currentSample = 0;
        double currentTime = 0;
        for (int i = 0; i < track.getSampleDurations().length; i++) {
            long delta = track.getSampleDurations()[i];

            if (Arrays.binarySearch(track.getSyncSamples(), currentSample + 1) >= 0) {
                // samples always start with 1 but we start with zero therefore +1
                timeOfSyncSamples[Arrays.binarySearch(track.getSyncSamples(), currentSample + 1)] = currentTime;
            }
            currentTime += (double) delta / (double) track.getTrackMetaData().getTimescale();
            currentSample++;

        }
        double previous = 0;
        for (double timeOfSyncSample : timeOfSyncSamples) {
            if (timeOfSyncSample > cutHere) {
                if (next) {
                    return timeOfSyncSample;
                } else {
                    return previous;
                }
            }
            previous = timeOfSyncSample;
        }
        return timeOfSyncSamples[timeOfSyncSamples.length - 1];
    }

torindev avatar Mar 13 '19 10:03 torindev

@torindev can you confirm if the new release fixed this:

implementation 'org.mp4parser:isoparser:1.9.39'
implementation 'org.mp4parser:muxer:1.9.39'

HBiSoft avatar May 15 '19 08:05 HBiSoft

@siosilvar 1.9.37 didn't fix it.

HBiSoft avatar May 26 '19 11:05 HBiSoft

Tested with 1.9.39 and still get the same issue. This issue goes back 3 years, I'm not sure this will ever be fixed.

HBiSoft avatar May 26 '19 11:05 HBiSoft

Ran into this error on some devices, Is there any solution yet?

java.lang.OutOfMemoryError: at java.nio.HeapByteBuffer. (HeapByteBuffer.java:54) at java.nio.HeapByteBuffer. (HeapByteBuffer.java:49) at java.nio.ByteBuffer.allocate (ByteBuffer.java:261) at com.googlecode.mp4parser.AbstractBox.parse (AbstractBox.java:110) at com.coremedia.iso.AbstractBoxParser.parseBox (AbstractBoxParser.java:107) at com.googlecode.mp4parser.BasicContainer.next (BasicContainer.java:185) at com.googlecode.mp4parser.BasicContainer.hasNext (BasicContainer.java:161) at com.googlecode.mp4parser.util.LazyList.blowup (LazyList.java:30) at com.googlecode.mp4parser.util.LazyList.size (LazyList.java:77) at com.googlecode.mp4parser.BasicContainer.getBoxes (BasicContainer.java:80) at com.googlecode.mp4parser.authoring.samples.DefaultMp4SampleList. (DefaultMp4SampleList.java:36) at com.coremedia.iso.boxes.mdat.SampleList. (SampleList.java:33) at com.googlecode.mp4parser.authoring.Mp4TrackImpl. (Mp4TrackImpl.java:64) at com.googlecode.mp4parser.authoring.container.mp4.MovieCreator.build (MovieCreator.java:57) at com.googlecode.mp4parser.authoring.container.mp4.MovieCreator.build (MovieCreator.java:38)

mdmoniskhan avatar Jul 28 '20 07:07 mdmoniskhan