Friday, November 14, 2008

The TV Server I Always Wanted, Part 3

In my first two parts of this series, I described what I wanted out of a TV server and what my options were. Here's the detailed HowTo.

Get SomeHardware

Get a decent mid-sized system with an NVidia card featuring the kind of TV-out you need, a lot of disk space, and a decent amount of RAM. You could go used on the equipment, but I wanted the disks to be fast, so that meant SATA II. Here's what I put together.
  • An Intel 82801-based motherboard with gigabit Ethernet and ICH7 audio.
  • NVidia GForce 8400GS with s-video (I have a low-def, 21" TV).
  • Intel(R) Pentium(R) Dual  CPU  E2180  @ 2.00GHz
  • 1GB RAM
  • one 500GB SATA II drive, a 320GB SATA II drive (old movies), and two 250GB SATA II drives (one is an external with old TV).
Put is all together. Better yet, have someone else do it for you. No bloody knuckels.

Install Mythbuntu 8.04.1

This part is pretty straightforward. We're using the LTS because we want this thing to last for years and not have to be screwed with again.

Use the standard CD, not the Alternate install disk because the alternate disk won't set things up in the same way and you'll need to do some extra work at the end. Follow the directions on the install and set up the NVidia card with the Mythbuntu tool. Install the modules you want to be using. Make sure to have Videos and Mythweb. Set everything to autologin.

The mount points for my disks look like this:
  • 250GB on /
  • 500GB on /var/lib/mythtv/recordings
  • 250GB of old TV on /var/lib/mythtv/recordings/OldTV
  • 320GB of old movies on /var/lib/mythtv/videos/OldMovies
When you are finished with the install, install SSH and use that for the rest of the Howto.

Through SSH, add the Launchpad PAA for Mythbuntu to /etc/apt/source.list in order keep up with the current version of MythTV and get some modules that aren't in Hardy
## Mythbuntu PPA
deb http://ppa.launchpad.net/mythbuntu-testing/ubuntu hardy main
Make sure to update and upgrade when you finish adding the repository.

Import your movie collection

You need to install a way to automatically add and look up any videos you add to the system so that you don't have to muck about in the Video Manager.
sudo aptitude install mythvideo-bulk-updater
 This will install a cron script which checks for new videos hourly. If you can't wait that long, you can run the Perl script manually the first time.
/usr/share/mythvideo-bulk-updater/mythvideo-bulk-updater.py
Make sure to run the script as the user MythTV logs in as.

Import your TV collection

Steven Adeff was nice enough to write a Perl program which parses a filename, scrapes RageTV, and inserts data into the MythTV database for us. We're goign to use it.
Go to http://www.jobs-khakis-chicks.com/MythTV/TVRageImport/ and download the newest version (0.7 right now). There's also a script that uses tv.com, but I haven't tried it. You might run both in parallel to get shows that are on one site but not the other. Save the file to your home directory and set it to be executable with
chmod +x ragetvgrab-0.7.pl
Run the following commands

cd /var/lib/mythtv/recordings
find . -name '*.avi' -type f -exec /home/user/ragetvgrab-0.7.pl -import -lv {} \;
You should repeat this command with *.mkv instead of *.avi if you have any old files in that format.
NOTE! You must be in the recordings directory when this command is run or you will have a bunch of listed shows which return "File not found" when you try to play them.

Set up RTorrent to get some new shows

Next, we need a BitTorrent client. I have chosen RTorrent because it is lightweight (I want to watch shows with no skipping) and powerful. It doesn't have a web interface, which some others do. Feel free to replace this with another one if you want. Don't bitch at me about my choice, though, eh?
sudo aptitude install rtorrent
Next, we need to create a configuration file and some temporary directories for the torrents.
cd ~
mkdir Torrents
cp /usr/share/doc/rtorrent/examples/rtorrent.rc ./
We want to ask RTorrent to do several jobs it doesn't do by default
Watch the directory for new files
We will be automatically downloading the .torrent files, so we want RTorrent to keep an eye out for them and stop seeding if the torrent files get deleted. Look for this section in ~/.rtorrent.rc, uncomment the following lines and edit the directory to match this.
# Watch a directory for new torrents, and stop those that have been
# deleted.
schedule = watch_directory,5,5,load_start=./Torrents/TV/EZTV/*
schedule = untied_directory,5,5,stop_untied=
Automatically move completed torrents to recordings
We don't want incomplete files in the recordings directory (for obvious reasons), but we also want to be able to watch while it's still seeding. We need to move while seeding. Let's edit ~/.rtorrent.rc again and add:

# Check hash for finished torrents. Might be usefull until the bug is
# fixed that causes lack of diskspace not to be properly reported.
#check_hash = no

on_finished = move_complete,"execute=mv,-u,$d.get_base_path=,/var/lib/mythtv/recordings/ ;d.set_directory=/var/lib/mythtv/recordings/"
Stop the torrent after seeding
In order to do this, we need to uncomment and edit the following line:
# example: stop at ratio 1.5 with at least 200 MB uploaded, or else ratio 20.0
schedule = ratio,60,60,"stop_on_ratio=150,200M,2000"
Stop the torrent after a certain date
Here we get complicated. RTorrent doesn't have the functionality to stop seeding after a certain time, but we can use the watched directory, find and cron to stop the torrent after a few days. Type
crontab -e
and add
55 21 * * * find /home/user/Torrents/TV/EZTV -mtime +2 -type f -exec rm {} \;
I run the screen command to get a terminal in SSH that I can disconnect from and run rtorrent inside that because I like to monitor RTorrent every few days, but you could also set RTorrent to run automatically on login. No matter the case, you need to make sure that RTorrent is running as the user the Mythbuntu logs in as.

Set up Podget to bring in the feeds

Now we need to automatically get torrent files for new TV shows and put them in RTorrent's watch directory. I'm going to use Podget because it's designed to download attachments from RSS feeds and can be easily cronned.
sudo aptitude install podget
Next, we need to find the RSS feeds for various TV shows. I'll leave that up to you. Podget doesn't handle strange URL characters well, so you'll want to make a tinyurl from them. We need to add these URLs to .podget/serverlist. The following line will put torrent files for every television show on TVRSS.net to the watched directory:
http://tvrss.net/feed/unique/ TV EZTV
The last two parts of the line are the category and subcategory. It's the reason that our .rtorrentrc file includes "load_start=./Torrents/TV/EZTV/" instead of something else. In this setup, all your feeds should be in the same subcategory.
NOTE: We could give our feeds different categories or subcategories and set up RTorrent to move finished torrents from different categories into different final directories and create a nice, ordered structure by, say, program name. I didn't do that, though.
Finally, we need to call Podget using cron. You can do it once a day or more, depending on how many programs and feeds you have going. If you have individual feeds, you'll only need to do the cron job once a day. I have a single, busy feed, so I check several times a day. Again,
crontab -e
and add
50 15 * * * podget -l Torrents/

Add the shows to the database using RageTV

We now have the torrent files being downloaded by Podget, RTorrent handling the downloading of the shows, the shows being moved to our recordings directory, and the seeding stopping after a few days. Sadly, when we open MythTV, there are still no new programs to watch. We need to import the new programs using RageTVGrab and the -mtime switch of the find command. Again
crontab -e
then add
35 21 * * * cd /var/lib/mythtv/recordings && find . -name '*.avi' \
-mtime -1 -type f -exec /home/user/ragetvgrab-0.7.pl -import -lv {} \;
Or you could create a more complicated script and call that instead. As I mentioned in the other section on importing your old TV programs, you need to make sure that you are in base recordings directory when running the script.

DAAP server

Mythbuntu automatically shares several folders using Samba, including /var/lib/mythtv/music, so we should use this to share music with our network. Why not, eh? Let's install Firefly Media Server and configure it.
sudo aptitude install mt-daapd
Then we need to log in to the web interface (or manually edit the config file, and tell the server to look into the music directory. In order for the server to work correctly, we need to set the permissions for the folder.
chgrp mt-daapd /var/lib/mythtv/music
and we need to make sure Samba keeps the group permissions for the directory. Edit /etc/smba/smb.conf
 [music]
comment = Music
path = /var/lib/mythtv/music
public = yes
writable = yes
create mask = 0660
directory mask = 0770
force user = mythtv
force group = mt-daapd

Sit back and enjoy

You are finished. You no longer need to do anything to the server but watch TV, really. You can check your free disk space, check your programs, and expire or delete them using MythWeb. I used Zeroconf to announce all the services and make my life easy on other clients in the house. On the client, you just open Rhythmbox to play your shared music. If you download and install the Coherence uPnP Totem plugin on the client, then you can just open Totem and watch your recordings there.

Missteps and pitfalls

I really wanted to use MythNetTV to make this happen. It looked promising. It is an easy-to-install module. I didn't want to have to cobble the stuff together, making many mistakes along the way, and cron it all up myself. Unfortunately, there's a problem with BitTornado causing MythNetTV not to work, There is a patch floating around, but I couldn't get it to work. The changes to BitTornado to remove the curses dependency stopping MythNetTV from working didn't even worked when I tried them manually. I hope one day that this module just works. Right now, it doesn't.

2 comments:

  1. So you use this primarily for watching TV shows that you've downloaded? Not shows that you've recorded?

    ReplyDelete
  2. I don't even HAVE TV to the house because I'm living in Korea and don't really speak the language. It just handles shows via BT. I wanted an easier solution, but nothing that I tried actually worked OOTB.

    ReplyDelete

Other I' Been to Ubuntu Stories

Related Posts with Thumbnails