我正在使用这段代码,需要合并两个视频。它将所有视频保存在临时文件夹中,但没有合并。Append和DoAppend是我想要用于合并视频的函数。
public String append(ArrayList<String> trimVideos) {
for (int i = 0; i < trimVideos.size() - 1; i++) {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
if (i == 0) {
String OutPutFileName = Constants.STORAGE_VIDEO_TEMP_PATH +
File.separator + "APPEND" + "_" + timeStamp + ".mp4";
doAppend(trimVideos.get(0), trimVideos.get(i + 1),OutPutFileName);
Log.e(Constants.TAG, "In First: " + i + " " + OutPutFileName);
} else {
String OutPutFileName = Constants.STORAGE_VIDEO_TEMP_PATH
+ File.separator + "APPEND" + i + "_" + timeStamp + ".mp4";
doAppend(lastAppendOut, trimVideos.get(i + 1), OutPutFileName);
Log.e(Constants.TAG, "In Second: " + i + " " + OutPutFileName);
}
}
Log.e(Constants.TAG, "In End: " + " " + lastAppendOut);
return lastAppendOut;
}
这个方法在添加音轨时导致我的应用程序崩溃。
private String doAppend(String _firstVideo, String _secondVideo,String _newName) {
try {
Log.e("test", "Stage1");
FileInputStream fis1 = new FileInputStream(_firstVideo);
FileInputStream fis2 = new FileInputStream(_secondVideo);
Movie[] inMovies = new Movie[] {
MovieCreator.build(fis1.getChannel()),MovieCreator.build(fis2.getChannel()) };
List<Track> videoTracks = new LinkedList<Track>();
List<Track> audioTracks = new LinkedList<Track>();
//It returns one item of video and 2 item of video.
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);
}
}
}
Log.e("test", "Stage2");
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()])));
}
IsoFile out = new DefaultMp4Builder().build(result);
Log.e("test", "Stage3");
String filename = _newName;
lastAppendOut = filename;
Log.e(Constants.TAG, "In Append: " + " " + lastAppendOut);
FileOutputStream fos = new FileOutputStream(filename);
FileChannel fco = fos.getChannel();
fco.position(0);
out.getBox(fco);
fco.close();
fos.close();
fis1.close();
fis2.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
Log.e("check", e.getMessage());
}
return _newName;
}