sannies / mp4parser

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

How to release resources #455

Open hnuuhc opened 2 years ago

hnuuhc commented 2 years 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