Extract raw AAC metadata from mpeg audio file (m4a aac)

2,329 views
Skip to first unread message

Jeff Gosling

unread,
Nov 30, 2012, 10:12:09 AM11/30/12
to mp4parser-...@googlegroups.com
The mpeg container can wrap AAC data. However you can also create raw AACs that don't use the mpeg container.

I was just wondering if it would be possible to extract the raw data from an AAC m4a file and play the byte stream in an AAC player?

Does anyone know if the data within a m4a continer is the same as raw aac data?

Thanks

Sebastian Annies

unread,
Dec 1, 2012, 6:55:54 AM12/1/12
to mp4parser-...@googlegroups.com
Hi Jeff, 

technically it is the reverse operation to what is happening in AACTrackImpl (I haven't implemented the AACTrackImpl so I'm too familiar with it). Before the samples are written in to the MP4 file their header is stripped of. To reverse it you can iterate over the samples and re-created the header bytes.  Pseudo-code: 

        FileChannel fc = new FileOutputStream("out.aac").getChannel();
        Movie audio = MovieCreator.build(new FileInputStream(in.mp4).getChannel());
        List<ByteBuffer> samples = audio.getTracks().get(0).getSamples();
        for (ByteBuffer sample : samples) {
            ByteBuffer header = createHeader(sample);
            fc.write(header);
            fc.write(sample);
        }
        fc.close();

this how the header looks like: http://wiki.multimedia.cx/index.php?title=ADTS

I hope that helps, it shouldn't be too much work for you if your are fluent in Java. 

Best Regards,
Sebastian



2012/11/30 Jeff Gosling <gosl...@gmail.com>

Jeff

unread,
Dec 4, 2012, 7:30:30 AM12/4/12
to mp4parser-...@googlegroups.com
Thanks for your help.
I don't expect you to offer me support for my issues, but just so you know:

Trying your suggested method on 3 different m4a files results in a Movie object with no tracks. Tracks is empty.

Loading the files like this:

IsoFile isoFile = new IsoFile(fc);
List<Box> boxes = isoFile.getBoxes();

gives me 4 boxes

ftype
moov
free
mdat

i assume the mdat is the raw aac data, but have no idea how to extract the audio frames from that box in order to wrap them in ADTS headers.

Thanks :)
Jeff

Sebastian Annies

unread,
Dec 4, 2012, 8:30:42 AM12/4/12
to mp4parser-...@googlegroups.com

Did you compile the mp4parser on your own without Maven? What you describe is an indication that the aspectJ compiler didn’t run!

Alden Torres

unread,
Jan 23, 2013, 3:09:04 PM1/23/13
to mp4parser-...@googlegroups.com, gosl...@gmail.com

kalpesh gohel

unread,
Sep 2, 2013, 10:38:23 AM9/2/13
to mp4parser-...@googlegroups.com
Hi Sebastian, 

I want to extract AAC from MP4 in android.

Assumption : I think createHeader function may be out of for loop.

private byte[] createHeader(ByteBuffer byteBuf) throws IOException {

byte[] buf = new byte[7];
int dataSize = byteBuf.limit();
// dataSize -= 1;

long bits = 0;
bits = writeBits(bits, 12, 0xFFF); // A
bits = writeBits(bits, 1, 0);// B
bits = writeBits(bits, 2, 1); // C
bits = writeBits(bits, 1, 1);// D

buf[0] = (byte) (bits >> 8);
buf[1] = (byte) (bits);

bits = 0;

bits = writeBits(bits, 2, 0); // E
bits = writeBits(bits, 4, 4); // F : assume 4 =44100 from
bits = writeBits(bits, 1, 0); // G
bits = writeBits(bits, 3, 2); // H
bits = writeBits(bits, 1, 0); // I
bits = writeBits(bits, 1, 0); // J
bits = writeBits(bits, 1, 0); // K
bits = writeBits(bits, 1, 0); // L
bits = writeBits(bits, 13, (dataSize + 7)); // M
// bits = writeBits(bits, 2, (dataSize + 7) & 0x1800); // half M
// bits = writeBits(bits, 11, (dataSize + 7) & 0x7FF); // other Half M
bits = writeBits(bits, 11, 0x7FF); // O
bits = writeBits(bits, 2, byteBuf.limit());// P

buf[2] = (byte) (bits >> 32);
buf[3] = (byte) (bits >> 24);
buf[4] = (byte) (bits >> 16);// P
buf[5] = (byte) (bits >> 8);// Q
buf[6] = (byte) (bits);

return buf;
}

I tried using code given by you and "createheader" function as above to get aac file from mp4 but it generate messy file.

I tried with set header before loop but same problem.

Pls help

Thanks,
kalpesh
Reply all
Reply to author
Forward
0 new messages