Reputation: 1762
Hi I've been having a lot of trouble with this, I have 2 Files, both Mp4 format, read them into FileInputStreams, then into ByteArrayOutputStreams. I then try to append the two byte arrays by using another ByteArrayOutputStream [finalOutputStream] and write()'ing the two. Finally I use FileOutputStream to write(finalOutputStream.toByteArray()), flush, close. When i look for the video on my phone, there is a "Final" video that should have the 2 combined videos, with a size that looks like the two part's sizes added together.. but when i watch the video, it is only the second part -_- ... it is like the second part overwrites the first, but somehow the size increases?... heres some code..
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();
Upvotes: 1
Views: 2903
Reputation: 1762
@Benoit With your help came up with this.. hope it helps someone else
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();
When i was debugging, i looked at the List videoTracks and saw with the first element [0] there was something about "vide" and the element [1] had "soun" so started working with that..
Upvotes: 2
Reputation: 4599
If you simply make a append the video together it won't work, you also need to rewrite the header.
I did this recently by using mp4parser
Then you can follow the 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();
Upvotes: 1