如何将Audio track添加到视频mp4文件android?

时间:2014-07-14 12:37:59

标签: android media audiotrack

我想将音频添加到mp4文件,视频没有任何音频,将由用户选择的音频添加,我该如何实现?

1 个答案:

答案 0 :(得分:3)

如果您的MP4已由h.264编码,则可以轻松使用MP4parser

try {
String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
H264TrackImpl h264Track = new H264TrackImpl(new FileDataSourceImpl(baseDir + "/video.h264"));
AACTrackImpl aacTrack = new AACTrackImpl(new FileDataSourceImpl( baseDir + "/aac_sample.aac" ));

Movie movie = new Movie();
movie.addTrack(h264Track);
movie.addTrack(aacTrack);
Container mp4file = new DefaultMp4Builder().build(movie);

FileChannel fc = new FileOutputStream(new File(baseDir +"/output.mp4")).getChannel();
mp4file.writeContainer(fc);
fc.close();

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