mp4parser icon indicating copy to clipboard operation
mp4parser copied to clipboard

OutOfMemory at android platform

Open liungkejin opened this issue 6 years ago • 8 comments

        Movie result = new Movie();
        result.addTrack(new AppendTrack(audioTracks.toArray(new Track[audioTracks.size()])));
        result.addTrack(new AppendTrack(videoTracks.toArray(new Track[videoTracks.size()])));

        FileChannel fc = new RandomAccessFile(mOutputFile, "rw").getChannel();

        Container out = new DefaultMp4Builder().build(result);
        // this code take 200+MB memory
        out.writeContainer(fc);
        fc.close();

The final video duration is 5 minutes, this code take 200+MB memory Please fix this problem @sannies #139

liungkejin avatar Feb 07 '18 09:02 liungkejin

Can you please confirm that the latest commit fixed this issue? - https://github.com/sannies/mp4parser/pull/288

HBiSoft avatar Oct 23 '18 04:10 HBiSoft

👍 , I will inform you as soon as I confirm it

liungkejin avatar Oct 25 '18 16:10 liungkejin

@liungkejin Thank you, so far I have not gotten any OutOfMemory exceptions in crashlytics. Please let me know when you have tested.

HBiSoft avatar Oct 25 '18 17:10 HBiSoft

Sorry for late reply ! I tested the release version 1.1.22 and the newest master branch.

Here is the test: Append 12 videos, each video size: 720x960, duration: 10s (sum: 2min), bitrate: 10Mb/s

here is the test code

public static void test() {
    ArrayList<String> files = new ArrayList<>();
    files.add("/sdcard/test1.mp4");
    files.add("/sdcard/test2.mp4");
    files.add("/sdcard/test3.mp4");
    files.add("/sdcard/test4.mp4");
    files.add("/sdcard/test5.mp4");
    files.add("/sdcard/test6.mp4");
    files.add("/sdcard/test7.mp4");
    files.add("/sdcard/test8.mp4");
    files.add("/sdcard/test9.mp4");
    files.add("/sdcard/test10.mp4");
    files.add("/sdcard/test11.mp4");
    files.add("/sdcard/test12.mp4");

    try {
        appendVideo(files, new File("/sdcard/test_out.mp4"));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

private static boolean appendVideo(ArrayList<String> files, File outputFile) throws Exception {
    if (files.isEmpty()) {
        return false;
    }

    if (files.size() < 2) {
        File file = new File(files.get(0));
        return file.renameTo(outputFile);
    }

    LinkedList<Track> videoTracks = new LinkedList<>();
    LinkedList<Track> audioTracks = new LinkedList<>();

    for (String path : files) {
        Movie movie = MovieCreator.build(path);
        Track vTrack = null;
        Track aTrack = null;
        for (Track t : movie.getTracks()) {
            String hand = t.getHandler();
            if (hand.equals("soun")) {
                aTrack = t;
            } else if (hand.equals("vide")) {
                vTrack = t;
            }
        }

        if (vTrack != null && aTrack != null) {
            videoTracks.add(vTrack);
            audioTracks.add(aTrack);
        }
    }

    if (audioTracks.isEmpty()) {
        throw new Exception("Video invalid");
    }

    Movie result = new Movie();
    result.addTrack(new AppendTrack(audioTracks.toArray(new Track[audioTracks.size()])));
    result.addTrack(new AppendTrack(videoTracks.toArray(new Track[videoTracks.size()])));

    FileChannel fc = new RandomAccessFile(outputFile, "rw").getChannel();

    Log.i("Mp4Parser", "Append video: tracks: " + videoTracks.size());
    Container out = new DefaultMp4Builder().build(result);
    out.writeContainer(fc);
    fc.close();

    System.gc();

    return outputFile.exists();
}

Test devices: XiaoMi 8, Android 8.1.0, RAM: 6GB release version: com.googlecode.mp4parser:isoparser:1.1.22 result: image

newest master branch : result: image

Test devices: HUAWEI P9, Android 6.0, RAM 3GB release version: com.googlecode.mp4parser:isoparser:1.1.22 result: image after force GC: image

newest master branch : result: image on this device, the memory take 100MB, and more stable than release version, but after force GC, the memory no change!

liungkejin avatar Dec 08 '18 09:12 liungkejin

@liungkejin did you ever resolve this issue? I still getting outofmemory crashes.

HBiSoft avatar May 25 '19 05:05 HBiSoft

@liungkejin did you ever resolve this issue? I still getting outofmemory crashes.

Sorry, I didn't resolve this issue, and I'm use MP4v2 library to splice mp4 files now :-)

liungkejin avatar Jun 04 '19 06:06 liungkejin

@liungkejin Did you compile MP4v2 or is there a library I don't know of?

HBiSoft avatar Jun 10 '19 10:06 HBiSoft

@liungkejin Did you compile MP4v2 or is there a library I don't know of?

Yes, I compiled MP4v2。 Bento4 is another library, and i think it is better than MP4v2. But Bento4 is available under two different licenses! so I'm using MP4v2 in my application!

liungkejin avatar Jun 20 '19 08:06 liungkejin