Watch and record live TV on your Raspberry Pi
18th May 2013 | 09:00
Forget a PVR, go for Pi!
Many of those set-top boxes hidden under televisions are already running Linux. And despite their lack of CPU power, they're all more than capable of recording and playing several channels at the same time, as well as streaming the data across your local network.
The Raspberry Pi is perfectly suited to this, too, and with the appropriate hardware it can be turned into a powerful low-cost digital video recorder, complete with media streaming, scheduling and time shift.
The appropriate hardware is the key phrase in the previous paragraph, because a painless installation is mostly dependent on your television-grabbing hardware 'just working'.
Fortunately, Linux has support for a great many such devices built in to the kernel, so many will work without modification.
And while these instructions start from the command line, we've split the entire tutorial into 10 different steps, hopefully making the project as easy to follow as possible.
At the end of this project, you'll find yourself with a fully-fledged digital TV recording platform, capable of recording multiple programmes from multiple sources, all running from the humble Raspberry Pi. It's the perfect backend for the just-released XBMC, which you'll be able to use as a front-end from any other computer on the same network.
1. Our hardware
We tested and configured two USB receiver devices, one for grabbing terrestrial digital television through an aerial and another for grabbing the data from a satellite feed. We'll include instructions for both.
For DVB-T (terrestrial) reception, we used a Sundtek MediaTV Pro, for DVB-S (satellite) reception, we used the Sundtek SkyTV Ultimate. The latter includes a 12v power adaptor that also needs to be connected.
But here's the most important requirement: these USB devices must be attached to the Raspberry Pi through a powered USB hub. We wasted two days trying to configure the system, firstly without a hub and secondly with an incompatible hub. In both cases, everything appeared to work but the devices wouldn't find any television channels in a scan. Switching to a powered hub compatible with the Raspberry Pi solved the problem, so we can't emphasise this point enough.
Plug a hub into a power supply, connect your USB receiver to the hub and the hub to the Pi. And don't forget to connect the aerial or satellite feed to your receiver.
2. External storage
We're assuming you've got a Raspberry Pi pre-configured and updated with the Raspbian distribution. We're also assuming it's connected to the internet and that you're typing your commands into the console directly or over an SSH session.
Our next consideration is going to be where you store the television recordings. We'd recommend connecting an external USB hard drive, as the constant read/write access will test the average SD card to its limits.
To add storage like this, simply plug the device into a spare USB port and check the output from the system logs by typing tail /var/log/ messages. You'll see output similar to usb 1-22.214.171.124: New USB device, and you'll need to look for the device identifier, which should look something like sda: sda1 – sda is the device itself, while sda1 is a partition.
Type sudo mkdir /mnt/storage to create a mount point and sudo mount /dev/sda1 /mnt/storage/ to connect it to your external device.
3. Install the drivers
Depending on the television hardware you're using, this step might be unnecessary. If you've chosen a device that's compatible with Linux and requires no additional driver files, then you can simply plug in your device and move on to the next step.
For our Sundtek devices, we need to download and install a driver. This is easy. From the Raspberry Pi command line, type:
chmod 777 sundtek_netinst.sh
The final line will execute the script that's downloaded in the first line. It will then detect the system you're running and install the latest version of the drivers. It will leave the drivers running and configured to launch at boot.
For users of the DVB-T version, you will also need to execute this command:
This will ensure the card is configured for terrestrial reception, rather than a 'cable' source, which the device is also capable of.
4. Install Tvheadend
The piece of software we're going to use to record and stream the digital television signal is called Tvheadend. There's a plugin for the just-released XBMC that will turn this awesome media player into a fully-fledged digital video recorder, with Tvheadend doing the hard work in the background from your Raspberry Pi.
Because Tvheadend is a tool that's constantly being developed, we used the development version, but you could just as easily use the OpenElec distribution instead of Raspbian if you wanted to skip this step.
Fortunately, building it is easy. First, install the development and DVB tools:
sudo apt-get update
sudo apt-get install unzip libcurl4-openssl-dev pkg-config git build-essential dvb-apps gcc-4.7
The final step is to download the latest version of Tvheadend from the developer's repository using git, and to build this using the build trinity of ./configure, make and sudo make install:
CC=gcc-4.7 ./configure; make; sudo make install
5. Configuring Tvheadend
Tvheadend's user interface is accessible through a web browser, but first you need to start it. As this is the first time, we're going to run the service in Configuration mode, and as a daemon, which means it becomes a background process. To do this, type the following:
tvheadend -C -d
Now open a browser, preferably from another machine on the network, and enter http://ip_of_rpi:9981/extjs.html
You can discover the IP address of your Raspberry Pi by typing ifconfig and looking for the value next to the 'inet addr' field for the 'eth1' device. Your browser will load the default frontend to Tvheadend. This is where you'll eventually see all your program data and set up and view recordings.
We need to tell it how to use the TV hardware we've connected. This can be done by clicking Configuration > TV Adaptors and selecting your device from the drop-down menu on the left. As the next steps are going to depend on whether you're receiving a satellite signal or a signal through your aerial, we're going to briefly separate the next steps. For satellite receivers, carry on. For terrestrial reception, jump to step 7.
6. Satellite reception
Satellite reception is easiest to configure. With the adapter selected, the General page will show an overview of the configuration. First, click on the Enable tick box, followed by Save. We want to add some channel information, and this is done by adding the data for finding a satellite and the multiplexes it provides and then searching these multiplexes for channels that can be received.
Tvheadend bundles the data for satellite location, so you just need to click on the Add DVB Network by location button on the left. This will open a window containing a global list of satellites. For the UK and Northern Europe, your best option is Astra_28_2E.
After selecting the satellite, Tvheadend will add a list of multiplexes associated with that satellite to the Multiplexes tab. These will now be scanned for channels, and you can watch the scanning progress in the Capabilities box on the right of the General page.
If everything is working, you should see many services (channels) detected and added to the Services page. For Astra_28_2E, we detected 970 services from 98 muxes. Unless you want to also configure your system for terrestrial viewing, skip to step 8.
7. Terrestrial reception
First, enable the receiver in the general page. Tvheadend includes a broad database of transmitters pre-configured with the details for each multiplex. You'll need to know which transmitter your aerial is pointing at.
In the UK, we've found that www.digitaluk.co.uk/postcodechecker is the best site for telling you which transmitter you're likely to be using. When you know, you simply need to click on the Add DVB Network button from the adaptor's General tab and find the transmitter 'By location'.
Our local transmitter wasn't listed. The solution was to go to www.ukfree.tv/transmitters.php, enter our postcode, and add the details for each multiplex manually. This can be done from the Multiplexes tab for the adaptor by clicking on Add mux(es) manually. In the window that appears, you will need to enter the frequency, bandwidth and constellation for each multiplex, while setting everything else as Auto. This data can be gleaned from the website.
You'll typically need to do this for three or four muxes, depending on your location. Tvheadend will scan them for services/channels and add them to your configuration.
8. Adding channels
You've hopefully got a healthy list of services extracted from the multiplexes. The next step is to allow Tvheadend to divine TV channels from those services, and this can be done from the adaptor's General tab by clicking on the Map DVB Services To Channels button. You'll hopefully be left with a list of channels to watch in the Channels page outside of the adaptor configuration area.
The default location for recordings will need to be changed - probably to the mount point we created at the beginning. The location can be changed by selecting the Digital Video Recorder page and changing the Recording System path. You must save the configuration on this page for any changes to take effect.
You will also need to configure the electronic program guide. There's usually a minimum of What's On Now and What's On Next embedded within each channel, but services such as Freeview and Freesat in the UK transmit a more comprehensive seven-day EPG. These can be enabled by selecting the EPG Grabber page, followed by clicking on either service in the Over-The-Air-Grabbers section. Don't forget to click on Save Configuration.
9. Record a program
After a while, you should notice the Electronic Program Guide page starts to populate itself with the broadcasts you can now watch or schedule to record. Clicking on any program will open another window, allowing you to set up a recording.
A secondary option, labelled Autorec, is more interesting. It sets up a search based on the same program data so you can record a full series without relying on series link data being embedded within the EPG. Depending on the number of channels and the amount of EPG data, this view can get unwieldy quickly.
To solve this, you can filter what's shown using the row of options at the top of the list. You can search for part of a title, or limit the list to a single channel or filter tag. If you find a filter you like, clicking on the Create Autorec button will add that search to Tvheadend, which will then record everything it finds that matches the search.
To remove scheduled recordings, click on the Digital Video Recorder tab. Upcoming recordings can be removed from the first page, while Autorec filters can be removed from the last. The centre pages can be used to play or delete recordings that have been made, or check why a recording might not have worked.
10. Watching recordings and live TV
By installing a VLC plugin into your browser, you can watch your recordings and live TV in your browser. We've only tried this feature in Firefox, but when you click on a program that's being broadcast, you get the option to Play. If the VLC plugin isn't installed, you'll be asked if you want to install it.
With the plugin installed, an embedded window appears showing the program. With the controls at the top of this window, you can watch full-screen or pause the current broadcast. You can watch programs you've recorded in the same way from the Digital Video Recorder page.
If you'd rather not use a browser, drag and drop the network URL into VLC on a different machine, or read one of our file sharing tutorials to learn how to share the recordings folder across your network.
However, in our opinion the best way of using Tvheadend is with the new version of XBMC. It includes a plugin that can talk with Tvheadend directly, downloading the EPG from your Raspberry Pi, and enabling you to watch live channels, and schedule and watch recordings. It's simple to set up - just visit XBMC's PVR plugin page; but as we're planning on covering XBMC in plenty of detail next month, we'll leave this as a piece of homework!