Saturday, July 30, 2011

SETTING UP AACPLUSENC WITH AMPACHE


AAC+ (also called HE-AAC) is a method to lower the bit rate of AAC (LC-AAC) while maintaining the audio fidelity. There’s a really good paper from the makers of AAC+ here.
Seeing how I just set up ampache, and I have low upstream bandwidth, I decided to get AAC+ going. It was a little bit tricky, but works very well now.


To set this up, I created a new format type ‘aacp’ (for AAC+). To be clear, Ampache expects you to give it some acronym of the file format (mp3, mp4, wmv, etc). I could have used mp4 or m4a, but I didn’t want to confuse things as AAC+ needs the MIME type audio/aacp to be sent, not audio/mp4. These monikers and their MIME types are defined in lib/class/song.class.php. I added the following lines:
      switch ($this->type) {
              case 'spx':
              case 'ogg':
                      $this->mime = "application/ogg";
              break;
              case 'wma':
              case 'asf':
                      $this->mime = "audio/x-ms-wma";
              break;
              case 'mp3':
              case 'mpeg3':
                      $this->mime = "audio/mpeg";
              break;
              case 'rm':
              case 'ra':
                      $this->mime = "audio/x-realaudio";
              break;
              case 'flac';
                      $this->mime = "audio/x-flac";
              break;
              case 'wv':
                      $this->mime = 'audio/x-wavpack';
              break;
              case 'aac':
              case 'mp4':
              case 'm4a':
                      $this->mime = "audio/mp4";
              break;
              /* begin Poojan's addition to support AAC+ */
              case 'aacp':
                      $this->mime = "audio/aacp";
              break;
              /* end  Poojan's addition to support AAC+ */
              case 'mpc':
                      $this->mime = "audio/x-musepack";
              break;
              default:
                      $this->mime = "audio/mpeg";
              break;
      }
I put the following in my ampache.cfg.php file to define transcoding from other formats to AAC+:
;######################################################

; These are commands used to transcode non-streaming

; formats to the target file type for streaming.

; This can be useful in re-encoding file types that don't stream

; very well, or if your player doesn't support some file types.

; This is also the string used when 'downsampling' is selected

; as some people have complained its not bloody obvious, any programs

; referenced in the downsample commands must be installed manually and in

; the web server path, and executable by the web server

; REQUIRED variables

; transcode_TYPE        = true/false ## True to force transcode regardless of prefs

; transcode_TYPE_target = TARGET_FILE_TYPE

; transcode_cmd_TYPE    = TRANSCODE_COMMAND

; %FILE%        = filename

; %OFFSET%      = offset

; %SAMPLE%      = sample rate

; %EOF%         = end of file in min.sec

; List of filetypes to transcode

transcode_m4a           = false

transcode_m4a_target    = aacp

transcode_flac  = true

transcode_flac_target   = aacp

;transcode_mp3          = false

transcode_mp3_target    = aacp

;transcode_ogg          = false

transcode_ogg_target    = mp3

; These are the commands that will be run to transcode the file

transcode_cmd_flac      = "flac -dc --skip=%OFFSET% --until=%EOF% %FILE% | acplusenc - - %SAMPLE%"

transcode_cmd_m4a       = "faad -f 2 -w %FILE% | aacplusenc - - %SAMPLE%"

transcode_cmd_mp3       = "mp3splt -qnf -o - %FILE% %OFFSET% %EOF% | lame --mp3input -S --decode - - | aacplusenc - - %SAMPLE%"

transcode_cmd_ogg       = "oggsplt -qn  -o - %FILE% %OFFSET% %EOF% | oggdec -Q -o - - | lame -S -q 3 -b %SAMPLE% -S - -"
You’ll notice that I’m encoding pretty much everything to AAC+. This has worked very well with a 64-kbps rate. One should note that aacplusenc is very picky about bit rates. Anything above 64 isn’t supported:
2008-07-19 15:28:16
Hi, I tried aacplusenc on Leopard (10.5.3) compiled myself with apple’s compiler and it works fine.
There is just one problem : I can’t encode more than 64kbps and I’d like to be able to encode on 80 kbps…
Another question : Is there any guidelines on using the libraries? I’d like to use them to build a stream encoder.
Thanks

teknoraver
2008-07-22 01:48:08
@NoSmile
maximum bitrate is 64kbit.
AAC+ achieves the same quality of AAC at half the bitrate,
so there is no need t ogo above 64kbit.
If you want higher bitrates, use plain AAC
Using AAC+ for everything breaks the XSPF flash player. However, I can still select the “stream” option for playback, which causes Ampache to send me a .m3uplaylist file that opens in Windows Media Player to stream the AAC+. Ampache faithfully transcodes from whatever native format the file is in (preferably FLAC) to AAC+ at 64 kbps.
Initially, my songs would cut off early. This post predicted that, stating that it’s because aacplusenc actually codes at a slightly higher bit rate than expected. However, I found that specifying the %OFFSET% and %EOF% options on each command helped for some reason.
It actually took a while to get the right command for FLAC. The flac command expects its —skip and —until parameters to be in a slightly different format than mp3splt. The subversion trunk version of ampache that I was using already took this into account for —skip, but I had to editlib/class/song.class.php to get the right formatting for %EOF%. I changed the following (original line is commented):
/* Get EOF */

$eofmm  = floor($song->time/60);

$eofss  = floor($song->time-$eofmm*60);

/* begin Poojan's edit to get flac to work */

// $eof    = sprintf("%02d.%02d",$eofmm,$eofss);

// If flac then format it slightly differently

if ($song->type == 'flac') {

$eof    = sprintf("%02d:%02d",$eofmm,$eofss);

}

else {

$eof    = sprintf("%02d.%02d",$eofmm,$eofss);

}

/* end  Poojan's edit to get flac to work */

$song_file = escapeshellarg($song->file);
In case you were wondering what AAC+ does, its’ the following: a spectral analysis is done. The lower half of the spectrum is encoded along with hints to reconstruct the higher half based on the lower half (this is called Spectral Band Replication, SBR, and forms HE-AAC v1). For low bit rates, mono audio is encoded with hints to reconstruct stereo (this is called Parametric Stereo, PS, and forms HE-AAC v2).
The upshot is that I don’t have to pay for serious bandwidth, and I effectively have my entire music collection on my Palm Pre (and in a browser at work). True: it cuts out sometimes. However, I’m starting to appreciate the album as an art form again. If I want to listen to a whole Guster or Fugazi album, I can


http://tech.poojanblog.com/blog/unix-linux/setting-up-aacplusenc-with-ampache/