2015-07-12 90 views
0

我想在Android中製作配音應用。 應用程序的流程是:如何在Android中配音視頻

  1. 從畫廊獲取視頻和音頻。
  2. 減少視頻文件的原始聲音。並在此視頻文件上混合(配音)所選音頻。
  3. 將此視頻文件中的音頻混合後,將其保存到外部存儲器中。

我正在使用MediaMuxer,但m沒有成功。請幫助我解決這個問題。

問候, Prateek

回答

0

即使我一直在尋找同樣的配音我視頻用mediaMuxer音頻,MediaMuxer有點難以接受的概念,我聽不懂,因爲我初學者。我結束了引用這個github代碼。 https://github.com/tqnst/MP4ParserMergeAudioVideo 這是我的救世主。對那個人真是太好了。 我剛拿起我想要的代碼,即用我指定的音頻配音視頻。

這裏是我的代碼,我在我的項目中使用下面

private void mergeAudioVideo(String originalVideoPath,String AudioPath,String OutPutVideoPath) { 
    // TODO Auto-generated method stub 
    Movie video = null; 
    try { 
     new MovieCreator(); 
     video = MovieCreator.build(originalVideoPath); 
    } catch (RuntimeException e) { 
     e.printStackTrace(); 

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

    } 

    Movie audio = null; 
    try { 
     new MovieCreator(); 
     audio = MovieCreator.build(AudioPath); 
    } catch (IOException e) { 
     e.printStackTrace(); 

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

    } 
    List<Track> videoTracks = new LinkedList<Track>(); 
    for (Track t : video.getTracks()) { 
     if (t.getHandler().equals("vide")) { 
      videoTracks.add(t); 
     //seperate the video from the orginal video 
     } 
    } 
    Track audioTrack = audio.getTracks().get(0);// get your audio track to dub the video 
    Movie result = new Movie(); 
    result.addTrack(videoTracks.get(0)); // add the video seprated from the originals 
    result.addTrack(audioTrack); //add the track to be put in resul video 

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

    FileOutputStream fos = null; 
    try { 
     fos = new FileOutputStream(OutPutVideoPath); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } 

    BufferedWritableFileByteChannel byteBufferByteChannel = new BufferedWritableFileByteChannel(fos); 
    try { 
     out.writeContainer(byteBufferByteChannel); 
     byteBufferByteChannel.close(); 
     fos.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

} 

這裏是BufferedWritableFileByteChannel類的outputVideo數據寫入到目錄中。

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

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

public BufferedWritableFileByteChannel(OutputStream outputStream) { 
    this.outputStream = outputStream; 
    this.byteBuffer = ByteBuffer.wrap(rawBuffer); 
} 

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

    if (inputBytes > byteBuffer.remaining()) { 
     dumpToFile(); 
     byteBuffer.clear(); 

     if (inputBytes > byteBuffer.remaining()) { 
      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); 
    } 
    } 
} 

並且不要忘記在您的項目中添加庫。

這可能不是問題的確切答案。但至少它能夠揭示可能的解決方案。