Installing and Configuring NextPVR as a Replacement for Windows Media Center

If you follow me on Google+ you’ll know I had a recent rant about Windows Media Center, which after running fine for about a year suddenly decided as of January 29 it was done downloading the program guide and by extension was therefore done recording any TV shows.

I’ll spare you more ranting and simply say that none of the suggestions I got (which I appreciate!) worked, and rather than spending more time figuring out why, I decided to try something different.

NextPVR is an awesome free (as in beer, not as in freedom unfortunately …) PVR application for Windows that with a little bit of tweaking handily replaced Windows Media Center. It can even download guide data, which is apparently something WMC no longer feels like doing.

Background

I wound up going down this road in a rather circuitous way. My initial goal for the weekend project was to get Raspbmc running on one of my Raspberry Pis. The latest version of XBMC has PVR functionality so I was anxious to try that out as a potential replacement for WMC.

Installing Raspbmc was easy enough and the base install is working fine, but based on what I’ve read it may not (yet) be up to functioning as a full-blown PVR like I’m used to, and will likely serve better (as some folks on Google+ suggested) as a front-end to something else.

That led me to searching around for a WMC replacement that will run on Windows 7.

Given my huge preference for free (as in freedom) software why continue to use Windows and not move to something like MythTV? Honestly I’ll probably go the MythTV route eventually, but in the near term I was just looking for something that would quickly allow me to schedule and record shows again. I’m running WMC on a very nice Acer Revo, which has a DVD player form factor and is designed to function specifically as a media center, complete with a nice pop-out dual-function mouse/keyboard pad. I’ll go the full free software route another weekend.

As I looked around I came across this article on Lifehacker about turning a Raspberry Pi into a media center, and it too was focused on the Pi as a front-end to another machine that does the heavy lifting. Honestly I like this idea anyway — sticking a quiet, low-power Raspberry Pi behind every TV other than the one with the NextPVR box seems like a slick solution.

Installing NextPVR

Installing NextPVR is very straight-forward — I mostly just did what’s outlined in the Lifehacker article.

  1. Download the latest version
  2. Download the patches and copy them into the directory in which you installed NextPVR
  3. Start NextPVR and adjust the settings

The specifics of that last step will be dependent upon your hardware. Note that the default buffer directory of C:Temp may not exists so if you get an error related to that when you fire up NextPVR, just create that directory and restart NextPVR. I also did enable the background recording service as recommended.

In my case I’m using a SiliconDust HDHomeRun as my tuner, which means I do not have a tuner card in the media center PC. The HDHomeRun makes three tuners available over ethernet, and NextPVR sees them out of the box, though there is some configuration and installation of codecs necessary to get things working properly.

Watching Live TV on NextPVR

Next you’ll want to configure NextPVR to be able to watch live TV, and the first step in this process is to have NextPVR scan your tuner devices to get a channel listing.

With the HDHomeRun I simply had to go into devices, choose each tuner, and click the “Scan” button. I have digital cable so this pulled back a little over 500 channels. Note that you have to do this for each tuner. You can tell it to copy a configuration from one tuner to another, but I found that to be much slower than performing the scan on each one individually.

The first problem I ran into after the channel scan is that although NextPVR sees the HDHomeRun fine, when I tried to watch live TV I got no picture or sound. I could see that it was talking to the HDHomeRun since I saw the tuner light come on and it was going through the motions of changing channels, and even had the channel names correct, but there was no picture or sound.

If you have this issue don’t panic; it’s very likely you just need to go into the NextPVR settings and choose the correct decoder. If you don’t have valid MPEG2 or AC3 codecs installed, which are the video and audio formats the HDHomeRun uses, you’ll have to do that separately.

In my case to get the video working I had to enable the MPEG2 and H.264 video decoders, and for those I selected the “Microsoft DTV-DVD Video Decoder” that was already installed on my machine.

As you change these settings, make sure and restart NextPVR. I’m not sure that’s supposed to be required but with both audio and video I had to restart NextPVR for the changes to take effect.

