2015-07-04 143 views
1

我正在使用mp4parser來合併音頻和視頻文件,這裏是下面的例子我已經嘗試過,但我在第一行本身得到空指針異常。我已將音頻和視頻文件保存在手機內存中的所需位置。如果我調試,第一線需要大量時間&與空指針錯誤分鐘後,剛剛停止在Android中合併音頻和視頻文件

try 
{ 
      H264TrackImpl h264Track = new H264TrackImpl(new FileDataSourceImpl("/mnt/sdcard/myvideo/video.mp4")); 
      AACTrackImpl aacTrack = new AACTrackImpl(new FileDataSourceImpl("/mnt/sdcard/myvideo/audio.acc")); 

      Movie movie = new Movie(); 
      movie.addTrack(h264Track); 
      movie.addTrack(aacTrack); 


      Container mp4file = new DefaultMp4Builder().build(movie); 

      FileChannel fc = new FileOutputStream(new File("output.mp4")).getChannel(); 
      mp4file.writeContainer(fc); 
      fc.close(); 

     } catch (Exception ee) 
     { 
      Toast.makeText(this,ee.getMessage(),Toast.LENGTH_LONG).show(); 
     } 

什麼是錯在我上面的代碼?

回答

1

**試試這個 我有幾件事要合併音頻和視頻。

  • 1)捕捉視頻空白不要使用任何音頻源等 「mediaRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_RECOGNITION);」
  • 2)將其保存到SD卡
  • 3)它不支持MIME類型= mp3。
  • 4)所以如果我們必須合併視頻和視頻。音頻必須是MP4或AAC **

5)調用的方法上按鈕點擊或在克里特

  • 字符串audiopath = 「/sdcard/audio.m4a」。

  • String videopath =「/sdcard/video.mp4」;

  • String outputpath =「/sdcard/output.mp4」;

  • mux(video,audio,output);

6)主代碼是在這裏傳遞只在您存儲的視頻,音頻(M4A,AAC),輸出路徑路徑。

public boolean mux(String videoFile, String audioFile, String outputFile) { 
    Movie video; 
    try { 
     video = new MovieCreator().build(videoFile); 

    } catch (RuntimeException e) { 
     e.printStackTrace(); 

     return false; 
    } catch (IOException e) { 
     e.printStackTrace(); 

     return false; 
    } 

    Movie audio; 
    try { 

     audio = new MovieCreator().build(audioFile); 

    } catch (IOException e) { 
     e.printStackTrace(); 

     return false; 
    } catch (NullPointerException e) { 
     e.printStackTrace(); 

     return false; 
    } 

    Track audioTrack = audio.getTracks().get(0); 
    video.addTrack(audioTrack); 

    Container out = new DefaultMp4Builder().build(video); 

    FileOutputStream fos; 
    try { 
       fos = new FileOutputStream(outputFile); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
     return false; 
    } 
    BufferedWritableFileByteChannel byteBufferByteChannel = new BufferedWritableFileByteChannel(fos); 
    try { 

     out.writeContainer(byteBufferByteChannel); 
     byteBufferByteChannel.close(); 
     Log.e("Audio Video", "11"); 
     fos.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
     return false; 
    } 
    return true; 
} 

private static class BufferedWritableFileByteChannel implements WritableByteChannel { 
    // private static final int BUFFER_CAPACITY = 1000000; 
    private static final int BUFFER_CAPACITY = 10000000; 

    private boolean isOpen = true; 
    private final OutputStream outputStream; 
    private final ByteBuffer byteBuffer; 
    private final byte[] rawBuffer = new byte[BUFFER_CAPACITY]; 

    private BufferedWritableFileByteChannel(OutputStream outputStream) { 
     this.outputStream = outputStream; 
     this.byteBuffer = ByteBuffer.wrap(rawBuffer); 
     Log.e("Audio Video", "13"); 
    } 

    @Override 
    public int write(ByteBuffer inputBuffer) throws IOException { 
     int inputBytes = inputBuffer.remaining(); 

     if (inputBytes > byteBuffer.remaining()) { 
      Log.e("Size ok ", "song size is ok"); 
      dumpToFile(); 
      byteBuffer.clear(); 

      if (inputBytes > byteBuffer.remaining()) { 
       Log.e("Size ok ", "song size is not okssss ok"); 
       throw new BufferOverflowException(); 
      } 
     } 

     byteBuffer.put(inputBuffer); 

     return inputBytes; 
    } 

    @Override 
    public boolean isOpen() { 
     return isOpen; 
    } 

    @Override 
    public void close() throws IOException { 
     dumpToFile(); 
     isOpen = false; 
    } 

    private void dumpToFile() { 
     try { 
      outputStream.write(rawBuffer, 0, byteBuffer.position()); 
     } catch (IOException e) { 
      throw new RuntimeException(e); 
     } 
    } 
}