I try to merge multiple mp4 files via moviemaker like I saw in one post:
public class Main {
public static void main(String[] args) throws IOException {
MovieCreator mc = new MovieCreator();
Movie movie1 = mc.build("./test1.mp4");
Movie movie2 = mc.build("./test2.mp4");
//Fetching the video tracks from the movies and storing them into an array
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);
//Fetching the audio tracks from the movies and storing them into an array
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("output.mp4");
c.writeContainer(fos.getChannel());
fos.close();
}
}
For two files this works totally fine. But I have more then two files to merge. So I started adding a loop which merged two files and used the merged file to merge with another one. Theoretically it works and I see by the time stamp they are merged. But I cant open the file because its an mp4a with avc1 sound... Has anybody an idea how to solve it? The Code below is my attemp: just choose a directory with some mp4 files, the merged files are named output0, output1, 2, ... The highest number is the final file. But while output0 works fine, already output1 wont work...
public void Merge(File folder){
File[] liste = folder.listFiles();
String path = jTextField1.getText();
for(int i = 0; i < liste.length-2; i++){
MovieCreator mc = new MovieCreator();
try{
Movie movie1 = mc.build(liste[i].toString());
System.out.println("Film 1: " + liste[i]);
Movie movie2 = mc.build(liste[i+1].toString());
System.out.println("Film 2: " + liste[i+1]);
//Fetching the video tracks from the movies and storing them into an array
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);
//Fetching the audio tracks from the movies and storing them into an array
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 = (Container) mp4Builder.build(movieOutput);
//Writing the output file
FileOutputStream fos = new FileOutputStream(path + "\\output" + i + ".mp4");
c.writeContainer(fos.getChannel());
fos.close();
File f = new File(path + "\\output" + i + ".mp4");
liste[i+1] = f;
}catch(Exception e2){
e2.printStackTrace();
}
}
}