2015-07-12 85 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); 
    } 
    } 
} 

并且不要忘记在您的项目中添加库。

这可能不是问题的确切答案。但至少它能够揭示可能的解决方案。