Integrating OTA and Pay TV Services Using Tvheadend: Part 2 Installation

In part 1 we discussed how a streaming video server can improved your viewing experience and why Tvheadend +  node-ffmpeg-mpegts-proxy + Kodi is an optimal solution.  In part 2, we'll cover installation and configuration of the components.

Choose an environment

Linux is required.  Tvheadend officially supports Debian/Ubuntu and CentOS/Fedora.  Running in a virtual machine works well, so spin up a vm on VirtualBox, KVM, or your favorite hypervisor and install your preferred distribution.  I run Lubuntu, so this article will use Debian/Ubuntu packages and methods.  Tvheadend exposes its configuration interface through a built-in web server, so a server installation is fine - no desktop required.  If you're not accustomed to a Linux environment, I would suggest a light weight desktop such as Lubuntu to make things more comfortable.

One hardware specific note:  if you will be using an HDHomeRun, the vm needs to be on the same subnet as the HDHomeRun.  Network discovery of the tuners will fail otherwise.

Install xmltv

xmltv is the software used to read EPG data from Schedules Direct and convert it to a format that Tvheadend can consume.

If you don't already have a Schedules Direct account, you'll need to go to http://www.schedulesdirect.org/ and sign up for a membership.  Once you have a membership, login and create a lineup.  Each membership allows up to four lineups and you'll need at least two if you're going to use OTA as well as cable/satellite.  Creating a lineup is easy, just enter your zip code then select between the various pre-defined lineups.

Create working directory and  install xmltv:

mkdir ~/tvh
sudo apt install xmltv

Configuring xmltv can be a little confusing. There are a number of EPG grabber scripts supplied with xmltv.  The one you use depends upon your location and services.  For North America with a Schedules Direct subscription, we want tv_grab_na_dd.

In a terminal enter:

tv_grab_na_dd --configure --config-file ~/tvh/myxmltv.conf

Answer each of the configuration questions and xmltv will write a .conf file used for future EPG data downloads.  If you have multiple sources (eg OTA, cable) with different content, you'll need to run tv_grab_na-dd --configure --config-file ~/tvh/myxmltv2.conf to create a second xmltv grabber configuration.

To test our xmltv installation, let's download channel listing for each of our configurations.

In a terminal enter:

tv_grab_na_dd --config-file ~/tvh/myxmltv.conf --list-channel --output ~/tvh/channels.xml

A file named channels.xml should be created in ~/tvh  with information about each of the channels in your Schedules Direct lineup.  If you have multiple lineups and xmltv configurations, repeat the process for each specifying the correct --config-file and supplying a new --output name.

For more information about xmltv and Schedules Direct, see the Ubuntu manpage at
http://manpages.ubuntu.com/manpages/xenial/man1/tv_grab_na_dd.1p.html

Install xmltv2m3u

This installation step is only required for a cable/satellite configuration that uses an external set-top-box.  If your only tuners are HDHomeRun variants, skip to the next section.

xmltv2m3u is a script I wrote to help automate some of the later configuration steps.  It reads the .xml channel files we created with xmltv in the last installation section and produces files in the formats required by Tvheadend and node-ffmpeg-mpegts-proxy.  The script and support files are hosted on GitHub, so let's make sure we have the tools we need, then download the script.

In a terminal enter:

sudo apt install git

Clone the repository:

cd ~/tvh
git clone https://github.com/jfath/xmltv2m3u

Make a working copy of the files in ~/tvh:

