目标,将MP3中的声音合成到MP4中去。
采用的第三方库:MP4Parser,
Muxing Audio/Video
The API and the process is straight-forward:
-
You wrap each raw format file into an appropriate Track object.
H264TrackImpl h264Track = new H264TrackImpl(new FileDataSourceImpl("video.h264")); AACTrackImpl aacTrack = new AACTrackImpl(new FileDataSourceImpl("audio.aac"));
-
These Track object are then added to a Movie object
Movie movie = new Movie(); movie.addTrack(h264Track); movie.addTrack(aacTrack);
-
The Movie object is fed into an MP4Builder to create the container.
Container mp4file = new DefaultMp4Builder().build(movie);
-
Write the container to an appropriate sink.
FileChannel fc = new FileOutputStream(new File("output.mp4")).getChannel(); mp4file.writeContainer(fc); fc.close();
要求.h264文件和aac文件的作为“输入”, 输出为mp4格式。
测试:使用MediaRecorder生成后缀为.H264的文件。测试可以在手机端播放。使用以上方法与一个aac文件合成。报错:
java.lang.NullPointerException: Attempt to read from field \'int com.googlecode.mp4parser.h264.model.PictureParameterSet.seq_parameter_set_id\' on a null object reference
h264Track = new H264TrackImpl(new FileDataSourceImpl("/storage/emulated/0/DCIM/TestCamera/VID_2016.H264"));//报错行
构造失败。谷歌该问题无果。
查看mp4parser的issues:
Combine a music.m4a on a video.mp4 overwrite my video audio
mp4parser的作者回复:
“Basically you want to merge two audio tracks to one. Players won\'t play two audio tracks at the same time. This is a use case that requires decoding, then merging and encoding into one audio track again. The MP4parser cannot do that. I guess you\'ll need to use ffmpeg or the MediaCodec API.”
一句话,mp4parser做不到。