2013-04-28 32 views
1

嗨,我一直有這個很多麻煩,我有2個文件,Mp4格式,閱讀他們到FileInputStreams,然後到ByteArrayOutputStreams。然後,我嘗試使用另一個ByteArrayOutputStream [finalOutputStream]和write()兩個字節來追加兩個字節數組。最後我使用FileOutputStream來寫入(finalOutputStream.toByteArray()),刷新,關閉。當我在手機上查看視頻時,有一個「最終」視頻應該有兩個組合視頻,尺寸看起來像兩個部分的大小加在一起..但是當我觀看視頻時,它只是第二部分-_- ......它就像第二部分覆蓋第一,但不知何故,該尺寸的增加?...繼承人一些代碼..安卓追加2 Mp4字節陣列一起,然後輸出1 Mp4文件(其中最終視頻是第一個視頻加上第二個視頻)

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

回答

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

感謝您的快速響應!我之前嘗試過,但可能已經搞砸了,對於Android來說很新。 1.)我是否需要該網站上的所有.jar文件? 2.)在.apk的包裝過程中,這些庫會被修剪嗎? 3.)我有一個包含視頻和音頻的文件(mp4),然後是另一個包含視頻和音頻的文件..我是否必須將每個文件分割成每一個? 重試..我會更新 – jp093121 2013-04-29 03:24:48

+0

幾乎有它,當我嘗試你的代碼(得到isoviewer-2.0-RC-23.jar和smooth-streaming-fragmenter-1.0-RC-23.jar [[遇到另一個問題[2013-04-28 23:56 :09 - Dex Loader]無法執行dex:多個dex文件定義Lcom/coremedia/iso/AbstractBoxParser $ 1;]]並在這裏計算出來:http://stackoverflow.com/a/16271158/1967928與我的答案.. :)]更換了audioTwo..etc ..等等音頻..現在我的最終視頻是第二個視頻附加在一起..第二個視頻..其第二個視頻連續兩次..將更新更多-_- – jp093121 2013-04-29 04:38:23

+0

我只用過這個: \t \t \t com.googlecode.mp4parser \t \t \t isoparser \t \t \t 1.0-RC-22 \t \t Benoit 2013-04-29 10:00:31

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視頻軌道,看到第一個元素[0]有一些關於「vide」的元素,元素[1]有「soun」s o開始與..

+0

你有任何問題,你的輸出文件的旋轉?我出來在風景旋轉,我需要它留在肖像 – 2013-06-16 01:19:14

+0

@埃德蒙羅哈斯你找到解決你的問題,我面臨同樣的問題。 – Ravi 2014-10-28 13:10:31

+0

@ rb16 我認爲不同設備的旋轉方式不同,我已經看到照片/視頻在某些方面出現旋轉,並在其他方面出現完美。 – jp093121 2014-10-28 14:03:53