0

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();
      }
    }
  }
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
  • Pick those 2 files which work, and try to merge them as 1-2-1-2 to see if it's really about the amount of files. Because it may easily happen that these 2 files use matching codecs by chance, and the 3rd one uses a different one. – tevemadar Jan 25 '23 at 10:38
  • Good idea, even if the files would be in wrong order. Unfortunatly its the same problem. mp4a with avc1... – Niklas Wicke Jan 26 '23 at 11:23

1 Answers1

0

since i can't comment yet, i'm writing it as an answer. i don't really know about moviemaker, however i've been dealing with media files and thought it could be different perspective to your problem.

you can use ffmpeg to merge media files as answered in here. also if you want to stick with java, here's the java wrapper for ffmpeg command line tool.

younver
  • 5
  • 1
  • 8