{"id":202,"date":"2010-05-01T05:20:35","date_gmt":"2010-05-01T05:20:35","guid":{"rendered":"http:\/\/poojanwagh.opalstacked.com\/techblog\/unix-linux\/setting-up-aacplusenc-with-ampache\/"},"modified":"2010-05-01T05:29:05","modified_gmt":"2010-05-01T05:29:05","slug":"setting-up-aacplusenc-with-ampache","status":"publish","type":"post","link":"https:\/\/tech.poojanblog.com\/blog\/unix-linux\/setting-up-aacplusenc-with-ampache\/","title":{"rendered":"Setting up aacplusenc with Ampache"},"content":{"rendered":"<p>AAC+ (also called HE-AAC) is a method to lower the bit rate of AAC (LC-AAC) while maintaining the audio fidelity. There\u2019s a really good paper from the makers of AAC+ <a title=\"PDF Format: HE-AAC v2 MPEG-4\" href=\"http:\/\/www.ebu.ch\/fr\/technical\/trev\/trev_305-moser.pdf\">here<\/a>.<\/p>\n<p>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.<\/p>\n<p><!--more--><\/p>\n<p>To set this up, I created a new format type \u2018aacp\u2019 (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\u2019t 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 <code>lib\/class\/song.class.php<\/code>. I added the following lines:<\/p>\n<p>[cce_php]<br \/>\n      switch ($this-&gt;type) {<br \/>\n              case &#8216;spx&#8217;:<br \/>\n              case &#8216;ogg&#8217;:<br \/>\n                      $this-&gt;mime = &#8220;application\/ogg&#8221;;<br \/>\n              break;<br \/>\n              case &#8216;wma&#8217;:<br \/>\n              case &#8216;asf&#8217;:<br \/>\n                      $this-&gt;mime = &#8220;audio\/x-ms-wma&#8221;;<br \/>\n              break;<br \/>\n              case &#8216;mp3&#8217;:<br \/>\n              case &#8216;mpeg3&#8217;:<br \/>\n                      $this-&gt;mime = &#8220;audio\/mpeg&#8221;;<br \/>\n              break;<br \/>\n              case &#8216;rm&#8217;:<br \/>\n              case &#8216;ra&#8217;:<br \/>\n                      $this-&gt;mime = &#8220;audio\/x-realaudio&#8221;;<br \/>\n              break;<br \/>\n              case &#8216;flac&#8217;;<br \/>\n                      $this-&gt;mime = &#8220;audio\/x-flac&#8221;;<br \/>\n              break;<br \/>\n              case &#8216;wv&#8217;:<br \/>\n                      $this-&gt;mime = &#8216;audio\/x-wavpack&#8217;;<br \/>\n              break;<br \/>\n              case &#8216;aac&#8217;:<br \/>\n              case &#8216;mp4&#8217;:<br \/>\n              case &#8216;m4a&#8217;:<br \/>\n                      $this-&gt;mime = &#8220;audio\/mp4&#8221;;<br \/>\n              break;<br \/>\n              \/* begin Poojan&#8217;s addition to support AAC+ *\/<br \/>\n              case &#8216;aacp&#8217;:<br \/>\n                      $this-&gt;mime = &#8220;audio\/aacp&#8221;;<br \/>\n              break;<br \/>\n              \/* end  Poojan&#8217;s addition to support AAC+ *\/<br \/>\n              case &#8216;mpc&#8217;:<br \/>\n                      $this-&gt;mime = &#8220;audio\/x-musepack&#8221;;<br \/>\n              break;<br \/>\n              default:<br \/>\n                      $this-&gt;mime = &#8220;audio\/mpeg&#8221;;<br \/>\n              break;<br \/>\n      }<br \/>\n[\/cce_php]<\/p>\n<p>I put the following in my ampache.cfg.php file to define transcoding from other formats to AAC+:<\/p>\n<p>[cce_winbatch]<\/p>\n<p>;######################################################<\/p>\n<p>; These are commands used to transcode non-streaming<\/p>\n<p>; formats to the target file type for streaming.<\/p>\n<p>; This can be useful in re-encoding file types that don&#8217;t stream<\/p>\n<p>; very well, or if your player doesn&#8217;t support some file types.<\/p>\n<p>; This is also the string used when &#8216;downsampling&#8217; is selected<\/p>\n<p>; as some people have complained its not bloody obvious, any programs<\/p>\n<p>; referenced in the downsample commands must be installed manually and in<\/p>\n<p>; the web server path, and executable by the web server<\/p>\n<p>; REQUIRED variables<\/p>\n<p>; transcode_TYPE\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 = true\/false ## True to force transcode regardless of prefs<\/p>\n<p>; transcode_TYPE_target = TARGET_FILE_TYPE<\/p>\n<p>; transcode_cmd_TYPE\u00a0\u00a0\u00a0 = TRANSCODE_COMMAND<\/p>\n<p>; %FILE%\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 = filename<\/p>\n<p>; %OFFSET%\u00a0\u00a0\u00a0\u00a0\u00a0 = offset<\/p>\n<p>; %SAMPLE%\u00a0\u00a0\u00a0\u00a0\u00a0 = sample rate<\/p>\n<p>; %EOF%\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 = end of file in min.sec<\/p>\n<p>; List of filetypes to transcode<\/p>\n<p>transcode_m4a\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 = false<\/p>\n<p>transcode_m4a_target\u00a0\u00a0\u00a0 = aacp<\/p>\n<p>transcode_flac\u00a0 = true<\/p>\n<p>transcode_flac_target\u00a0\u00a0 = aacp<\/p>\n<p>;transcode_mp3\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 = false<\/p>\n<p>transcode_mp3_target\u00a0\u00a0\u00a0 = aacp<\/p>\n<p>;transcode_ogg\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 = false<\/p>\n<p>transcode_ogg_target\u00a0\u00a0\u00a0 = mp3<\/p>\n<p>; These are the commands that will be run to transcode the file<\/p>\n<p>transcode_cmd_flac\u00a0\u00a0\u00a0\u00a0\u00a0 = &#8220;flac -dc &#8211;skip=%OFFSET% &#8211;until=%EOF% %FILE% | acplusenc &#8211; &#8211; %SAMPLE%&#8221;<\/p>\n<p>transcode_cmd_m4a\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 = &#8220;faad -f 2 -w %FILE% | aacplusenc &#8211; &#8211; %SAMPLE%&#8221;<\/p>\n<p>transcode_cmd_mp3\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 = &#8220;mp3splt -qnf -o &#8211; %FILE% %OFFSET% %EOF% | lame &#8211;mp3input -S &#8211;decode &#8211; &#8211; | aacplusenc &#8211; &#8211; %SAMPLE%&#8221;<\/p>\n<p>transcode_cmd_ogg\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 = &#8220;oggsplt -qn\u00a0 -o &#8211; %FILE% %OFFSET% %EOF% | oggdec -Q -o &#8211; &#8211; | lame -S -q 3 -b %SAMPLE% -S &#8211; -&#8221;<\/p>\n<p>[\/cce_winbatch]<\/p>\n<p>You\u2019ll notice that I\u2019m encoding pretty much everything to AAC+. This has worked very well with a 64-kbps rate. One should note that <code>aacplusenc<\/code> is very picky about bit rates. <a title=\"See comments on this page for the following quote\" href=\"http:\/\/teknoraver.net\/software\/mp4tools\/\">Anything above 64 isn\u2019t supported:<\/a><\/p>\n<blockquote><p><strong><a href=\"mailto:djnosmile@gmail.com\">NoSmile<\/a><\/strong><\/p>\n<p>2008-07-19 15:28:16<\/p>\n<p>Hi, I tried aacplusenc on Leopard (10.5.3) compiled myself with apple&#8217;s compiler and it works fine.<\/p>\n<p>There is just one problem : I can&#8217;t encode more than 64kbps and I&#8217;d like to be able to encode on 80 kbps&#8230;<\/p>\n<p>Another question : Is there any guidelines on using the libraries? I&#8217;d like to use them to build a stream encoder.<\/p>\n<p>Thanks<\/p>\n<hr \/>\n<p><strong>teknoraver<\/strong><\/p>\n<p>2008-07-22 01:48:08<\/p>\n<p>@NoSmile<\/p>\n<p>maximum bitrate is 64kbit.<\/p>\n<p>AAC+ achieves the same quality of AAC at half the bitrate,<\/p>\n<p>so there is no need t ogo above 64kbit.<\/p>\n<p>If you want higher bitrates, use plain AAC<\/p><\/blockquote>\n<p>Using AAC+ for everything breaks the XSPF flash player. However, I can still select the \u201cstream\u201d option for playback, which causes Ampache to send me a <code>.m3u<\/code> playlist 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.<\/p>\n<p>Initially, my songs would cut off early. This post predicted that, stating that it\u2019s because <code>aacplusenc<\/code> actually codes at a slightly higher bit rate than expected. However, I found that specifying the <code>%OFFSET%<\/code> and <code>%EOF%<\/code> options on each command helped for some reason.<\/p>\n<p>It actually took a while to get the right command for FLAC. The <code>flac<\/code> command expects its <code>\u2014skip<\/code> and <code>\u2014until<\/code> parameters to be in a slightly different format than <code>mp3splt<\/code>. The subversion <em>trunk<\/em> version of ampache that I was using already took this into account for <code>\u2014skip<\/code>, but I had to edit <code>lib\/class\/song.class.php<\/code> to get the right formatting for <code>%EOF%<\/code>. I changed the following (original line is commented):<\/p>\n<p>[cce_php]<\/p>\n<p>\/* Get EOF *\/<\/p>\n<p>$eofmm\u00a0 = floor($song-&gt;time\/60);<\/p>\n<p>$eofss\u00a0 = floor($song-&gt;time-$eofmm*60);<\/p>\n<p>\/* begin Poojan&#8217;s edit to get flac to work *\/<\/p>\n<p>\/\/ $eof\u00a0\u00a0\u00a0 = sprintf(&#8220;%02d.%02d&#8221;,$eofmm,$eofss);<\/p>\n<p>\/\/ If flac then format it slightly differently<\/p>\n<p>if ($song-&gt;type == &#8216;flac&#8217;) {<\/p>\n<p>$eof\u00a0\u00a0\u00a0 = sprintf(&#8220;%02d:%02d&#8221;,$eofmm,$eofss);<\/p>\n<p>}<\/p>\n<p>else {<\/p>\n<p>$eof\u00a0\u00a0\u00a0 = sprintf(&#8220;%02d.%02d&#8221;,$eofmm,$eofss);<\/p>\n<p>}<\/p>\n<p>\/* end\u00a0 Poojan&#8217;s edit to get flac to work *\/<\/p>\n<p>$song_file = escapeshellarg($song-&gt;file);<\/p>\n<p>[\/cce_php]<\/p>\n<p>In case you were wondering what AAC+ does, its\u2019 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).<\/p>\n<p>The upshot is that I don\u2019t 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\u2019m starting to appreciate the <em>album<\/em> as an art form again. If I want to listen to a whole Guster or Fugazi album, I can.\n<\/p>\n<div class='wp_likes' id='wp_likes_post-202'><a class='like' href=\"javascript:wp_likes.like(202);\" title='' ><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/tech.poojanblog.com\/blog\/wp-content\/plugins\/wp-likes\/images\/like.png\" alt='' border='0'\/><\/a><span class='text'>Be the first to like.<\/span><\/p>\n<div class='like' ><a href=\"javascript:wp_likes.like(202);\">Like<\/a><\/div>\n<div class='unlike' ><a href=\"javascript:wp_likes.unlike(202);\">Unlike<\/a><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>AAC+ (also called HE-AAC) is a method to lower the bit rate of AAC (LC-AAC) while maintaining the audio fidelity. There\u2019s 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 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[10],"tags":[83,84,81,82,87,86,85],"class_list":["post-202","post","type-post","status-publish","format-standard","hentry","category-unix-linux","tag-aac","tag-aacplusenc","tag-ampache","tag-flac","tag-he-aac","tag-ps","tag-sbr"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/tech.poojanblog.com\/blog\/wp-json\/wp\/v2\/posts\/202","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tech.poojanblog.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tech.poojanblog.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tech.poojanblog.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/tech.poojanblog.com\/blog\/wp-json\/wp\/v2\/comments?post=202"}],"version-history":[{"count":4,"href":"https:\/\/tech.poojanblog.com\/blog\/wp-json\/wp\/v2\/posts\/202\/revisions"}],"predecessor-version":[{"id":206,"href":"https:\/\/tech.poojanblog.com\/blog\/wp-json\/wp\/v2\/posts\/202\/revisions\/206"}],"wp:attachment":[{"href":"https:\/\/tech.poojanblog.com\/blog\/wp-json\/wp\/v2\/media?parent=202"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tech.poojanblog.com\/blog\/wp-json\/wp\/v2\/categories?post=202"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tech.poojanblog.com\/blog\/wp-json\/wp\/v2\/tags?post=202"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}