Tuesday, October 21, 2014

Raspberry Pi DVR - tvheadend

Now that the final configuration of the Raspberry Pi is complete, it is time to install and configure tvheadend.  I felt this process was complex enough to warrant a dedicated post.

Installation:
Installing applications on most Linux distros is very straight forward, you issue a command which downloads and installs the package from the repositories on the Internet.  Unfortunately, the tvheadend package is not a part of the Raspbian repositories, so you can either download the source code and compile it yourself or you can manually connect to another repository.  I went with the latter, specifically using the repository maintained by tvheadend themselves.

First run the command "sudo curl http://apt.tvheadend.org/repo.gpg.key | sudo apt-key add -"  This adds the tvheadend GPG signing key to your system.  Next run "sudo nano /etc/apt/sources.list"  Append "deb http://apt.tvheadend.org/stable wheezy main" to this file.  Lastly, run "apt-get update" and "apt-get install tvheadend"  This will install and start the tvheadend server.  Installation will prompt you for a user account.  You can use the default Raspbian login, or use another account you previously created.

Configuration:
Now that tvheadend is installed we can configure it.  I wish I could say that tvheadend is easy to configure... but it is not.  I spent a lot of time learning the ins and outs of tvheadend before I was finally able to get it to work.

First you need to open a web browser and navigate to "<server>:9981" where <server> is the IP address of your Raspberry Pi or the name (if you enabled Samba).  You can use Epiphany from the Pi itself, or browser on another machine.

You may be prompted for a username and password, if so enter the credentials above used during the installation of tvheadend.  Once tvheadend is displayed, click on Configuration | DVB Inputs | TV Adapters.  From the combo box select your TV tuner.

If you do not see your TV tuner in the combo box then chances are you forgot to download the firmware file into /lib/firmware.  Consult the Linux TV Wiki for more info.

Make sure the device is enabled (checkbox in the middle) and click Save.  Next click on the "Add DVB Network by location" button.  Select the TV signal you wish to add.  For digital over-the-air broadcast TV in the U.S. select "us_ATSC_center_frequencies_8VSB."

After doing this, the right side of the screen where is says "Muxes awaiting initial scan" should read something like 68.  This will slowly countdown to 0.  Although there is little feedback, tvheadend is scanning all possible channels looking for signals.  This will take time - 10, 15 minutes, or more.  When that number reaches 0, hopefully the "services" and "muxes" above will read something other than 0.  That means it found some channels.  If "services" and "muxes" still read 0, then you need to check connections.  Is the antenna connected to the TV tuner?  If the antenna in a good location?

Once you have found services and muxes you are ready to map to channels.  Here is where things get really painful.  You can click the "Map DVB services to channels" button, but in my experience this does nothing.  Instead, we need to manually map these channels.  Here is how.  First, click on Configuration | Channel/EPG | Channels.  Next click "Add channel" and give the channel a name, something like "NBC 6-1."  You should also give it an integer number, but as far as I can tell this number means nothing.  Be sure and click "Save changes" after adding the channels.  Here is a screenshot showing manual entries for all the channels in my area.

Now that you have manually created the channels, you need to manually map them to the services found in the prior scan.  Click on Configuration | DVB Inputs | Services.  All found services should be listed, but the Channel name column should be blank.  For each and every row, double-click on the Channel name column.  This should open up a combo box and allow you to select one of the channels you manually created.  But how do you know which channel to select?  You basically need to do a double-mapping, manually of course (welcome to the world of Linux, do as much of the leg work yourself).  Every digital broadcast channel in the U.S. has something called a physical and virtual channel.  The virtual channel is "6.1" but the physical channel might be 15.  You can use antennaweb.org or tvfool.com (or probably a host of other sites) to get physical and virtual channels for all stations in your area.  The second mapping is physical channel to frequency.  A simple google search will show frequencies in MHz for all physical channels.  Now you are ready to map channels.  For each row there is a Multiplex column.  This shows the frequency for the channel.  Map this frequency to the physical channel, then map that physical channel to the virtual channel, then select that channel in the Channel name field.  After making all these changes, do not forget to click Save changes.  If this step is too complex, just use trial and error.  Set a channel to something, anything, then watch that channel.  Once you know what it really is, go back and change it.

You are getting very close now to completion.  Next click on Configuration | DVB Inputs | TV Adapters.  From the combo box select your TV tuner.  In the settings in the middle, make the following changes, be sure and click Save when you are done.

  • Enable "Skip initial scan."  This will prevent tvheadend from scanning for new channels every time the system reboots.
  • Disable "Idle scanning."  This will prevent tvheadend from continuously scanning in the background for new channels.
  • Enable "Close device handle when idle."  Without this tvheadend keeps the TV tuner active 24/7 which consumes a lot of electricity plus the TV will get really hot.  With this enabled the TV tuner is put into standby after each use.

The final piece of configuration can be found by clicking on Configuration | Recording | Digital Video Recorder.  Make the following changes, and be sure to click Save configuration when done.
  • For the recording path I entered "/mnt/nas/Videos"  This is where I want recorded videos to live, over the network on my NAS box.
  • Change Media container to "Same as source (passthrough)."  This saves the broadcast stream to a video file without any changes (called transcoding).  Since the Raspberry Pi does not have a powerful processor, it is unlikely it could transcode on the fly in real time.
  • I also checked boxes at the bottom control the output filename.


There, that was not so bad, was it?  Ok, I admit, that was a major pain in the butt!  Getting tvheadend is a pain to setup, but once it is done, it works pretty well.  Again, welcome to the world of Linux, expect more effort on your part, and less user-friendly compared to a typical Windows application.

