您好我遇到了很多麻烦,我有2个文件,都是Mp4格式,将它们读入FileInputStreams,然后读入ByteArrayOutputStreams。然后我尝试使用另一个ByteArrayOutputStream [finalOutputStream]和write()来附加两个字节数组。最后我使用FileOutputStream来编写(finalOutputStream.toByteArray()),flush,close。当我在手机上查看视频时,会有一个“最终”视频应该有2个组合视频,其大小看起来像两个部分的大小加在一起..但是当我观看视频时,它只是第二部分-_- ......就像第二部分覆盖第一部分,但不知何故大小增加了?...继承了一些代码..
File fileOne = new File(fileLongName);
File fileTwo = new File(mediaStorageDir.getPath() + File.separator +"VID_TUTPART_"+ (foo-1) + ".mp4");
FileInputStream fisOne = new FileInputStream(fileOne);
FileInputStream fisTwo = new FileInputStream(fileTwo);
int bufferSize = 1024;
//FileOne
byte[] buffer = new byte[bufferSize];
ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
//FileTwo
byte[] bufferTwo = new byte[bufferSize];
ByteArrayOutputStream byteBufferTwo = new ByteArrayOutputStream();
int len = 0;
//FileOne to bytebuffer
while ((len = fisOne.read(buffer)) != -1) {
byteBuffer.write(buffer, 0, len);
}
//FileTwo to bytebuffer
while ((len = fisTwo.read(bufferTwo)) != -1) {
byteBufferTwo.write(buffer, 0, len);
}
byte[] byteArrayOne = byteBuffer.toByteArray();
byte[] byteArrayTwo = byteBuffer.toByteArray();
ByteArrayOutputStream finalOutputStream = new ByteArrayOutputStream( );
finalOutputStream.write( byteArrayOne );
finalOutputStream.write( byteArrayTwo );
int counterFileNumber = 0;
while(new File(mediaStorageDir.getPath() + File.separator +"VID_TO_TUTFIN_"+ counterFileNumber + ".mp4").exists()){
counterFileNumber++;
}
String outputFileNameString = mediaStorageDir.getPath() + File.separator +"VID_TO_TUTFIN_"+ counterFileNumber + ".mp4";
File outputFile = new File(outputFileNameString);
FileOutputStream fos = new FileOutputStream(outputFile);
fos.write(finalOutputStream.toByteArray());
fos.flush();
fos.close();
答案 0 :(得分:2)
@Benoit在你的帮助下想出了这个......希望它可以帮助其他人
File fileOne = new File(mediaStorageDir.getPath() + File.separator +"theNameOfMyFirstVideo.mp4");
File fileTwo = new File(mediaStorageDir.getPath() + File.separator +"theNameOfMySecondVideo.mp4");
FileInputStream fisOne = new FileInputStream(fileOne);
FileInputStream fisTwo = new FileInputStream(fileTwo);
Movie video = MovieCreator.build(Channels.newChannel(fisOne));
Movie videoTwo = MovieCreator.build(Channels.newChannel(fisTwo));
List<Track> videoTracks = video.getTracks();
Track testOneVideoTrack = videoTracks.get(0);
video.setTracks(new LinkedList<Track>());
List<Track> videoTwoTracks = videoTwo.getTracks();
Track testTwoVideoTrack = videoTwoTracks.get(0);
video.addTrack(new AppendTrack(testTwoVideoTrack,testOneVideoTrack));
int counterFileNumber = 0;
while(new File(mediaStorageDir.getPath() + File.separator +"VID_TO_TUTFIN_"+ counterFileNumber + ".mp4").exists()){
counterFileNumber++;
}
IsoFile out = new DefaultMp4Builder().build(video);
String outputFileNameString = mediaStorageDir.getPath() + File.separator +"VID_TO_TUTFIN_"+ counterFileNumber + ".mp4";
FileOutputStream fos = new FileOutputStream(new File(String.format(outputFileNameString)));
out.getBox(fos.getChannel());
fos.close();
当我调试时,我查看了List videoTracks并看到了第一个元素[0]有一些关于“vide”的东西,而元素[1]有“soun”所以开始使用它...
答案 1 :(得分:1)
如果您只是将视频附加在一起它将无效,您还需要重写标题。
我最近使用mp4parser
做了这个然后您可以关注sample
MovieCreator mc = new MovieCreator();
Movie video = mc.build(Channels.newChannel(AppendExample.class.getResourceAsStream("/count-video.mp4")));
Movie audio = mc.build(Channels.newChannel(AppendExample.class.getResourceAsStream("/count-english-audio.mp4")));
List<Track> videoTracks = video.getTracks();
video.setTracks(new LinkedList<Track>());
List<Track> audioTracks = audio.getTracks();
for (Track videoTrack : videoTracks) {
video.addTrack(new AppendTrack(videoTrack, videoTrack));
}
for (Track audioTrack : audioTracks) {
video.addTrack(new AppendTrack(audioTrack, audioTrack));
}
IsoFile out = new DefaultMp4Builder().build(video);
FileOutputStream fos = new FileOutputStream(new File(String.format("output.mp4")));
out.getBox(fos.getChannel());
fos.close();