Mux audio and video into an MP4 file
masterTo mux raw audio and video files into an MP4 container, follow these steps:
- Wrap raw format files into
Trackobjects (e.g.,H264TrackImpl,AACTrackImpl). - Add the
Trackobjects to aMovieobject. - Use
DefaultMp4Builderto build theContainerfrom theMovie. - Write the
Containerto aFileChannel.
Supported track formats include H264, AAC, AC3, and EC3.
// 1. Wrap raw format files into Track objects
H264TrackImpl h264Track = new H264TrackImpl(new FileDataSourceImpl("video.h264"));
AACTrackImpl aacTrack = new AACTrackImpl(new FileDataSourceImpl("audio.aac"));
// 2. Add Track objects to a Movie object
Movie movie = new Movie();
movie.addTrack(h264Track);
movie.addTrack(aacTrack);
// 3. Feed the Movie object into an MP4Builder to create the container
Container mp4file = new DefaultMp4Builder().build(movie);
// 4. Write the container to an appropriate sink
FileChannel fc = new FileOutputStream(new File("output.mp4")).getChannel();
mp4file.writeContainer(fc);
fc.close();