是否可以将音频和视频文件合并为一个文件。我的 要求是:一个可以录制视频(mp4)的android应用程序,然后我的本地文件夹中有一些音频文件(mp3),我想将它添加到通过摄像机捕获的视频中。所以最终的效果就像播放视频,听到添加的音频,在内部进行合并。我该怎么做? 附言合并必须在内部完成。摄像机不会录制任何音频。
请帮忙
最佳答案
我无法通过使用 mp3 文件成功。相反,我使用 mp4 文件作为背景音频。如果您有兴趣处理 mp4 文件,下面的代码 fragment 可能对您有所帮助
String root = Environment.getExternalStorageDirectory().toString();
String audio = root +"/test.mp4";
String video = root +"/myvideo.mp4";
String output = root+ "/ouputVideo.mp4";
Log.e("FILE", "audio:"+audio + " video:"+video+ " out:"+output);
mux(video, audio, output);
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(1);
//video.getTracks().remove(1);
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();
fos.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
关于android - 在android中将音频文件与捕获的视频合并,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29474629/