Friday, June 20, 2014

How to Set Up The Ampache Streaming Music Server In Ubuntu 12.04 LTS

Arguably transcoding is simultaneously the most powerful and most frustrating feature of an ampache install but please bare with me because you'll be thankfull you got it to work. I'm going to explain a little about transcoding then I'm going to give MY version of the configuration file lines as they are in my file and the commands you need to run to install the additional transcoding software that's required.
The way transcoding works is that ampache takes the original file, which could be in any format, and feeds it to a transcoding program such as avconv, which needs to be installed separately, along with a selection of program arguments depending on how the transcoding needs to be done and into what format the final file needs to be. The transcoding program then attempts to convert the file from one format to the next using the appropriate codec which also needs to be separately installed.
Since the ampache team has no control over the programs and codecs that carry this out there is only minimal help available for the correct syntax of the transcoding commands. I think the idea is that you should read the manual that comes with each program... Anyway, if any of your music is stored in anything other than mp3 you will need to get transcoding to work. Firstly the transcode command tells ampache the main file to use to start the transcode process. Next the encode args are what ampache adds to the main transcode command in order to modify the output format to suit the requested output format. With the new HTML5 player different browsers will request output in different formats for example, Firefox will request ogg, and chrome will request mp3 hence your encode args lines must be correct for both requested formats if you expect the player to work on different systems. At the time of writing the latest version of explorer was not capible of using HTML5 players, the bug is at there end but hopefully they will fix it soon. Until then you will need to use firefox or chrome to access the HTML5 player.
Here are the config lines for my transcode settings you will need to find the lines in the config php file as before and edit them to look as follows:
max_bit_rate = 576
min_bit_rate = 48
transcode_flac = required
transcode_mp3 = allowed
encode_target = mp3
transcode_cmd = "avconv -i %FILE%"
encode_args_ogg = "-vn -b:a max\(%SAMPLE%K\,49K\) -acodec libvorbis -vcodec libtheora -f ogg pipe:1"
encode_args_mp3 = "-vn -b:a %SAMPLE%K -acodec libmp3lame -f mp3 pipe:1"
encode_args_ogv = "-vcodec libtheora -acodec libvorbis -ar 44100 -f ogv pipe:1"
encode_args_mp4 = "-profile:0 baseline -frag_duration 2 -ar 44100 -f mp4 pipe:1"
I may come back and edit these later to improve video transcoding but for now they'll do for music at least.
You may note the strange encode_args_ogg line. This is because at the time of writing the libvorbis library cant handle outbit sample rates less than 42 or so. For this reason I've added a bit to the line to force at least 42K bit rate.
OK save and exit the config file and we will install the missing programs and codec libraries.
If at any time you think you've ruined any particular config parameter you can open and check the distribution config file.
Do this with the command
sudo nano /usr/local/src/www/ampache-3.6-alpha6/config/ampache.cfg.php.dist

Transcoding software

Add the repository of multimedia libraries and programs:
sudo nano  /etc/apt/sources.list
Add the following two lines to your sources file:
deb http://download.videolan.org/pub/debian/stable/ /
deb-src http://download.videolan.org/pub/debian/stable/ /
Save and quit (ctrl -x then hit y).
Add the repository:
wget -O - http://download.videolan.org/pub/debian/videolan-apt.asc|sudo apt-key add -
Install the compiler and package programs:
sudo apt-get -y install build-essential checkinstall cvs subversion git git-core mercurial pkg-config apt-file
Install the audio libraries required to build the avconv audio transcoding:
sudo apt-get -y install lame libvorbis-dev vorbis-tools flac libmp3lame-dev libavcodec-extra*
Install the video libraries required for avconv video transcoding:
sudo apt-get install libfaac-dev libtheora-dev libvpx-dev
At this stage support for video cataloging and streaming in ampache is limited so I have focussed mostly on audio transcoding options with the view to incorporate video transcoding when support within ampache is better. I've included some video components as I get them to work.
Now we're going to try and install the actual transcoding binary "avconv" which is preferred over ffmpeg in ubuntu.
This part will seem pretty deep and complicated but the processes are typical for installing programs from source files in ubuntu.
First you create a directory to store the source files, then you download the source files from the internet, then you configure the compiler with some settings, then you make the program and its libraries into a package, then you install the package.
Note that the "make" process is very time consuming (and scary looking) particularly for avconv. Just grap a cup of coffee and watch the fun unfold until the command line appears waiting for input from you again.
Note also that some online guides suggest "make install" rather than "checkinstall". In ubuntu I prefer checkinstall as this process generates a simple uninstall process and optionally allows you to easily specify the package dependancies if you so choose enabling you to share the packages with others.

