I’ve been working on this project for months. Everything works perfect, except my combineVideos method which relies on the mp4Parser library in Java, shown below.
public static void combineVideos(String videoPath1, String videoPath2, String output, boolean last) throws IOException {
Movie movie1 = MovieCreator.build(videoPath1);
Movie movie2 = MovieCreator.build(videoPath2);
//Getting the video tracks from the videoPath params
Track[] vetTrackVideo = new Track[0];
vetTrackVideo = Stream.of(movie1, movie2)
.flatMap(movie -> movie.getTracks().stream())
.filter(movie -> movie.getHandler().equals("vide"))
.collect(Collectors.toList())
.toArray(vetTrackVideo);
//Getting the audio tracks from the videoPath params
Track[] vetTrackAudio = new Track[0];
vetTrackAudio = Stream.of(movie1, movie2)
.flatMap(movie -> movie.getTracks().stream())
.filter(movie -> movie.getHandler().equals("soun"))
.collect(Collectors.toList())
.toArray(vetTrackAudio);
//Creating the output movie by setting a list with both video and audio tracks
Movie movieOutput = new Movie();
List<Track> listTracks = new ArrayList<>(List.of(new AppendTrack(vetTrackVideo), new AppendTrack(vetTrackAudio)));
movieOutput.setTracks(listTracks);
//Building the output movie and storing it into a Container
DefaultMp4Builder mp4Builder = new DefaultMp4Builder();
Container c = mp4Builder.build(movieOutput);
//Writing the output file
FileOutputStream fos = new FileOutputStream("C:\Users\willj\Downloads\" + output + ".mp4");
c.writeContainer(fos.getChannel());
fos.close();
}
This method is ALMOST perfect, but I have found in the concatenated file that there is an issue where the audio starts about half a second before the video for the second videoPath. So the first video in the concatenation will be perfect, but the second video will have unsynced audio and video, as the audio will be about half a second ahead of the video. I read in the README for the MP4Parser that this is a common issue, but I was unable to determine whatever solution they proposed. If anyone could explain how this issue could be resolved or if there is a better way for me to concatenate two .mov files in Java, I would be most appreciative.
wbradshaw is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.