sannies / mp4parser

A Java API to read, write and create MP4 files
Apache License 2.0
2.76k stars 566 forks source link

Merge video files, in which one of them has no audio track #296

Open Zer0bee opened 6 years ago

Zer0bee commented 6 years ago

I want to merge some video files, in which any one of them may not have audio track. Currently, if i merge these files, audio is moving into another segment, which is wrong. This is how, i am merging files

    LinkedList<Track> videoTracks = new LinkedList<>();
    LinkedList<Track> audioTracks = new LinkedList<>();

    for (Movie m : inMovies) {
        for (Track t : m.getTracks()) {
            if (t.getHandler().equals("soun")) {
                audioTracks.add(t);
            }
            if (t.getHandler().equals("vide")) {
                videoTracks.add(t);
            }
        }
  }

    Movie result = new Movie();
    if (audioTracks.size() > 0) {
        result.addTrack(new AppendTrack(audioTracks.toArray(new Track[audioTracks.size()])));
    }
    if (videoTracks.size() > 0) {
        result.addTrack(new AppendTrack(videoTracks.toArray(new Track[videoTracks.size()])));
    }

Video file that does not have audio, will not go inside if (t.getHandler().equals("soun"))

Zer0bee commented 6 years ago

Yes, you have to add silent audio track for the segment not having audio.

stonyz commented 5 years ago

@Zer0bee how to add silent audio, can you give sample code?