Features
How to create a Linux iPlayer download GUI using Zenity

Copyright © TechPad.co.uk
Here's how I created a simple graphical utility to download TV and radio programmes from the BBC's iPlayer site using Zenity, Bash and iplayer-dl.
I've been using Paul Battley's excellent iplayer-dl Ruby script to download TV and radio programmes from the BBC's iPlayer site ever since it was released.
It's a fantastic little tool, and allows you to download and save the DRM free iPhone versions of BBC content to watch on your iPhone when you're away from a Wifi connection, or save on your machine, just as you would with a recording made using your olde worlde VHS video recorder.
However, the command line application is a little cumbersome, and I've been entirely unsuccessful in getting Paul's wxruby powerered GUI for iplayer-dl up and running on Ubuntu.
So, I decided to knock up a really simple example myself using some shell scripting and Zenity.
Zenity is a GUI application for shell scripting that comes installed by default on most Gnome-based Linux distributions, and it's dead easy to use.
To get started, the first thing you'll need to do is to install iplayer-dl. There's a tutorial here on installing iplayer-dl, aimed at OS X users, but it's the same process on Linux.
Step 1
One you've got that installed, and have confirmed that it's all working properly, you'll then need to knock up your shell script. This will make a GUI that allows you to graphically enter the details on the download, and then creates and runs the iplayer-dl command without the need to touch the command line each time.
Fire up a text editor such as Gedit, and add #!/bin/bash on the first line to load Bash when the script is run.

Step 2
The first thing we want to do is get the user to tell us the URL of the programme on the iPlayer site that they want to download. To do that, we need to add the following line to the script:
iPlayerURL=$(zenity --entry --text "$MessageEnterUrl" --title "$WindowTitle" --width 500);
This creates a variable called iPlayerURL and then uses Zenity to create a text entry box. It makes this 500 pixels wide and adds a title to the window and a bit of text to the box itself, which tells the user what to do.
Here's the full script, so far.
Save the script as iplayergui.sh, open a terminal, cd to the directory containing your script and enter sh iplayergui.sh and you should see a Zenity text entry box, which allows you to enter the URL.

Step 3
Now we've got the URL, we want to ask the user where they want to save the programme file to on their machine, and for that we're using the Zenity --file-selection option with the --directory flag.
To do this, you need to add the following line below the last bit you added:
Directory=$(zenity --file-selection --directory --title "$WindowTitle" --width 500);

Re-run your Zenity script by typing sh iplayergui.sh, enter some text in the URL field and then hit OK. You should now get the familiar Gnome directory selection dialog box.
The line added simply creates a variable called Directory and assigns the directory the file-selection dialog selected. We can then use the variable in a later step.

Step 4
The iplayer-dl gives you a number of other features, such as the ability to download a signed version of the programme. To add an extra step asking the user whether they want the version with sign language or the original version, you need to use the radiolist feature of Zenity.

Run through the script again and you should see an additional step at the end giving you the option to download the original or the signed version of the programme.
Step 5
Once we've got these bits of information, and have assigned each of them to a variable, we can populate the command we need to pass to iplayer-dl to get it to download the programme.
To do this, add the following line to your shell script.
iplayer-dl $iPlayerURL -d $Directory -s --type-preference=$TypePreference

Run this for real now by entering the URL of a programme you want to download and running through the steps. You should find that it downloads the programme to the directory you selected.
Step 6
That's handy, but it doesn't give any feedback on how far into the download you are, or when the download has completed.
To add a nifty little progress bar, change the iplayer-dl line to the following: iplayer-dl $iPlayerURL -d $Directory -s --type-preference=$TypePreference 2>&1 | zenity --progress --title "$WindowTitle" --text="$MessageDownloading" --width 500 --auto-kill --auto-close

Run the script again and you should find that you get a lovely little progress bar, showing how long the download has left to go.

Step 7
To make it just that little bit better, we can add a message at the end of the script to show a message telling the user that their download has finished. Add this line:
zenity --info --title "$WindowTitle" --text "$MessageDone"
exit

Run the script and you should now get a status message at the end when the script has finished your download.

Step 8
One thing you'll notice about the script now is that the cancel buttons don't yet do anything. To make them work, you need to add an extra bit of code beneath each call to Zenity.
if [ $? = 1 ];
then exit
fi
Zenity uses the $? variable to hold the value of the OK and Cancel buttons on its forms. The cancel button has a value of 1, while the OK button has a value of 0. This if statement basically exits the script if the cancel button has been clicked.
Step 10
As iplayer-dl is a prerequisite for running the script, it would be neat if we could check that it's installed, and provide an error message and halt execution if it's not. This bit of code needs to go at the top of your script.
if [ $(basename `which iplayer-dl`) !="iplayer-dl" ];
then zenity --title "$WindowTitle" --error --text "$MessageNotInstalled"
exit
fi
It uses the 'which' command to check to see whether iplayer-dl is installed. If it isn't, it spawns a Zenity error message contain the text we've stored in the variable $MessageNotInstalled.
Step 9
As a final finishing touch, I've created a shortcut to my shell script from the Gnome taskbar. Simply right click on the taskbar and select "Add to panel", then click "Custom Application Launcher".

Enter the location of your shell script, pick an icon and give the application a name. And there you go, a dead easy to make, graphical user interface to iplayer-dl allowing you to download videos from the BBC iPlayer site and watch on your mobile, iPhone or PC.
All you need to do now is go to the iPlayer site, copy the URL of the programme you want to download, and then click your shortcut icon and enter it in there.
Get the script
To download the full script, click here.
Published: TechPad.co.uk Monday 29 December 2008, 9:43 pm
Views: 10,352 times
Filed under: Zenity iPlayer shell scripting Linux Ubuntu
Login to leave your comments
Please login