(required) Get the architecture optimised compiler yasm

sudo mkdir /usr/local/src/yasm
cd ~/downloads
wget http://www.tortall.net/projects/yasm/releases/yasm-1.2.0.tar.gz  
You could also go to http://www.tortall.net/projects/yasm/releases/ and check that there isnt a version newer than 1.2.0 if you so choose and replace the link above with its link.
Unzip, configure, build, and install yasm:
tar xzvf yasm-1.2.0.tar.gz
sudo mv yasm-1.2.0 /usr/local/src/yasm/
cd /usr/local/src/yasm/yasm-1.2.0
sudo ./configure
sudo make
sudo checkinstall
Hit enter to select y to create the default docs.
Enter yasm when requested as the name for the installed program package.
Hit enter again and then again to start the process.

(optional) Get the x264 codec - required to enable on-the-fly video transcoding

sudo mkdir /usr/local/src/x264
cd /usr/local/src/
sudo git clone git://git.videolan.org/x264.git x264
cd /usr/local/src/x264
sudo ./configure --enable-shared --libdir=/usr/local/lib/
sudo make
sudo checkinstall
Hit enter to select y and create the default docs.
Enter x264 when requested as the name for the install.
Hit enter to get to the values edit part.
Select 3 to change the version.
Call it version 1.
Then hit enter again and again to start the process.

(Required) Get the avconv transcoding program

cd /usr/local/src/
sudo git clone git://git.libav.org/libav.git avconv
cd /usr/local/src/avconv
sudo LD_LIBRARY_PATH=/usr/local/lib/ ./configure --enable-gpl --enable-libx264 --enable-nonfree --enable-shared --enable-libmp3lame --enable-libvorbis --enable-libtheora --enable-libfaac --enable-libvpx > ~/avconv_configuration.txt
Note the bunch of settings we have set in build configuration - the most important for audio transcoding are --enable-libmp3lame --enable-libvorbis
libmp3lame allows us to transcode to mp3 and libvorbis allows us to transcode to ogg, which is required for the firefox HTML5 player.
Now we'll build avconv which takes ages. I've added the switch -j14 to runn multiple jobs during make. You may or may not have better luck with different values after the j depending on your own cpu architecture. 14 was best for me with my dual core hyperthreaded 8Gb RAM machine.
sudo make -j14
sudo checkinstall
Hit enter to select y to create the default docs.
Enter avconv when requested as the name for the installed program package.
Hit enter again and then again to start the process.
sudo ldconfig
sudo /etc/init.d/apache2 restart

Set up API/RPC access for external client program access

Next, in order that your iphone, android phone, or pimpache device  can access the music we need to set ACL/RPC and streaming permissions. NB: pimpache is not a typo but a "raspberry pi ampache client" project. It's currently under construction and located on github under "PiPro".
Go to a browser and login to ampache.
First we'll create an external user. In the web interface click the gray "admin" filing cabinet on the left.
Click the "add user" link.
Create a username such as <external user>.
Give it a GOOD password <external user password> but also remember that you may well be entering the password for it on your phone.
Set user access to "user".
Click "add user".
Click continue.
You can create users for different situations as well as different people. For example I have a "work" user with a lower default transcoding bit rate and a home user with a very high transcoding default bit rate.
Set the transcoding bitrate for a given user by going to the admin section (gray filing cabinet) then clicking "browse users" then the "preferences" icon.
The default transcoding bitrate is then set in the "transcode bitrate" section.
Logged in users may also set there own default transcode bit rate by clicking the "preferences" icon in the menu tab then streaming.
Now we'll add the API/RPC so the external user can get at the catalog.
In the web interface click the gray "admin" filing cabinet on the left.
Click the "show acls" link.
Now click the "add API/RPC host" link.
Give it an appropriate name such as "external" (no quotes).
  • level "read/write"
  • user external
  • acl type API/RPC
  • start 1.1.1.1 end 255.255.255.255
