Posts Tagged ‘transcode’

Automatically Convert Radio Shows To MP3 Format

Sunday, February 15th, 2009

It’s great to be able to record radio as well as TV channels using MythTV but to listen to them on an MP3 player they need to be transcoded to MP3 format. The process can be completely automated by setting up a User Job which runs after the show has recorded.

There are four main steps needed to get this going:

1. Install FFmpeg with MP3 encoding support
There’s a great guide for installing FFmpeg with MP3 encoding support here. Just follow it through making sure you use the suggested –enable-libmp3lame switch when running ./configure in step 5.

2. Install libid3-3.8.3-dev for automated ID3 tagging
This library will allow the MP3 encoding script to place ID3 meta tags into the resulting MP3.  Info like artist, song and year is set based on the show you are recording.

sudo apt-get install libid3-3.8.3-dev

3. Setup conversion script and MythTV User Job
The script below is the one that is executed by the User Job when the show finishes recording. It is based on this script from the MythTV Wiki but I’ve modified it to work with the latest version of FFmpeg and tag and name the resulting MP3 in my preferred format.

#!/bin/bash
 
###########
#
# Suggested execution format in the job settings for the backend (in general of the backend setup)
# Remember to allow the job to run in the backend general settings too :)
#
# autoaudio.sh %FILE% %STARTTIMEISO% "%TITLE%"
#
###########
 
OUTPUTDIR="/media/recordedtv/transcoded"
INPUTDIR="/media/recordedtv"
INFILE=$1
ISODATE=`date +%F`
PROGTITLE=$3
STARTDATE=${2:0:10}
 
# Split
###########
OUTFILE="$OUTPUTDIR/$PROGTITLE - $STARTDATE.mp3"
 
#transcode mp2 audio to mp3
ffmpeg -i "$INPUTDIR/$INFILE" -acodec libmp3lame -ab 192k -ar 44100 -f mp3 "$OUTFILE"
 
# Tag
##########
YEAR=`date +%Y`
id3tag --artist="$PROGTITLE" --song="$PROGTITLE - $STARTDATE" --comment="" -y$YEAR -A"Radio" "$OUTFILE"

Copy the script above and put it in a new file in your home directory called autoaudio.sh. For me this was /home/colin/autoaudio.sh. Change the “OUTPUTDIR” and “INPUTDIR” paths to match your setup. “OUTPUTDIR” will be where the MP3 files are created and “INPUTDIR” should be the path to the mpg files of your recorded shows.

Remember to make the script executable.

chmod 777 autoaudio.sh

Also, make sure the MythTV user can write to the output path.

4. Setup the user job
Go into the mythbackend setup and from the menu choose “General” and go to the last screen in this section titled “Job Queue (Job Commands)”. Enter a new job with the description of “Convert to MP3″ then enter the command as below changing the path to the location of your autoaudio.sh file. For me this is

/home/colin/autoaudio.sh %FILE% %STARTTIMEISO% "%TITLE%"

Choose “Finish” so you are back at the mythbackend main menu. Now we need to allow the job to run, so go back into “General” and scroll through to the screen titled “Job Queue (Backend-Specific)”. Tick the box for “Allow Convert to MP3 jobs”. Click “Next” and “Finish” until you are back to the main menu.

All Done
That’s it, you now have a user job which will convert shows to MP3, let’s check it works. I use MythWeb to manage things most of the time. In MythWeb, go to “Recorded Programs”, Click on a show title, then under “Queue a job” click the “Convert to MP3″ button. If all went well you should see an MP3 file created in the output directory you specified in autoaudio.sh. There may be a delay between clicking the button and an MP3 being created depending on how often your mythbackend is set to run user jobs. If nothing is created check the mythbackend logfile at /var/log/mythtv/mythbackend.log for errors.

Once it’s all working it can be activated on an existing recording schedule. In MythWeb, go to “Recording Schedules”, click on a schedule you have setup and look under “Advanced Options”. You should see a tick box for “Convert to MP3″. The show will now be converted to MP3 every time it records.