我正在开发一个使用 mp4parser 库(isoparser-1.0-RC-27.jar 和 aspectjrt-1.8.0.jar)合并 mp4 剪辑的应用程序。当两个剪辑合并时,它们成为一个剪辑,但随着更多剪辑添加到其中,输出 mp4 的音频位于视频后面。
这是代码:
Movie[] clips = new Movie[2];
//location of the movie clip storage
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "TestMerge");
//Build the two clips into movies
Movie firstClip = MovieCreator.build(first);
Movie secondClip = MovieCreator.build(second);
//Add both movie clips
clips[0] = firstClip;
clips[1] = secondClip;
//List for audio and video tracks
List<Track> videoTracks = new LinkedList<Track>();
List<Track> audioTracks = new LinkedList<Track>();
//Iterate all the movie clips and find the audio and videos
for (Movie movie: clips) {
for (Track track : movie.getTracks()) {
if (track.getHandler().equals("soun"))
audioTracks.add(track);
if (track.getHandler().equals("vide"))
videoTracks.add(track);
}
}
//Result movie from putting the audio and video together from the two clips
Movie result = new Movie();
//Append all audio and video
if (videoTracks.size() > 0)
result.addTrack(new AppendTrack(videoTracks.toArray(new Track[videoTracks.size()])));
if (audioTracks.size() > 0)
result.addTrack(new AppendTrack(audioTracks.toArray(new Track[audioTracks.size()])));
//Output the resulting movie to a new mp4 file
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String outputLocation = mediaStorageDir.getPath()+timeStamp;
Container out = new DefaultMp4Builder().build(result);
FileChannel fc = new RandomAccessFile(String.format(outputLocation), "rw").getChannel();
out.writeContainer(fc);
fc.close();
//Now set the active URL to play as the combined videos!
setURL(outputLocation);
}
我的猜测是,随着更多剪辑的添加,视频到音频的同步被搞砸了,因为如果合并两个较长的剪辑,那么音频/视频就可以了。有没有办法防止多个较小剪辑中的视频和音频同步不佳,或者有没有人找到使用 mp4parser 的解决方案?FFMpeg 是我正在考虑的另一种解决方案,但没有发现其他人使用它来执行此操作
编辑:我发现音频通常比视频长,因此,当添加越来越多的剪辑以创建一个剪辑时,这就是导致最终生成的视频偏移如此之大的原因。我将通过切断音频样本来解决