1

To anyone experienced with using jcodec, my understanding is that the library cannot yet encode audio (it can decode it, however).

However, jcodec does have a PCMMP4MuxerTrack class which allows you to addSamples(ByteBuffer) of raw PCM data to an audio track while encoding your MP4 video. This produces an MP4 video file with sound.

I've successfully added some dummy PCM audio to an MP4 file that I exported and VLC played it properly, so it seems to work...


But if this worked, why do I keep seeing people say that MP4 doesn't support PCM audio (one source)?

Am I just getting lucky that VLC is accurately playing my MP4 file with sound? Would it potentially not work on other players/operating systems?

Community
  • 1
  • 1
yesbutmaybeno
  • 1,078
  • 13
  • 31
  • `why do I keep seeing people say that MP4 doesn't support PCM audio?` well because it doesn't. MP4 is by MPEG for containing MPEG codecs only (h.264/h.265 video and AAC/MP3 audio). In VLC go to **Tools** then **Codec Information**. Stream 0 is usually video and stream1 is audio. Does audio say MP4a? (then JCodec hasconverted PCM to AAC or MP3) otherwise it means you are lucky that VLC played it. Test by also dragging file into a browser tab (it might work since browsers understand both AAC and PCM). If browsers can play it too is that enough? – VC.One Aug 12 '15 at 11:25
  • Ah, that's actually very helpful, I didn't know that VLC trick. It is indeed PCM, which is good to know, since I was wondering if jcodec was converting to AAC/MP3 behind-the scenes - guess not. – yesbutmaybeno Aug 12 '15 at 18:10
  • Unfortunately Java is not my main language but I understand the media formats. I don't get how JCodec can write PCM but not AAC/MP3 bytes in that same place?? If you **[look here](https://github.com/jcodec/jcodec/tree/master/src/main/java/org/jcodec/containers/mp4/muxer)** then I think either **MP4Muxer.java** or **FramesMP4MuxerTrack.java** would help you add audio frames. MP3 and AAC audio is separated into frames themselves so maybe do a `for loop` to grab each AAC frame bytes and mux?? – VC.One Aug 12 '15 at 19:19
  • Lets say you have aac file you want to use as audio... open it in some **hex editor** to confirm what you **[read here](http://wiki.multimedia.cx/index.php?title=ADTS)**. Anyways get the frame length (integer) to a variable. Now go back to frame start bytes position and skip 7 bytes then grab everything according to `frame_length - 7` You have one aac frame ready to mux.. repeat from new position in bytes and so on until file end. – VC.One Aug 12 '15 at 19:24
  • @FTLRalph I tried your approach , but I am getting a wonky sound while playing video – UVM Oct 08 '15 at 18:16

1 Answers1

2

I hope this works, you tell me.. Open an AAC file in java to read its bytes

1) Prepare variables

int frameLength;
byte[] header = new byte[7]; //a byte array of 7 slots

2) Now copy first 7 bytes of AAC file stream into this "header" array

frameLength = (header[3]&0x03) << 11 | 
              (header[4]&0xFF) << 3 | 
              (header[5]&0xFF) >> 5 ;

3) From 8th byte in the AAC onwards you can copy all bytes up to FrameLength minus 7. We minus 7 because the extracted FrameLength includes the 7 bytes of an AAC header (called ADTS header) and in MP4 you have the sound minus ADTS header so each frame copy must skip those 7 bytes.

A tip: The first 5 bytes of the header remain same for each frame so you can search for that pattern of bytes throughout the file to identify positions of each AAC frame and if you know previous the frame's start pos then you can work out length also.

Now you have one AAC frame and you can try using FramesMP4MuxerTrack.java which has a function addFrame(MP4Packet pkt) where MP4Packet pkt would be your copied AAC frame bytes. You call the function each time you have a new AAC frame found.

VC.One
  • 14,790
  • 4
  • 25
  • 57
  • Thanks a lot for taking the time to put this all together, it seems pretty valid to me and gives me something to work off of, curious to see if this approach will work. Thanks again. – yesbutmaybeno Aug 13 '15 at 12:25
  • @Para I think I went the way of letting jcodec make the MP4 video file, then took my AAC file and MP4 file and muxed them together with mp4parser - works pretty simple https://github.com/sannies/mp4parser/blob/master/examples/src/main/java/org/mp4parser/examples/mux/filebased/MuxMe.java – yesbutmaybeno Apr 11 '18 at 11:05