To get the audio working, I had to install additional codecs since I didn’t have any on the machine that could handle AC3 audio. For MPEG1 audio the Microsoft DTV-DVD Audio Decoder should work (though I can’t confirm since I’m not sure I tested any MPEG1 audio specifically), but it definitely didn’t work for me for any of the other audio types.

Some suggestions for fixing this issue were to install Windows Media Center since it comes with codecs that won’t otherwise be on the machine, but from what I could tell anything WMC installs is pretty MS-specific, so you’re better off installing more agnostic codecs.

Luckily there are some excellent free (as in beer) codecs available. What I ended up installing was the Windows 7 Codec Pack. This comes with codecs for just about every audio and video format you can imagine and gave me what I needed to fix the lack of audio.

One the Windows 7 Codec Pack was installed, I selected “ffdshow Audio Decoder” for all the audio types other than MPEG1, and after restarting NextPVR I was getting audio. Again I think the only audio type I was really verifying was AC3 since that’s what the HDHomeRun uses, so if you have a different setup that requires different decoders, you may need to try some different options.

Downloading Program Guide Data (EPG) and Mapping Channels

With video and audio both working there was one last problem to solve, namely program guide data. Without guide data you won’t know what’s on and, as I discovered the hard way with WMC, you won’t be able to record anything.

In the case of the guide data I decided to throw a little money at this problem. You don’t necessarily have to pay for program guide data but from the little bit of research I did it seems like the free solutions would require some ongoing interaction on my part (or some scripting), and I don’t want to turn recording TV into another job for myself, so I decided to pay for it. If you want to investigate solutions like XMLTV and others, the NextPVR wiki page on program data (EPG) is a great place to start.

Specifically, I decided to pay $25/year for a Schedules Direct account. It integrates completely seamlessly with NextPVR and based on what I read was well worth the money compared to dealing with this yourself. (I’m not averse to a little scripting and hacking but I have enough side jobs at the moment!)

After getting a Schedules Direct account, you then create a listing in Schedules Direct, which is done by providing your zip code and choosing your cable provider.

Once the listing is created in Schedules Direct you then go into the Channels section of NextPVR, enter your Schedules Direct login information, pick a listing, and then program guide data will be continually updated for you.

That’s not quite the end of the story though. You still have to map the channels so NextPVR knows which channel in NextPVR goes with which channel in the lineup.

This is where I think NextPVR could use a little bit of work. You can do a bulk mapping, which works pretty well overall (though verify because it did mismatch some in my case), but if you do the bulk mapping and manually change any specific mappings it asks you every time you change one if you want to have it scan and try to do a bulk match even after it already did that. Minor annoyance but seems like it’d be an easy fix as well.

With all that in place, in the Channels section of the NextPVR setting I clicked the Update EPG button and a couple of minutes later, program guide data was available. (Take that, Windows Media Center!) Then I could finally go in and start recreating the season passes I lost when I tried reinstalling WMC.

As for the Raspberry Pi …

Since I basically took a huge detour from my original goal of getting XBMC up and running on my Raspberry Pi, that piece isn’t quite done yet. Raspbmc itself is running fine, and I installed the PVR Client for NextPVR, but it’s not working properly for some reason so I still need to dig into that.

Once I get that figured out then I can have the Raspberry Pi hooked up to a TV in another room and watch either live TV or recordings from the NextPVR box, which will be very slick. I’ll do another post on that once I get it working.

Conclusion

Overall I’m quite impressed with NextPVR so far. The installation was quite simple, it saw the HDHomeRun without any problems, and the codec issues I ran into were very easy to solve.

The UI may not be quite as slick, strictly speaking, as WMC, but it’s also a lot cleaner and zippier, and I’ll take that any day. With the guide data in place I’m very happy with the functionality and don’t think I’ll miss anything at all from WMC.

Longer term I’ll definitely look into MythTV so I have a free as in freedom solution, but this was a quick and easy project that at least got me up and running again after the WMC meltdown.

The first real test of my new setup will be when NextPVR records The Walking Dead tonight. As WMC found out if it screws that up, it’s history.

Setting Up Django On a Raspberry Pi

