我想在Android中制作一个配音应用程序。该应用程序的流程是:
- 从图库中获取视频和音频。
- 减少视频文件的原始声音。并在此视频文件上混合(配音)选定的音频。
- 在此视频文件上混合音频后,将其保存到外部存储器中。
我为此使用 MediaMuxer,但没有成功。请帮助我解决这个问题。
问候, 普拉泰克
我想在Android中制作一个配音应用程序。该应用程序的流程是:
我为此使用 MediaMuxer,但没有成功。请帮助我解决这个问题。
问候, 普拉泰克
即使我正在寻找相同的方法来使用 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);
}
}
}
并且不要忘记在您的项目中添加库。
这可能不是您问题的确切答案。但至少它能够阐明可能的解决方案。