Reputation: 1148
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?
Upvotes: 1
Views: 2728
Reputation: 15926
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.
Upvotes: 2