1

嗨,我遇到了很多麻烦,我有 2 个文件,都是 Mp4 格式,将它们读入 FileInputStreams,然后读入 ByteArrayOutputStreams。然后我尝试通过使用另一个 ByteArrayOutputStream [finalOutputStream] 和 write()'ing 来附加两个字节数组。最后,我使用 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();
4

2 回答 2

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] 有“声音”所以开始使用它..

于 2013-04-29T05:09:18.610 回答
1

如果您只是将视频附加在一起,它将无法正常工作,您还需要重写标题。

我最近使用mp4parser做到了这一点

然后你可以按照示例

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();
于 2013-04-28T21:01:58.350 回答