我正在使用MP4ParserMergeAudioVideo将新音频添加到MP4
文件中。图书馆说过要使用.wav
文件,如果我.wav
在目录中保留一个文件并将音频文件的名称命名为example.m4a
,我会得到一个找不到文件的异常。所以我将文件更改为.m4a
音频文件。代码如下。
findViewById(R.id.append).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String root = Environment.getExternalStorageDirectory().toString();
Log.e("",""+root);
String audio = root + "/download/"+"mymovieaudio.m4a";
String video = root + "/download/"+"My_Movie.mp4";
String output = root + "/download/ouput.mp4";
mux(video, audio, output);
}
});
mux函数是这样的
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();
fos.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
我成功地output.mp4
在我的设备的下载目录中获取了该文件。问题是它没有任何音频。请帮忙。提前致谢。