Android的追加2的MP4字节数组相加后输出1 MP4文件(其中最后一个视频是第一视频和第二视频) [英] Android Appending 2 Mp4 Byte Arrays Together to Then Output 1 Mp4 File (where final video is first video plus second video)
问题描述
您好我一直有一个很大的麻烦,我有2个文件,无论是MP4格式,读成FileInputStreams,然后进入ByteArrayOutputStreams。然后,我尝试用另一种ByteArrayOutputStream [finalOutputStream]和写()'荷兰国际集团两个附加的两个字节数组。最后,我用的FileOutputStream写(finalOutputStream.toByteArray()),刷新,关闭。当我寻找我的手机上的视频,有一个最后的视频,应该有2组合的视频,其大小,看起来像两部分的尺寸加在一起..但是当我看录像,这仅仅是第二部分-_- ......它就像第二部分覆盖第一,但不知何故尺寸的增加?...继承人一些code ..
文件fileOne =新的文件(fileLongName);
文件FileTwo传送=新的文件(mediaStorageDir.getPath()+文件分割符+VID_TUTPART _+(FOO-1)+.MP4);
的FileInputStream fisOne =新的FileInputStream(fileOne);
的FileInputStream fisTwo =新的FileInputStream(FileTwo传送);
INT缓冲区大小= 1024;
// FileOne
byte []的缓冲区=新的字节[BUFFERSIZE]
ByteArrayOutputStream的ByteBuffer =新ByteArrayOutputStream();
// FileTwo传送
byte []的bufferTwo =新的字节[BUFFERSIZE]
ByteArrayOutputStream byteBufferTwo =新ByteArrayOutputStream();
INT的len = 0;
// FileOne到的ByteBuffer
而((LEN = fisOne.read(缓冲液))!= - 1){
byteBuffer.write(缓冲液,0,LEN);
}
// FileTwo传送到的ByteBuffer
而((LEN = fisTwo.read(bufferTwo))!= - 1){
byteBufferTwo.write(缓冲液,0,LEN);
}
byte []的byteArrayOne = byteBuffer.toByteArray();
byte []的byteArrayTwo = byteBuffer.toByteArray();
ByteArrayOutputStream finalOutputStream =新ByteArrayOutputStream();
finalOutputStream.write(byteArrayOne);
finalOutputStream.write(byteArrayTwo);
INT counterFileNumber = 0;
而(新文件(mediaStorageDir.getPath()+文件分割符+VID_TO_TUTFIN _+ counterFileNumber +.MP4)。存在()){
counterFileNumber ++;
}
字符串outputFileNameString = mediaStorageDir.getPath()+文件分割符+VID_TO_TUTFIN _+ counterFileNumber +.MP4;
文件OUTPUTFILE =新的文件(outputFileNameString);
FileOutputStream中FOS =新的FileOutputStream(OUTPUTFILE);
fos.write(finalOutputStream.toByteArray());
fos.flush();
fos.close();
如果你只是做一个附加的视频一起,将无法正常工作,您还需要重写头。
我最近做这个用 mp4parser
然后就可以按照样品
MovieCreator MC =新MovieCreator();
电影视频= mc.build(Channels.newChannel(AppendExample.class.getResourceAsStream(/计数video.mp4)));
电影音效= mc.build(Channels.newChannel(AppendExample.class.getResourceAsStream(/计数 - 英语 - audio.mp4)));
名单<轨道> videoTracks = video.getTracks();
video.setTracks(新的LinkedList<轨道>());
名单<轨道> audioTracks = audio.getTracks();
对于(轨道videoTrack:videoTracks){
video.addTrack(新AppendTrack(videoTrack,videoTrack));
}
对于(轨道audioTrack:audioTracks){
video.addTrack(新AppendTrack(audioTrack,audioTrack));
}
IsoFile OUT =新DefaultMp4Builder()建(视频)。
FileOutputStream中FOS =新的FileOutputStream(新文件(的String.Format(output.mp4)));
out.getBox(fos.getChannel());
fos.close();
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();
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();
这篇关于Android的追加2的MP4字节数组相加后输出1 MP4文件(其中最后一个视频是第一视频和第二视频)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!