cp ~/tvh/xmltv2m3u/* ~/tvh
chmod 775 ~/tvh/*.sh
chmod 775 ~/tvh/changechannel
chmod 775 ~/tvh/grabepg

Edit ~/tvh/xmltv2m3u.pl to customize it for your environment.  Near the top of the file, you'll see these lines:

my $iptvprovider = "Smithville Digital";
my $iptvsource = "http://192.168.###.##:8080/hdmi";
my $iptvpre = "/home/hts/tvh/pre.sh";
my $iptvpost = "/home/hts/tvh/post.sh";

Change the value assigned to $iptvprovider to the name of your cable/satellite provider.  Change the value assigned to $iptvsource to match the URL of your HDMI encoder.  You can leave the $iptvpre and $iptvpost values as-is.
Update:  If you're using an HD-PVR, enter the device name as the $iptvsource value in this form: file:/dev/video0.

The pre.sh script will be called by node-ffmpeg-mpegts-proxy just before video streaming starts for live video or dvr recording.  This is the hook that we need to change the channel on our set top box.  The supplied pre.sh script calls /home/hts/tvh/changechannel with the requested channel number.  You must customize changechannel for your environment.  The supplied changechannel script uses lirc to change the channel using a remote named adb3800.  Assuming you intend to communicate with your set-top-box using an IR blaster, you'll need to install and configure LIRC, then change the name of the remote in the supplied changechannel script to match the remote defined in your LIRC configuration.

LIRC configuration can be difficult.  The details are beyond the scope of this document, but a reasonable place to start is at http://www.lirc.org/html/configuration-guide.html.  Once configuration is successful, you should be able to execute '~/tvh/changechannel 123' and see your STB tune to channel 123.

Use xmltv2m3u to create the configuration files from our channel.xml file(s):

sudo apt-get install libxml-libxml-perl
cd ~/tvh
perl ./xmltv2m3u.pl ~/tvh/channels.xml

This should create files named channels.m3u and channels.json in the ~/tvh directory.  Run the script again if needed for other lineups.

Install node-ffmpeg-mpegts-proxy

This installation step is only required for a cable/satellite configuration that uses an external set-top-box.  If your only tuners are HDHomeRun variants, skip to the next section.

node-ffmpeg-mpegts-proxy requires node.js >= 0.12.  You can add the node repo and install using instructions found at https://nodejs.org/en/download/package-manager/#debian-and-ubuntu-based-linux-distributions.  I would recommend the newer version, Node.js 8 as of this writing.

In a terminal enter:

sudo apt install build-essential libav-tools
cd ~/tvh
git clone https://github.com/Jalle19/node-ffmpeg-mpegts-proxy
cd ~/tvh/node-ffmpeg-mpegts-proxy
npm install

Make sure no errors are encounter during the 'npm install' step, but warnings are fine.

We're going to copy node-ffmpeg-mpegts-proxy.js to a different directory for the final installation, so no need to execute it yet, but when we're ready, the usage is:

nodejs ./node-ffmpeg-mpegts-proxy.js -p <port> [-a <avconv>] [-q | -v] [-s <sources>]

A suggested command line listening on localhost at port 9128 using the sources file we created previously would look like this:

nodejs ~/tvh/node-ffmpeg-mpegts-proxy/node-ffmpeg-mpegts-proxy.js -l 127.0.0.1 -p 9128 -s ~/tvh/channels.json

Additional information, including how to run as a service at startup, can be found in the readme at:
https://github.com/Jalle19/node-ffmpeg-mpegts-proxy.

Install Tvheadend

Installation of the Tvheadend package varies according to your distribution and version as well as the desired version of Tvheadend.  See this link for information:
https://tvheadend.org/projects/tvheadend/wiki/AptRepository

The following instructions assume you are running Ubuntu 16.04 and want to install Tvheadend stable-4.2.  This matched my environment, but you must refer to the above link to customize the steps for your environment.

In a terminal enter:

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 379CE192D401AB61
echo "deb https://dl.bintray.com/tvheadend/deb xenial stable-4.2" | sudo tee -a /etc/apt/sources.list
sudo apt-get update
sudo apt-get install tvheadend

Tvheadend should now be installed and the configuration interface is available from a web browser at http://SERVERIPADDRESS:9981.  The actual configuration procedure deserves its own article, so we'll leave the details for later.

Install Kodi

You'll probably want to install Kodi on multiple devices, one installation for each viewing station on your network.  Assuming our Tvheadend server includes a desktop environment, we can install Kodi on the same machine for convenient testing.  If you've chosen to install Tvheadend on a gui-less server, you can install Kodi on any network device running Linux, Windows. MacOS, Android, or iOS.  The instructions below assume Debian/Ubuntu Linux,  Check the Kodi web site for download and installation instructions for other platforms.

In a terminal enter:
sudo apt-get install software-properties-common
sudo add-apt-repository ppa:team-xbmc/ppa
sudo apt-get update
sudo apt install kodi kodi-pvr-hts

We'll need to enable the Tvheadend pvr addon in Kodi and configure it with theTvheadend server address and credentials after we complete the Tvheadend configuration.

Prepare for Tvheadend configuration

The Tvheadend installation created a user named hts.  If you're using node-ffmpeg-mpegts-proxy and xmltv2m3u you need to copy all of the configuration files we created and edited into the /home/hts directory:

sudo cp -rp ~/tvh /home/hts
sudo chown -R hts:hts /home/hts/tvh

Once you've done the copy, you can start the proxy server

nodejs /home/hts/tvh/node-ffmpeg-mpegts-proxy/node-ffmpeg-mpegts-proxy.js -l 127.0.0.1 -p 9128 -s /home/hts/tvh/channels.json

That's it!  You are now ready to configure Tvheadend for streaming video using your HDHomeRun and/or HDMI Encoder/STB.  That's the topic for part 3.

Comments

Popular posts from this blog

Integrating OTA and Pay TV Services Using Tvheadend

Integrating OTA and Pay TV Services Using Tvheadend: Part 5 EPG Configuration