16

I'm developing an Android App that records screen video and audio.
I recorded these 2 files : mp3 audio file and mp4 video file(no sound).

Now I want to mix them and create a new mp4 video file(with sound). From Android 4.3, Google suggests using the MediaMuxer class to mix stream audio and video. I have tried many times without success.

Any solution to resolve my issue with MediaMuxer API from Google? Any help will be greatly appreciated.

Crippledsmurf
  • 3,982
  • 1
  • 31
  • 50
nguoitotkhomaisao
  • 1,247
  • 1
  • 13
  • 24
  • Can you post the relevant code as an edit to your answer? If the question contains your code, others can see what you've tried thus far and might be able to figure out where your problem lies. – Crippledsmurf Dec 06 '13 at 05:58
  • The Android CTS tests include a test case that clones media files by copying the tracks with `MediaExtractor` and `MediaMuxer`; see http://bigflake.com/mediacodec/#MediaMuxerTest . It's a pretty rudimentary test but may be illuminating. – fadden Dec 06 '13 at 15:37
  • 1
    Dear fadden,I tried your example but still no success. It throws this exception : 12-09 11:58:33.569: E/MPEG4Writer(332): Unknown mime type 'audio/mpeg'. 12-09 11:58:33.569: A/MPEG4Writer(332): frameworks/av/media/libstagefright/MPEG4Writer.cpp:2699 CHECK(!"should not be here, unknown mime type.") Can you help me to solve this issue? Thanks in advance :) – nguoitotkhomaisao Dec 09 '13 at 05:00
  • @ChelseaDrogba Did you ever resolve this issue? it seems that the MediaMuxer can not read mp3 files?! – Guy Jun 25 '14 at 12:36
  • Dear Guy, I tried sometimes but still no success. I'm finding other solutions :( – nguoitotkhomaisao Jun 27 '14 at 02:33
  • I resolved this issue. First, I convert mp3/wav audio file to m4a using MediaCodec. After that I merge m4a audio to mp4 video. – nguoitotkhomaisao Aug 22 '14 at 04:03
  • Hey @ChelseaDrogba how did you manage to add audio into a video.I'm facing a same problem please help me in this issue.reply me here or Email me at robinroyal15@gmail.com – Robin Royal Nov 08 '14 at 09:33
  • @RobinRoyal I made a project to do this. Do you need full source code? (supoprt Android 4.3 and later) – nguoitotkhomaisao Nov 17 '14 at 06:26
  • @ChelseaDrogba Thanks for reply. If you can provide me source code I'll be very grateful to you.my Email Id is robinroyal15@gmail.com or you can help me in solving this issue. – Robin Royal Nov 18 '14 at 07:37
  • 1
    Dear @RobinRoyal I updated my answer with full sample source code. Please view below answer – nguoitotkhomaisao Nov 18 '14 at 09:04
  • @ChelseaDrogba can u please give the link of code "mp3/wav to m4a using Mediacodec" ? I will be very thankful to you. – Udit Kumawat Jun 16 '15 at 08:57
  • @RobinRoyal can u please give the link of code "mp3/wav to m4a using Mediacodec" ? I will be very thankful to you – Udit Kumawat Jun 16 '15 at 09:05
  • @UditKumawat any update on your query "can u please give the link of code "mp3/wav to m4a using Mediacodec" ? I will be very thankful to you " could you please tell the solution as even I am stuck here. – ayush bagaria Feb 03 '18 at 08:29
  • @ayushbagaria I didn't get any update on it – Udit Kumawat Feb 03 '18 at 11:04

2 Answers2

8

MediaMuxer does not transcode. If you write out an MPEG4 file, it will expect the video file to be MPEG4/AAC and the audio file to be an AAC file (m4a) as well.

Once you feed it with an m4a, muxing will succeed.

Guy
  • 12,250
  • 6
  • 53
  • 70
  • Hi,I have a video file without audio source when I tried to merge audio it gives illegal argument exception but it works for video with audio source is there any solution for video without audio source? i want to merge audio in a video file that doesn't contains any audio before any suggestion ? – Muhammad Haroon Jan 10 '22 at 05:31
6

This is a full sample source code to merge wav audio file to mp4 video file :

https://github.com/tqnst/MP4ParserMergeAudioVideo

nguoitotkhomaisao
  • 1,247
  • 1
  • 13
  • 24
  • 1
    facing NullPointerexception at new MovieCreator().build(audioFile) in the method mux(video, audio, output) – Nowshad May 17 '16 at 07:53
  • @Chelsea Drogba: I believe you solution employs mp4parser and such does not answer your question (which was to use Android's APIs). – FuzzyAmi Aug 09 '16 at 09:24
  • 1
    Useful code, but doesn't answer the original question, since it uses external `mp4parser` library. – smg Jun 29 '17 at 23:56
  • what should I do if I want to mix audio and video with different length? For example, length of the audio is 10 seconds, but length of the video is 1 minutes, and I want the audio repeat until the video is over. – Charlesjean Jul 26 '18 at 11:56