Now that tvheadend is configured, you can set it to record TV programs.  To do that login to the above tvheadend configuration page.  Click on Digital Video Recorder | Upcoming Recordings.  Click Add entry and select channel, date, and time for the recording.  This is just like programming an old VCR.  You tell tvheadend to record channel 6.1 from 8:00 to 9:00 for example.  It uses 24-hour notation, so 5pm is represented as 17:00.


One area I would like to improve here is the use of EPG (electronic program guide).  This is a digital TV guide showing you what is on each channel at a given time.  You can simply click on a show to record it.  Or tell tvheadend to do things like "record the Simpsons" and it will records all episodes of the Simpsons, regardless of channel or time.  I have not configured EPG, for now I am doing everything manually.  EPG would be nice, but frankly after all this, I am just glad it is working.  So maybe in the future I will decide to tackle EPG.

9 comments:

  1. Thanks for the this! My project is to use the HDHomerun as my tuner, the Raspberry Pi B+ as my PVR server and then watch on xbox 360...I'll let you know how it goes...

    ReplyDelete
  2. Greetings:

    I LOVE what you've done here!!

    I followed your build pretty closely and my DVR has been working well for a couple of months now.

    However, I have a few questions:

    1. In the instructions above, you say "Enable "Close device handle when idle." Without this tvheadend keeps the TV tuner active 24/7 which consumes a lot of electricity plus the TV will get really hot. With this enabled the TV tuner is put into standby after each use." -- I've got this set, but I can't tell that my tuner is being placed into standby mode. Shouldn't the light on the tuner go out, dim, or change color to indicate the change?

    2. When recording back-to-back programs (say 7:00 to 8:00 and then 8:00 to 9:00), the 8:00 program often gets missed. Sys log errors indicate the issue may be timing, as the 8:00 program seems to be attempting to grab control of the tuner before the 7:00 program has stopped recording. Suggestions?

    3. Any advise for upgrading the version of TVHeadEnd? The current/latest version for the Pi is 3.4~Wheezy. On your raspberrypi.org forum post, that got me started on this, at least one of the posters indicated they'd built the 3.9.1987 build and it seemed better. Thoughts?

    Thanks, and keep up the good work!!!

    Neil

    ReplyDelete
    Replies
    1. Thanks for the feedback. Glad my blog helped you out.

      1) One way to check if the TV tuner is in standby is to log into tvheadend with a web browser. At the top click on the "Status" tab. At the bottom is a signal strength meter. If it reads 0% then the tuner is in standby. If it shows a percentage other than 0%, then the tuner is active. Note, it will take a few minutes after recording a show before tvheadend will will go into standby.

      2) Unfortunately this is a problem I've run into a few times as well. I rarely record things back-to-back so it's usually not a problem for me. There might be an official solution out there, but for now I would suggest separating them by a minute or two. End the first early and/or start the next late. Not ideal, but it should work.

      3) I too want the latest version, but I believe the only options are A) download the source code yourself and compile it or B) hope that someone on the Raspbian team compiles it and adds it to the repositories. As much as I want a newer version, at the same time tvheadend was a pain to get set up and it's working, so I'm afraid to mess with it.

      Delete
  3. Thanks for the responses!

    1. OK, mine shows 0%, so I guess it's good.

    2. The ability to separate them by a minute or two doesn't seem to exist.

    3. Agreed. Moan...

    Thanks, again!!

    Neil

    ReplyDelete
  4. Hello, and thanks for the tutorial. I don't have a Pi, but I installed OpenELEC 5.0.3 on an Android box, it already had tvheadend server and drivers for my HDHomeRun tuners built in, so it was pretty easy to get tvheadend set up. With the newer version of tvheadend, 3.9.2182, it seems simpler than the version you have demonstrated here. I have 2 HDHomeRun tuners, so I have 4 tuners. I have never had a problem of loosing a recording back to back. I got the epg by using mc2xml to create my xmltv files for my 14 days of guide data.

    Here are a couple of videos showing what my setup looks like.

    https://www.youtube.com/watch?v=5yRp0j-6xmE
    https://www.youtube.com/watch?v=TU_hJBmfm7o

    ReplyDelete
  5. Hello,

    I set up tvheadend on my pi following your blogpost and I'm experiencing this issue where it is causing my system to crash. When I check the syslog after rebooting, the last line before the crash is ALWAYS "tvheadend2338: epggrab: module xmltv created" (with a different pid of course). This doesn't happen if I have the tvheadend service turned off. My setup is nearly identical to yours except I'm connected via ethernet and I'm using the newest Raspberry Pi 2 B+.

    I'm not sure if you can help me but I greatly appreciate any insight you could lend (I'm pretty noob).

    Thanks!

    ReplyDelete
    Replies
    1. I wish I could offer help here, but I'm very far from a Linux expert. I can say that "epg" from your log refers to Electronic Program Guide. So if you enabled this in tvheadend you might want to try and disable it. The problem could be related to yours being a Pi 2. I recently got my first Pi 2 but haven't started using it yet. It I run into similar issues I'll post my findings.

      Delete
  6. Hello, I have followed your guide entirely and I do not seem to have the TVHeadEnd scanning anything. I have added the network and Force Scan, but it just sits there....for 2 hours now, with 0 of 68 scanned. What am I doing wrong??

    ReplyDelete
    Replies
    1. Ahh, forgot to choose the Networks to associate it with in the Basic Settings of the TV Tuner.

      Delete