What are the afconvert settings for the iTunes Plus AAC encoding setting? The challenge

I would like to convert a large collection of WAV files to AAC using the "iTunes Plus" setting in iTunes. iTunes describes this setting as:


  128 kbps (mono) / 256 kbps (stereo), 44.100 kHz, VBR, optimized for MMX/SSE2.


I would like to automate this import process.

Trial and error

The afconvert command line tool can convert between audio file formats. I've tried a few settings and the closest I could get is:

afconvert -v -d aac -b 256000 -f m4af -q 127 -s 2 --soundcheck-generate input.wav output.m4a

So: be verbose (-v), use the AAC data format (-d aac), encode at 256 kbps (-b 256000), store in the Apple MPEG-4 Audio file format (-f m4af), use the highest quality (-q 127), use VBR constrained (-s 2) and add SoundCheck data to the file.

I've also created an m4a file using the iTunes Plus setting.

The m4a file created by afconvert is about the same size as the iTunes file; just 65536 bytes smaller.

Results

Using the afinfo tool, I compared the file generated by iTunes to the file generated by afconvert. There are two differences in the afinfo output:


The file generated by iTunes has an audio data file offset of 131072, the one by afconvert 65536. This explains the difference in file size
Somehow the file generated by iTunes has different sound check values than the one converted by afconvert:


iTunes file:

sc ave perceived power coeff             343 407
sc max perceived power coeff             6873 8940
sc peak amplitude msec                   154389 194861
sc max perceived power msec              194025 194025
sc peak amplitude                        31129 30928

afconvert file:

sc ave perceived power coeff             341 388
sc max perceived power coeff             6087 4157
sc peak amplitude msec                   212509 152323
sc max perceived power msec              215481 185017
sc peak amplitude                        31024 29857



Especially the sc peak amplitude msec and sc max perceived power msec values differ quite a lot. These are the exact same input files, what could cause this difference?


Question

Is there a way to get results even closer to the "iTunes Plus" setting?
I had the exact same obsession, and ended up creating a FLAC-to-iTunes importer that decodes FLAC files to WAV and imports them into iTunes using iTunes' own encoder: https://github.com/cbguder/FLAC2iTunes

It should be easy enough to modify FLAC2iTunes to support direct WAV input.

As for the soundcheck values, I compared iTunes, XLD and afconvert by encoding WAV files to AAC and decoding them back to WAV and comparing the decoded audio data. I also compared afinfo outputs for the encoded files. While the souncheck values are different for each file, the decoded WAVs are exactly the same, so even if you use XLD or afconvert, the chances are the resulting files will sound exactly the same.

For afconvert, I used the parameters suggested by Apple in the "Mastered for iTunes" guide, with an intermediate CAF file:

afconvert source.wav intermediate.caf -d 0 -f caff --soundcheck-generate
afconvert intermediate.caf -d aac -f m4af -u pgcm 2 --soundcheck-read -b 256000 -q 127 -s 2 final.m4a


For XLD, I used these settings:

Format: MPEG-4 AAC
Mode: VBR (Constrained)
Encoder Quality: Max
Sample Rate: Auto
Target bitrate: 256 kbps
Add gapless information for iTunes: checked
Write accurate bitrate information: checked