This past weekend I finally got a chance to set up one of my two Raspberry Pis to use as a Django server so I thought I’d share the steps I went through both to save someone else attempting to do this some time as well as get any feedback in case there are different/better ways to do any of this.

I’m running this from my house (URL forthcoming once I get the real Django app finalized and put on the Raspberry Pi) using dyndns.org. I don’t cover that aspect of things in this post but I’m happy to write that up as well if people are interested.

General Comments and Assumptions



Reconfigure the Keyboard Mapping

If you already did this at some other point with your Raspberry Pi or your keyboard is working properly you can skip this step. After the base install of Raspbian “wheezy” in my case I noticed when in vim I couldn’t type things like #, |, and @ so I had to do the following:

  1. sudo dpkg-reconfigure keyboard-configuration
  2. Follow the prompts to set things appropriate for your keyboard (US English in my case)
  3. Restart the Raspberry Pi (sudo reboot)


Install Required Server-Wide Tools and Libraries


  1. sudo apt-get install vim python-dev python-setuptools nginx supervisor
  2. sudo easy_install pip
  3. sudo pip install virtualenv virtualenvwrapper
  4. Configure virtualenvwrapper
    1. sudo vim /etc/bash.bashrc
    2. Add this line at the bottom of the file:
      source /usr/local/bin/virtualenvwrapper.sh
    3. Save the file and exit vim
    4. Log out and log back in


Create and Configure a virtualenv for Your Application

tl;dr version of the following paragraph: Run the following commands from your home directory unless you have a reason not to and know what you’re doing. For the purposes of this tutorial I’m using the default ‘pi’ user on the Raspberry Pi.

You can create a virtualenv for your application in your home directory or wherever you prefer your files to be located, just be aware that depending on where you put your files and whether or not you have to execute virtualenv-specific tasks (pip install, etc.) you may have to explicitly specify which python or pip command to use for it to work properly.

For the purposes of this example we’ll create a ‘foo’ application. The steps below where you’re running django-admin.py startproject and firing up the development server are simply to test to make sure everything’s working properly. If you’re doing this all for the first time, I’d suggest doing this both to get familiar with it as well as to double-check that everything with the environment is working as expected.

  1. mkvirtualenv foo
    1. Note that this will activate the virtualenv. You MUST have the virtualenv activated when performing the steps that follow for everything to work properly.
    2. If you’re used to using virtualenv directly as opposed to virtualenvwrapper, note that when you create a virtualenv using  mkvirtualenv, it will put the virtualenv files (again, if you’re doing this from your home directory) in ~/.virtualenvs/foo
  2. pip install django docutils south gunicorn
  3. django-admin.py startproject foo
  4. cd foo
  5. python manage.py runserver
    1. The first time you run this you may see a ton of blank lines output on the Raspberry Pi. It’ll eventually finish, or if you hit Ctrl-C when this happens and try again a couple of times it’ll eventually calm down and the next time you run things this won’t happen. I’m not sure what’s going on here since the Raspberry Pi is the only platform on which I’ve seen this happen.
    2. Once the development server starts up you should see “0 errors found [other stuff here] Development server is running at http://127.0.0.1:8000” If you don’t see that, double-check everything and try again.
    3. You can make a call to the development server by sshing into the Raspberry Pi from another machine and doing curl localhost:8000


Enable Gunicorn and South and Enable a Database in Your Application


  1. cd ~/foo/foo
  2. vim settings.py
  3. In the DATABASES section make the following changes:
    1. ‘ENGINE’: ‘django.db.backends.sqlite3’
    2. ‘NAME’: ‘/home/pi/foo/foo.db’
  4. In the INSTALLED_APPS section, enable the admin and admindocs (optional):
    1. Uncomment # ‘django.contrib.admin’
    2. Uncomment # ‘django/contribu.admindocs’
  5. Also in the INSTALLED_APPS section, add ‘south’ and ‘gunicorn’ to the list of installed apps.
  6. Save the file and exit vim
  7. Test Gunicorn:
    1. python manage.py run_gunicorn (you can hit Ctrl-C to kill it once you see it’s working)


