sannies / mp4parser

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

How to release resources #455

Open hnuuhc opened 1 year ago

hnuuhc commented 1 year ago

Code:

public static boolean audioVideoMerge(@NotNull String audio, @NotNull String video, @NotNull String output) {
        boolean success = false;
        try (FileOutputStream fos = new FileOutputStream(output); 
                            DataSource videoDataSource = new FileDataSourceImpl(video); 
                            DataSource audioDataSource = new FileDataSourceImpl(audio)) {
            Movie countVideo = MovieCreator.build(videoDataSource);
            MovieCreator.build(audioDataSource).getTracks().forEach(countVideo::addTrack);
            Container out = new FragmentedMp4Builder().build(countVideo);
            out.writeContainer(fos.getChannel());
            success = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        new File(audio).delete();
                new File(video).delete();
        return success;
}

Result: audio file exists video file exists

Is this a bug? Must use System.gc() and wait time to delete.

tiro8183 commented 1 year ago

I had the same problem