transcoding mp3-to-mp3 in mt-daapd
Introduction
mt-daapd is an iTunes daap-protocol server for Unix systems. I’m using mt-daapd on by Ubuntu 9.04 system to stream music over the internet to my workplace. The issue is that many of my mp3 files are 256kbps mp3 files, and streaming these takes up more bandwidth than necessary.
mt-daapd includes a transcoding system called “ssc” for “Server Side Conversion”. This system works by either a shared-object plugin (i.e. ssc_ffmpeg) or via an external script file (i.e. ssc_script).
using the ssc_script functionality, one can invoke ‘lame’ (the mp3 encoder) to transcode high bitrate mp3 files into lower bitrate mp3 files. The setup for this in mt-daapd wasn’t very obvious, so I’ll document it here.
Install mt-daapd
The version of mt-daapd provided by Ubuntu 9.04 includes support for transcoding, so you can just run:
$ sudo apt-get install mt-daapd
Turn on ssc
Edit your /etc/mt-daapd.conf file, and make sure you have the following lines in the correct places:
ssc_codectypes = mpeg
always_transcode = mpeg
ssc_prog = /path/to/script/shown/below/mt-daapd-mp3-ssc.sh
[plugins]
plugins = ssc-script.so
Create mt-daapd-mp3-ssc.sh
You’ll need to create a shell script to do the transcoding via ssc_scrpit / ssc_prog. Here’s the script I’m using:
#!/bin/bash
FILE=$1
OFFSET=0if [ "$2" == "" ]; then
OFFSET=0
else
OFFSET=$2
fiif [ "$3" == "" ]; then
FORGELEN=$3
filame -V6 –quiet “$FILE” -
Note that I’m looking at the 2nd and 3rd args, but I’m not doing anything with them. These arguments are used to make sure that seeking works properly in your daap client. I don’t care about seeking, and doing it properly is somewhat hard, so I’ve just ignored those arguments. I think that this could be accomplished properly using the program ‘mp3splt’ but I haven’t looked into it enough to see what it would take.
Make sure the script above is put somewhere accessible by the user specified as ‘runas’ in your mt-daapd.conf. Make sure the script is executable by this user as well.
Restart mt-daapd and see if it works
Restart mt-daapd via:
$ sudo /etc/init.d/mt-daapd restart
Then reconnect your daap client (I use rhythmbox). You can see if the script is working by running “ps auwxww | grep lame” just after pressing play on a song. You should see your script executing lame and doing realtime transcoding.
Hi,
I’ve just tried to set the mt-daapd.conf, but I can’t get it to transcode on the fly. I have all my songs in flac and I’d like to transcode it to the mp3 with your script.
My settings are:
ssc_codectypes = mpegalways_transcode = mpeg,flac,wav
ssc_prog = /usr/bin/mt-daapd-mp3-ssc.sh
[plugins]
plugins = ssc-script.so
The script has the +x bit set. This is my log where you can see it still uses the ssc-ffmpeg, although I don’t want it to do so:
2010-10-24 18:08:04 (b6087b70): Entering config_set_status
2010-10-24 18:08:04 (b6087b70): Exiting config_set_status
2010-10-24 18:08:04 (b6087b70): Executing: update songs set play_count=play_count + 1, time_played=1287936484 where id=41559
2010-10-24 18:08:04 (b6087b70): Rows: 1
2010-10-24 18:08:04 (b6087b70): Thread 2: Terminating
2010-10-24 18:08:04 (b6087b70): Thread 2: Freeing request headers
2010-10-24 18:08:04 (b6087b70): Thread 2: Freeing response headers
2010-10-24 18:08:04 (b6087b70): Thread 2: Freeing request vars
2010-10-24 18:08:04 (b6087b70): Thread 2: Closing fd
2010-10-24 18:08:04 (b6087b70): With thread 2 exiting, 1 are still running
2010-10-24 18:08:05 (b7089b70): Got listen socket 13
2010-10-24 18:08:05 (b6087b70): Thread 3:
2010-10-24 18:08:05 (b6087b70): Request: GET /databases/1/items/41562.wav?session-id=1 HTTP/1.1
2010-10-24 18:08:05 (b6087b70): Thread 3: Read: Host: 192.168.200.2:3689
2010-10-24 18:08:05 (b6087b70): Thread 3: Adding header *Host=192.168.200.2:3689*
2010-10-24 18:08:05 (b6087b70): Added *Host=192.168.200.2:3689*
2010-10-24 18:08:05 (b6087b70): Thread 3: Read: Connection: close
2010-10-24 18:08:05 (b6087b70): Thread 3: Adding header *Connection=close*
2010-10-24 18:08:05 (b6087b70): Added *Connection=close*
2010-10-24 18:08:05 (b6087b70): Thread 3: Read: transferMode.dlna.org: Streaming
2010-10-24 18:08:05 (b6087b70): Thread 3: Adding header *transferMode.dlna.org=Streaming*
2010-10-24 18:08:05 (b6087b70): Added *transferMode.dlna.org=Streaming*
2010-10-24 18:08:05 (b6087b70): Thread 3: Read: Accept: */*
2010-10-24 18:08:05 (b6087b70): Thread 3: Adding header *Accept=*/**
2010-10-24 18:08:05 (b6087b70): Added *Accept=*/**
2010-10-24 18:08:05 (b6087b70): Thread 3: Read: Cache-Control: no-cache
2010-10-24 18:08:05 (b6087b70): Thread 3: Adding header *Cache-Control=no-cache*
2010-10-24 18:08:05 (b6087b70): Added *Cache-Control=no-cache*
2010-10-24 18:08:05 (b6087b70): Thread 3: Read: Accept-Language: en-us, en;q=5.0
2010-10-24 18:08:05 (b6087b70): Thread 3: Adding header *Accept-Language=en-us, en;q=5.0*
2010-10-24 18:08:05 (b6087b70): Added *Accept-Language=en-us, en;q=5.0*
2010-10-24 18:08:05 (b6087b70): Thread 3: Read: Client-DAAP-Access-Index: 2
2010-10-24 18:08:05 (b6087b70): Thread 3: Adding header *Client-DAAP-Access-Index=2*
2010-10-24 18:08:05 (b6087b70): Added *Client-DAAP-Access-Index=2*
2010-10-24 18:08:05 (b6087b70): Thread 3: Read: Client-DAAP-Version: 3.0
2010-10-24 18:08:05 (b6087b70): Thread 3: Adding header *Client-DAAP-Version=3.0*
2010-10-24 18:08:05 (b6087b70): Added *Client-DAAP-Version=3.0*
2010-10-24 18:08:05 (b6087b70): Thread 3: Read: Client-DAAP-Validation: F6BAACD87A4DD3B5FE199E60ACB28A76
2010-10-24 18:08:05 (b6087b70): Thread 3: Adding header *Client-DAAP-Validation=F6BAACD87A4DD3B5FE199E60ACB28A76*
2010-10-24 18:08:05 (b6087b70): Added *Client-DAAP-Validation=F6BAACD87A4DD3B5FE199E60ACB28A76*
2010-10-24 18:08:05 (b6087b70): Thread 3: Read: Client-DAAP-Request-ID: 2
2010-10-24 18:08:05 (b6087b70): Thread 3: Adding header *Client-DAAP-Request-ID=2*
2010-10-24 18:08:05 (b6087b70): Added *Client-DAAP-Request-ID=2*
2010-10-24 18:08:05 (b6087b70): Thread 3: Read: Connection: close
2010-10-24 18:08:05 (b6087b70): Thread 3: Adding header *Connection=close*
2010-10-24 18:08:05 (b6087b70): Updating Connection from close to close
2010-10-24 18:08:05 (b6087b70): Thread 3: Read: User-Agent: GStreamer souphttpsrc libsoup/2.31.92
2010-10-24 18:08:05 (b6087b70): Thread 3: Adding header *User-Agent=GStreamer souphttpsrc libsoup/2.31.92*
2010-10-24 18:08:05 (b6087b70): Added *User-Agent=GStreamer souphttpsrc libsoup/2.31.92*
2010-10-24 18:08:05 (b6087b70): Thread 3: Read:
2010-10-24 18:08:05 (b6087b70): Thread 3: Headers parsed!
2010-10-24 18:08:05 (b6087b70): Checking to see if connection matches close
2010-10-24 18:08:05 (b6087b70): And it DOES!
2010-10-24 18:08:05 (b6087b70): Thread 3: Connection type HTTP/1.1
: Connection: non-persist
2010-10-24 18:08:05 (b6087b70): Thread 3: parsing GET args
2010-10-24 18:08:05 (b6087b70): Thread 3: Adding arg session-id = 1
2010-10-24 18:08:05 (b6087b70): Added *session-id=1*
2010-10-24 18:08:05 (b6087b70): Thread 3: Done parsing GET/POST args!
2010-10-24 18:08:05 (b6087b70): Thread 3: Original URI: /databases/1/items/41562.wav
2010-10-24 18:08:05 (b6087b70): Thread 3: Translated URI: /databases/1/items/41562.wav
2010-10-24 18:08:05 (b6087b70): Thread 3: Preparing to find handler
2010-10-24 18:08:05 (b6087b70): Checking /databases/1/items/41562.wav against handler for /
2010-10-24 18:08:05 (b6087b70): Thread 3: URI Match!
2010-10-24 18:08:05 (b6087b70): Thread 3: Time is 1287936485 seconds after epoch
2010-10-24 18:08:05 (b6087b70): Thread 3: Setting time header
2010-10-24 18:08:05 (b6087b70): Added *Date=Sun, 24 Oct 2010 16:08:05 GMT*
2010-10-24 18:08:05 (b6087b70): Added *Connection=close*
2010-10-24 18:08:05 (b6087b70): Added *Server=mt-daapd/svn-1696*
2010-10-24 18:08:05 (b6087b70): Added *Content-Type=text/html*
2010-10-24 18:08:05 (b6087b70): Added *Content-Language=en_us*
2010-10-24 18:08:05 (b6087b70): Thread 3: Using non-default handler
2010-10-24 18:08:05 (b6087b70): in main_auth
2010-10-24 18:08:05 (b6087b70): Checking url /databases/1/items/41562.wav
2010-10-24 18:08:05 (b6087b70): Dispatching auth for /databases/1/items/41562.wav to plugin
2010-10-24 18:08:05 (b6087b70): Checking url /databases/1/items/41562.wav
2010-10-24 18:08:05 (b6087b70): Dispatching /databases/1/items/41562.wav to daap/svn-1696
2010-10-24 18:08:05 (b6087b70): in main_handler
2010-10-24 18:08:05 (b6087b70): Checking url /databases/1/items/41562.wav
2010-10-24 18:08:05 (b6087b70): Dispatching /databases/1/items/41562.wav to plugin
2010-10-24 18:08:05 (b6087b70): Checking url /databases/1/items/41562.wav
2010-10-24 18:08:05 (b6087b70): Dispatching /databases/1/items/41562.wav to daap/svn-1696
2010-10-24 18:08:05 (b6087b70): Getting uri...
2010-10-24 18:08:05 (b6087b70): Mallocing privinfo...
2010-10-24 18:08:05 (b6087b70): Added *Accept-Ranges=bytes*
2010-10-24 18:08:05 (b6087b70): Added *DAAP-Server=firefly/svn-1696*
2010-10-24 18:08:05 (b6087b70): Updating Content-Type from text/html to application/x-dmap-tagged
2010-10-24 18:08:05 (b6087b70): Added *Cache-Control=no-cache*
2010-10-24 18:08:05 (b6087b70): Added *Expires=-1*
2010-10-24 18:08:05 (b6087b70): Tokenizing url
2010-10-24 18:08:05 (b6087b70): Found 17 elements
2010-10-24 18:08:05 (b6087b70): Checking reponse 0
2010-10-24 18:08:05 (b6087b70): Checking reponse 1
2010-10-24 18:08:05 (b6087b70): Checking reponse 2
2010-10-24 18:08:05 (b6087b70): Checking reponse 3
2010-10-24 18:08:05 (b6087b70): Checking reponse 4
2010-10-24 18:08:05 (b6087b70): Checking reponse 5
2010-10-24 18:08:05 (b6087b70): Checking reponse 6
2010-10-24 18:08:05 (b6087b70): Checking reponse 7
2010-10-24 18:08:05 (b6087b70): Checking reponse 8
2010-10-24 18:08:05 (b6087b70): Checking reponse 9
2010-10-24 18:08:05 (b6087b70): Found it! Index: 9
2010-10-24 18:08:05 (b6087b70): Executing: select * from songs where id=41562
2010-10-24 18:08:05 (b6087b70): Creating new db handle
2010-10-24 18:08:05 (b6087b70): Entering config_set_status
2010-10-24 18:08:05 (b6087b70): Exiting config_set_status
2010-10-24 18:08:05 (b6087b70): Session 0: Streaming file '02 - Underneath.flac' to 192.168.200.142 (offset 0)
2010-10-24 18:08:05 (b6087b70): Transcoding /media/multimedia/audio/hudba/Alanis Morissette/2008 - Alanis Morissette - Flavors Of Entanglement/02 - Underneath.flac with ssc-ffmpeg/svn-1696
2010-10-24 18:08:05 (b6087b70): opening /media/multimedia/audio/hudba/Alanis Morissette/2008 - Alanis Morissette - Flavors Of Entanglement/02 - Underneath.flac
2010-10-24 18:08:05 (b6087b70): opening file raw
2010-10-24 18:08:05 (b6087b70): Updating Content-Type from application/x-dmap-tagged to audio/wav
2010-10-24 18:08:05 (b6087b70): Updating Connection from close to Close
2010-10-24 18:08:05 (b6087b70): Emitting reponse header Expires: -1
2010-10-24 18:08:05 (b6087b70): Emitting reponse header Cache-Control: no-cache
2010-10-24 18:08:05 (b6087b70): Emitting reponse header DAAP-Server: firefly/svn-1696
2010-10-24 18:08:05 (b6087b70): Emitting reponse header Accept-Ranges: bytes
2010-10-24 18:08:05 (b6087b70): Emitting reponse header Content-Language: en_us
2010-10-24 18:08:05 (b6087b70): Emitting reponse header Content-Type: audio/wav
2010-10-24 18:08:05 (b6087b70): Emitting reponse header Server: mt-daapd/svn-1696
2010-10-24 18:08:05 (b6087b70): Emitting reponse header Connection: Close
2010-10-24 18:08:05 (b6087b70): Emitting reponse header Date: Sun, 24 Oct 2010 16:08:05 GMT
2010-10-24 18:08:05 (b6087b70): Channels.......: 2
2010-10-24 18:08:05 (b6087b70): Sample rate....: 44100
2010-10-24 18:08:05 (b6087b70): Bits/Sample....: 16
2010-10-24 18:08:05 (b6087b70): Swab...........: 0