A note on databases: I’m not addressing the broader topic of which database server you should use simply because it’s a separate discussion that’s potentially fraught with zealous opinions and general peril, and is a bit tangential to the present discussion.

If you’re doing all this just to learn, or for your own personal low-use “screwing around” type applications, SQLite (http://www.sqlite.org/), which is what comes with Django, will work just fine, so that’s what I outlined above.

I’m writing this up as part of setting up a site for alt.* Radio (http://codebassradio.net/shows/alt/), which is a weekly show I do on the super-awesome CodeBass Radio (http://codebassradio.net), so for that I’ll be running PostgreSQL on a separate machine. For low-traffic stuff you might even be able to get away with running MySQL or Postgres directly on the Raspberry Pi but I have a couple of unused machines laying around so I figured I might as well keep the Raspberry Pi focused on the Django side of things.

Note that if you DO want to use PostgreSQL, you’ll have to do this on the Raspberry Pi:

  1. sudo apt-get install libpq-dev
  2. pip install psycopg2 (with your virtualenv active)


Configure Supervisor

Supervisor (http://supervisord.org/), which we installed earlier, is a tool that lets you easily create startup/restart scripts for services that aren’t installed and managed through apt-get. In our case we’ll need this for Gunicorn so it’ll fire up when the machine boots and automatically restart if it crashes.

  1. cd /etc/supervisor/conf.d
  2. sudo vim gunicorn.conf
  3. Put the following in the new file:
    [program:gunicorn]
    command = /home/pi/.virtualenvs/foo/bin/python /home/pi/foo/manage.py run_gunicorn -w 4
    directory = /home/pi/foo
    user = pi
    autostart = true
    autorestart = true
    stdout_logfile = /var/log/supervisor/gunicorn.log
    stderr_logfile = /var/log/supervisor/gunicorn_err.log
  4. Save the file and exit vim
  5. sudo service supervisor restart
  6. sudo supervisorctl start gunicorn
    1. Note that you may get an “already started” error here. If you get a “no such process” error, that means supervisor didn’t load the new configuration file. If that happens:
      1. sudo ps -wef | grep supervisor
      2. sudo kill -9 SUPERVISOR_PROCESS_ID (use the real process ID)
      3. sudo service supervisor start


Configure Nginx


  1. sudo rm -f /etc/nginx/sites-enabled/default
  2. sudo vim /etc/nginx/sites-available/foo
  3. Put the following in the foo file:

# upstream server for gunicorn
upstream gunicorn {
  server localhost:8000;
}

# nginx server for the host
server {
  listen 80;

  server_name foo.com www.foo.com;

  root /home/pi/foo;

  access_log /var/log/nginx/foo_access.log;
  error_log /var/log/nginx/foo_error.log;

  # try to serve a static file and if it doesn’t exist, pass to gunicorn
  try_files $uri @gunicorn;

  # rules for gunicorn
  location @gunicorn {
    proxy_pass http://gunicorn;
    proxy_redirect off;
    proxy_read_timeout 5m;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Fowarded-For $proxy_add_x_forwarded_for;
  }
}

  1. Save the file and exit vim
  2. sudo ln -s /etc/nginx/sites-available/foo /etc/nginx/sites-enabled/foo
  3. sudo service nginx restart


Verify Everything’s Working

If you’re not using a real hostname/URL, make sure and add it to /etc/hosts on the Raspberry Pi so you can easily test. In this case we used foo.com as the hostname, so do the following:

  1. sudo vim /etc/hosts
  2. Add this line:
    127.0.0.1 foo.com
    www.foo.com
  3. Save the file and exit vim


If you want to hit the Raspberry Pi from a browser on another machine you’ll have to also add the IP and host names to /etc/hosts on any machines from which you want to hit that host, but use the IP of the Raspberry Pi instead of 127.0.0.1.

With that modification in /etc/hosts locally on the Raspberry Pi, you can do a curl foo.com to see if you get the default Django page (or your application if you took things a bit further than are outlined in this tutorial). If you do, everything’s working! If not, start over and don’t screw up this time!

Seriously though if you need any help just comment here or email/Google+/Twitter me and I’ll be happy to assist.