Creating movie from files by adding track using com.googlecode.mp4parser with with memory limitations?
Asked Answered
C

0

5

I have a audio recording in multiple files. I am creating one continues audio file using com.googlecode.mp4parser:isoparser:1.0.2 library.

Below is my code :

String mediaKey = isAudio ? "soun" : "vide";
                List<Movie> listMovies = new ArrayList<>();
                for (String filename : sourceFiles) {
                    listMovies.add(MovieCreator.build(filename));
                }
                List<Track> listTracks = new LinkedList<>();
                for (Movie movie : listMovies) {
                    for (Track track : movie.getTracks()) {
                        if (track.getHandler().equals(mediaKey)) {
                            listTracks.add(track);
                        }
                    }
                }
                Movie outputMovie = new Movie();
                if (!listTracks.isEmpty()) {
                    outputMovie.addTrack(new AppendTrack(listTracks.toArray(new Track[listTracks.size()])));
                }
                Container container = new DefaultMp4Builder().build(outputMovie);
FileChannel fileChannel = new RandomAccessFile(String.format(targetFile), "rw").getChannel();
                container.writeContainer(fileChannel);
                fileChannel.close();

Above code runs on Android phone. As its mobile environment there are memory limitations per application.

Problem with above code is when I load Movie from file and create a track list for small files its working fine. But as file size grows the operation starts to become non-responsive. It takes lot of memory. How can I make it memory efficient. Is their any way of doing this operations in small streams as we do in case of file copy operations in Java ?

Update : For recording audio in files, I am using android MediaRecorder for this operations with Output format as MPEG_4 and Audio encoder as AAC

mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
Calamint answered 30/10, 2017 at 6:55 Comment(6)
What is the recorded audio file format?Adam
have you tried to write audio file data in sequential on fileoutputstream? like gist.github.com/adneerav/98dd82ebefbe1b06619bfc5c8cec09b0Adam
you need call this in background threadAdam
@Adam It is happening on background thread runs on intent service problem is memory allocation with large files.Calamint
gist.github.com/adneerav/98dd82ebefbe1b06619bfc5c8cec09b0 using fileoutput stream ?Adam
@Adam your approach doesn't work it only plays first file the remaining files are written to output file but don't play. Thanks for suggestion.Calamint

© 2022 - 2024 — McMap. All rights reserved.