I wanted to add audio to a mp4 file,that video doesn't have any audio ,will be added by user selected audio ,How can i achive this?
Asked
Active
Viewed 4,303 times
1 Answers
4
if your MP4 has been encoded by h.264 , you can easily use 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();
}

Ehsan
- 517
- 1
- 7
- 32
-
Thanks.. I did this by using a media mixer – rKrishna Sep 08 '14 at 06:14
-
cool, but I have never heard of media mixer library, could you send me the link? Thanks – Ehsan Sep 08 '14 at 08:11
-
1sorry not media mixer ,it was media muxer android 4.3 api https://developer.android.com/reference/android/media/MediaMuxer.html – rKrishna Sep 08 '14 at 08:13
-
Yup, it is for 4.3 and up but mp4parser dose not have this limitation. Good luck – Ehsan Sep 08 '14 at 08:14
-
2@rKrishna Would you be kind enough to let us know how you did it using media muxer? – Ankit Batra Mar 20 '17 at 18:53