aoakley.com

Hacky Scripts: GetRAM and LoadGuest/SaveGuest

Presented at Gloucestershire Linux User Group 17 February 2009 by Andrew Oakley. This was an evening of show-and-tell "Hacky Scripts"; small utility scripts that the speakers had developed themselves and now couldn't live without, but which lacked polish for general release to the public. As such you may find these scripts half-finished, lacking functionality and documentation. The talks aimed to inspire people and spread ideas, rather than present fully-finished products.

OpenOffice Presentation:

  • 2009-02-17-hacky-scripts.odp - OpenOffice presentation
  • 2009-02-17-hacky-scripts.pdf - PDF
  • HTML below.

    Script source codes:

  • GetRAM - Bash script to convert RealAudio stream to MP3
  • SaveGuest - Bash script to save a Gnome guest desktop session, save in /usr/local/sbin/ and run as root
  • LoadGuest - Bash script to load a Gnome guest desktop session, resetting a guest account to your desired defaults rather than system defaults, save in /usr/local/bin and call from /etc/gdm/PostLogin/Default

    Hacky Scripts

  • GetRAM
  • LoadGuest/SaveGuest
  • www.aoakley.com
  • www.gloucs.lug.org.uk
  • www.dircaster.org

    GetRAM Aims

  • Original problem: I wanted to record BBC Radio 4 and BBC7 programmes which were:
  • Original solution:
  • Now use this to download radio recordings over 3G on the bus using Nokia Podcasting on N95 Symbian phone with Bluetooth headset

    GetRAM

  • Bash script combines Mplayer, LAME and mp3info
  • Mplayer converts .RAM RealAudio stream (eg. BBC Radio 4) into .PCM/.WAV file
  • LAME converts .PCM/.WAV into MP3
  • Mp3info tags MP3 file
  • Use crontab to set recording start times
  • Length of recording passed as parameter
  • DirCaster.org PHP script creates XML RSS podcast feed based on MP3 tags

    GetRAM Source Code

    #!/bin/bash
    ..lots of setup snipped...
    # Dump the RealAudio stream
    # Using -t for timeout after N seconds?
    if [ "$timeout" == "0" ]; then
      mplayer -nojoystick -nomouseinput -nolirc -quiet $bandwidthflag -cache 128 -vc null -vo null -dumpstream -dumpfile $filename.rm $ramfile
    else
      mplayer -nojoystick -nomouseinput -nolirc -quiet $bandwidthflag -cache 128 -vc null -vo null -dumpstream -dumpfile $filename.rm $ramfile &
      playerpid=$!
      sleep $timeout
      kill $playerpid
    fi
    # Convert the RealAudio stream to a PCM file
    mplayer -nojoystick -nomouseinput -nolirc -quiet -ao pcm:file=$filename.pcm -vo null $filename.rm
    if [ "$mono" == "y" ]; then
      # Convert to a mono MP3
      lame -h -b $monorate -m m $filename.pcm -o $outfile
    else
      # Convert to a stereo MP3
      lame -h -b $stereorate -m s $filename.pcm -o $outfile
    fi
    # Delete temporary files
    rm -f $filename.ram
    rm -f $filename.rm
    rm -f $filename.pcm
    # Create MP3 ID3 tags
    mp3info -a "$artist" -l "$album" -g "Speech" -t "$filename" -y "`date +\"%Y\"`" $outfile
    echo "Saved as $outfile - done"
    

    GetRAM Command Line

    getram [-d] [-m] [-t N] http://site/path/filename.ram [outputfilename]

    -d datestamps yyyymmdd-outputfilename

    -m mono 24 kbit/s (default is stereo 96 kbit/s)
    Also forces RealAudio client to report 33.6Kbps bandwidth which will encourage RealAudio server to switch client to low-bandwidth stream

    -t timeout (stop recording) after N seconds

    outputfilename is optional. If not given, it will create a default filename YYYYMMDD-HHMMSS-NNNN.mp3 where NNNN is unique-ish based on nanoseconds.

    Input URL may be a .ra or a .ram URL. If it is a .ram URL, it will attempt to determine the .ra URL.

    MP3 tags for artist, album and genre are constants in the script. MP3 tag title is exactly as the outputfilename .

    GetRAM Usage Ideas

    For example, a crontab line to record part of The Today Programme on BBC Radio 4, in low bitrate mono, every Monday through Saturday from 7:59am for 35 minutes, and overwrite the previous recording:

    #m hh dd mo day command
    59 07 * * 1-6 /usr/local/bin/getram -m -t 2100 "http://www.bbc.co.uk/radio4/realplayer/media/fmg2.ram" R4-Today-8am.mp3

    To record Paul McGann as the Eighth Doctor on BBC7, in medium bitrate stereo, for an hour plus a minute padding each side, on Saturday evenings at 6pm, and datestamp the recording for archival:

    59 17 * * SAT /usr/local/bin/getram -d -t 3720 "http://www.bbc.co.uk/bbc7/realplayer/dsatg2.ram" R7-Doctor-Who.mp3

    GetRAM Usage Tips

  • When a series finishes on BBC Radio 4 and BBC7, they tend to start a new series of a similar genre in the same time slot. So you might wish to use generic filenames such as "R7-Saturday-Sci-Fi.mp3" or "R4-Friday-Satire.mp3" instead of specific programme titles such as "Doctor Who", "The Now Show" or "The News Quiz".

  • You can find the RealAudio stream URL by using the BBC's "Listen Live" feature. If this defaults to a Flash player, you can select "Dial-up modem" or "Low bandwidth" and then select RealPlayer. From there, right-click and select "Play in Realplayer", then use the menu to select Properties and copy the URL. Alternatively... view source!

  • GetRAM works with any RealAudio stream, not just BBC.

    GetRAM To Do List

  • Out-of-order parameter parsing (currently, parameters must be in order)
  • Artist, album and genre parameters
  • Working --help text and man page
  • .deb and .rpm install files with correct dependencies
  • Script to auto-generate crontab entries based on selections from Radio Times XML schedule
  • WAP or lightweight HTML interface to schedule a recording from a mobile device

    SaveGuest / LoadGuest

  • Guest account must reset back to defaults and forget any changes after use
  • Ubuntu guest account uses Gnome defaults- not very user friendly for Mac or MS Win users
  • Need guest account that, for example:
  • Has settings that get recreated if deleted
  • Has more familiar MS Windows-like taskbar
  • Always loads Firefox even if session changed
  • Has your choice of defaults

    LoadGuest / SaveGuest

  • Script uses "visitor" username by default but any username can be passed as parameter
  • "guest" username is (partially) reserved by Ubuntu 8.10 onwards
  • Set up the Gnome account as you desire
  • Then log out and run SaveGuest as root
  • Visitor home directory is tar.gzipped
  • LoadGuest goes in /etc/gdm/PostLogin/Default and is run on every GDM login

    SaveGuest source code

    #!/bin/bash
    username=$1
    if [[ "$username" == "" ]]
    then
      username=visitor
    fi
    if [[ -d /home/$username ]]
    then
      cd /home
      mv -f $username.tar.gz $username-old.tar.gz
      tar cvzf $username.tar.gz $username --exclude="*/.thumbnails/*" --exclude="*cache*" --exclude="*Cache*"
      chmod 664 $username.tar.gz
    else
      echo "/home/$username does not exist or is not a directory"
      echo "Usage: saveguest [username]"
      echo "Assumes username 'visitor' if no username supplied"
    fi

    LoadGuest source code

    #!/bin/bash
    # Goes in /etc/gdm/PostLogin/Default:
    # #!/bin/bash
    # if [[ "$LOGNAME" == "visitor" ]]
    # then
    # /usr/local/bin/loadguest
    # fi
    username=$1
    if [[ "$username" == "" ]]
    then
      username=visitor
    fi
    if [[ -f /home/$username.tar.gz ]]
    then
      cd /home
      rm -rf $username/*
      tar xvfz /home/$username.tar.gz
    else
      echo "/home/$username.tar.gz does not exist or is not a regular file"
    fi

    Public Domain - Andrew Oakley - 2009-02-17

    Top - More Broadcasting Articles - More  Computing Articles - Article Index - aoakley.com