我尝试通过moviemaker合并多个MP4文件,就像我在一个帖子中看到的:
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();
}
}
对于两个文件,这完全可以正常工作。但我有两个以上的文件要合并。所以我开始添加一个合并两个文件的循环,并使用合并的文件与另一个合并。理论上它的工作,我看到他们合并的时间戳。但我不能打开文件,因为它是一个带有avc1声音的mp4a ...有没有人知道如何解决它?下面的代码是我的尝试:只要选择一个包含一些mp4文件的目录,合并后的文件命名为output0,output1,2,...最大的数字是最终文件。但是当output0工作正常时,output1已经不工作了...
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();
}
}
}
1条答案
按热度按时间s8vozzvw1#
因为我还不能发表评论,我写它作为一个答案。我真的不知道电影制作者,但我一直在处理媒体文件,并认为它可能是不同的Angular 来看待你的问题。
你可以使用ffmpeg来合并here中回答的媒体文件.同样如果你想坚持使用java,这里是ffmpeg命令行工具的java wrapper.