安卓系统将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字节数相加,然后输出1个Mp4文件(最终视频是第一个视频加第二个视频)。的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

嗨,我有很多麻烦这个问题,我有2个文件,两个mp4格式,将它们读入fileInputStream,然后进入ByteArrayOutputStreams.然后,我尝试通过使用另一个字母向外跟踪stream [finalOutputStream]并编写()'ing the两个字节数组.最后我使用fileoutputstream来写入(finalOutputStream.TobyteArray()),刷新,关闭.当我在手机上寻找视频时,有一个"最终"视频应该拥有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();

推荐答案

如果您只是将视频附加在一起,它将不起作用,还需要重写标题.

我最近通过使用 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();

本文地址:https://www.itbaoku.cn/post/102582.html

问题描述

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();