Click create ACL.
Click continue.
Now click the "add API/RPC host" link AGAIN.
Give it an appropriate name such as "external" (no quotes).
  • level "read/write"
  • user external
  • acl type API/RPC + Stream access
  • start 1.1.1.1 end 255.255.255.255
Click create ACL.
You may get the message "DUPLICATE ACL DEFINED".
That's ok.
Now click show ACLS and you should see two listed called external with the settings above - one for API/RPC and one for streaming.
You may see 4, thats ok as long ans external (or all) can get streaming and API access).
Now you can install and use many and varied clients to attach to your ampache server and start enjoying its musical goodness.
NB: The phones, viridian clients, etc will ask for a username, password and ampache url. Above, you setup an external username and password to use in just such a situation and the URL is the URL to your server from the outside world + /ampache.
eg: http://mypersonalampachesite.no-ip.biz/ampache
If you want to use an internal network address you may need to specify the ip address rather than the server host name depending on your router DNS system.

Catalog  setup

Finally after all that your music will hopefully have finished copying over and you can create your ampache music catalog.
Click on the gray "admin" filing cabinet in your ampache website.
Click "add a catalog".
Enter a simple name for your catalog eg "home".
Enter the path to the music folder you've been copying your files across to ie if you created a shared folder and copied your music to it as described above the path is: "/home/<ubuntu username>/music"
(watch out for capital letters here, swap out the <ubuntu username> for the username you used, and dont use the quotes " ).
Set the "Level catalog type" as local but note that you could potentially chain other catalogs to yours - how cool is that.
If you keep your music organised like I do then leave the filename and folder patterns as is.
Click to "gather album art" and "build playlists" then click "add catalog".

Final security tidyup

In the putty terminal. We'll restrict permissions to the extracted ampache folder to protect it from malicious software/people.
sudo chown www-data:www-data /usr/local/src/www/ampache-3.6-alpha6
sudo chmod -R 0700 /usr/local/src/www/ampache-3.6-alpha6/
That should do it.
If you need to move around in that directory again for some reason you will need to make the permissions more relaxed.
You can do this with
sudo chmod -R 0777 /usr/local/src/www/ampache-3.6-alpha6/
Dont forget to do
sudo chmod -R 0700 /usr/local/src/www/ampache-3.6-alpha6/
after to tighten it up again.

Now, go and amaze your friends and family by streaming YOUR music to THEIR PC or phone.

Troubleshooting

The best help I can offer is to look inside the log file if you followed the how-to above then you can find the log file at /var/log/ampache/.
cd /var/log/ampache
dir
Note the latest file name then access the log file with (for example):
 sudo nano /var/log/ampache/yyyymmdd.ampache.log
Please, if you have any advice re: transcoding commands feel free to leave helpful comments. I think help on this is really hard to come by.
For starters the best info I can find for avconv in general is at http://libav.org/avconv.html.
If you get permission errors when trying to copy to the music folder try again to relax the permissions on this folder with
sudo chmod 777 ~/music

Messed it up and want to start again from scratch?

Instead of reinstalling  ubuntu and LAMP and SAMBA you can delete ampache and its database with:
sudo mysql -u root -p
Enter your mysql password:
drop database ampache;
quit
sudo rm -R /usr/local/src/www/ampache-3.6-alpha6
cd ~/downloads
sudo tar zxvf ampache.tar.gz -C /usr/local/src/www
sudo chmod -R 7777 /usr/local/src/www/ampache-3.6-alpha6  
That should get you reset with all the default settings and ready to try again from the intial web logon.

0 comments: