Sunday, May 25, 2014

How to Install PyTivo on Synology NAS

PyTivo is a free program that allows you to serve to your Tivo videos, pics and music stored on your Synology. Here are instructions that attempt to be complete enough so that most anyone can get PyTivo working on their Synology NAS.

There are a number of web pages and forum entries talking about how to get PyTivo to run on a Synology NAS but they assume that you are already knowledgeable about Linux and this very process. I have attempted to provide a complete set of steps so that just about anyone can get PyTivo to work on their Synology.


The steps will cover the following:

  • Setup of a Command Line Interface (CLI) to send commands to the Synology NAS.
  • Installing ipkg using a bootstrap. This will make it easy to install other programs such as PyTivo.
  • Installing Git which makes it easier to download open source programs such as PyTivo.
  • Install Python which is needed to run PyTivo.
  • Download and install PyTivo.
  • Setup of PyTivo.

And now for the steps.

  1. Download PuTTY. This is the CLI program that enables SSH communications with the NAS.
    http://...~sgtatham/putty
  2. Log onto Synology NAS
  3. Enable SSH on the Synology NAS. This will allow you to externally execute commands on the NAS.
    1. Go to Control Panel and select the "Terminal & SNMP" page.
    2. Check the box for Enable SSH Service
    3. Click Apply.
  4. While in Control Panel lookup the CPU type. You will need this to get the correct ipkg bootstrap.
    1. Select the "Info Center" page.
    2. Look for and note the value after "CPU". For example, the CPU on the DS213j is "MARVELL Armada 370"
  5. Find the correct bootstrap. In most cases, you will download and install using commands typed into the PuTTY command window (see step 7).
  6. Execute PuTTY.exe
    1. The Configuration screen will appear. Enter the IP address for the Synology NAS, enter a name for the "Saved Session" and hit Save. You will reuse this session info in the future so you don’t have to reenter the IP address.
    2. Click on Open. You will get a command window asking, "login as".
    3. Login using the "root" account. The password is the same as admin.
  7. Using the PuTTY CLI, the following commands are to download and install the bootstrap for the Armada 370. Each bootstrap has its own instructions. Note the first command is too long to fit on one line in the window below but it is to be entered as one line.
    • /usr/syno/bin/wget https://github.com/hsleep/ds213j_bootstrap/archive/master.zip --no-check-certificate -O ds213j_bootstrap-master.zip
    • unzip ds213j_bootstrap-master.zip
    • cd ds213j_bootstrap-master
    • sh bootstrap.sh
  8. Verify the install
    • source /etc/profile
    • ipkg info
      You will see something like:
      Package: wget
      Version: 1.12-2
      Conflicts: wget-ssl
      Status: install user installed
      Architecture: arm
      Installed-Time: 1400475847

      Successfully terminated.
  9. Make sure everything is up to date
    • ipkg update
    • ipkg upgrade
  10. If you haven’t already installed python using the Synology Package Center (the recommended method), you can install it from the PuTTY CLI. However, using the command below will not likely install the latest version.
    • ipkg install python
  11. Install git to make it easier to download pyTivo.
    • ipkg install git
  12. Verify the location of ffmpeg.
    • ls /usr/syno/bin/ffmpeg

    You will see the path repeated if the file exists at this location. If it doesn’t, you will need to install it.
  13. Install pyTivo
    • mkdir -p /usr/local/src/pytivo
    • cd /usr/local/src/pytivo
    • git clone git://repo.or.cz/pyTivo/wmcbrine.git
    • cd /usr/local
    • ln -s src/pytivo/wmcbrine pyTivo
    • cd pyTivo
    • cp pyTivo.conf.dist pyTivo.conf
  14. Edit pyTivo.conf and set the path to ffmpeg (from step 12).
    • vi pyTivo.conf

    vi is a text editor. Here are some useful commands (Hit ESC to enter a command):
    dd
    delete line
    O
    insert line above cursor
    o
    insert line below cursor
    u
    undo
    :wq{Enter}
    save and quit
    :q! {Enter}
    quit without saving
    ZZ
    Save
    The line to be changed will say
      "ffmpeg=/usr/bin/ffmpeg"
    but it needs to say
      "ffmpeg=/usr/syno/bin/ffmpeg".

    More commands available at
    ...synology.com...Linux_VI_Editor
  15. Additional changes to the file include changing the name of the Share (which displays on your Tivo under "Now Playing"), changing the path for that share, and adding additional shares if needed.
    1. The default share is MyMovies and appears in the file as "[MyMovies]" (a section header).
    2. The correct path will be something like "path=/volume1/video".
  16. Find the location of python.
    Depending on how you installed python, it will likely be in one of the following two directories:
    • /usr/bin
    • /opt/bin
    Run the following two commands to see if it is in one of those directories. The response of each command will either be a repeat of the path indicating the file is there or "No such file or directory".
    • ls /usr/bin/python
    • ls /opt/bin/python

    You can get the version of python by running it (and then hit {Ctrl}+D to exit). For example:
    • /usr/bin/python
      You will see something like:
      Python 2.7.6 (default, Apr 8 2014, 23:36:51)
      [GCC 4.6.4] on linux2
      Type "help", "copyright", "credits" or "license" for more information.
      >>>

  17. Create file "start.sh". If you’ve been following the instructions, you are already in the "/usr/local/src/pytivo/wmcbrine" directory and don’t need to specify it.
    • vi start.sh

    The contents will be as follows. Note that you will either be entering "/usr/bin/python" or "/opt/bin/python" on line 2.
sleep 60
/usr/bin/python /usr/local/pyTivo/pyTivo.py > /dev/null 2>&1 &

  1. Create file "pyTivo.sh".
    • vi pyTivo.sh

    The contents will be as follows (from Iluvatar on pytivo.sourceforge.net...)
RETVAL=0

start() {
   echo -n ""Starting pyTivo: ""
   ps | grep pyTivo.py | grep python
   RETVAL=$?
   [ $RETVAL -eq 0 ] && echo ""pyTivo already running: Exiting"" && exit 1

   sh /usr/local/pyTivo/start.sh > /dev/null 2>&1 &
   RETVAL=$?
   [ $RETVAL -eq 0 ] && echo -n ""done""
   Echo
   return $RETVAL
}

stop() {
   echo -n ""Stopping pyTivo: ""
   kill -9 `ps |grep pyTivo.py |grep python | awk -F" " '{print $1}'`
   RETVAL=$?
   Echo
   [ $RETVAL -eq 0 ] && echo -n ""done""
   Echo
   return $RETVAL
}

case ""$1"" in
   start)
    start
    ;;
   stop)
    stop
    ;;
   restart|reload)
    stop
    sleep 1
    start
    RETVAL=$?
    ;;
   *)
    echo ""Usage: $0 {start|stop|restart}""
    exit 1
esac
exit $RETVAL


























  1. Make the two .sh files executable.
    • chmod 755 start.sh
    • chmod 755 pyTivo.sh

  2. Create a link to pyTivo.sh so that it will get auto started when the NAS is booted. The file has to appear as if it is located under "/usr/syno/etc/rc.d/" and has "S99" at the beginning of its name.
  • ln -s /usr/local/pyTivo/pyTivo.sh /usr/syno/etc/rc.d/S99pyTivo.sh

  1. Restart the NAS and, after a minute, pyTivo should have a web page at http://your_NAS:9032. You can make option changes here instead of reediting the pyTivo.conf file. You can also start pyTivo without a restart by running the following:
    • /usr/syno/etc/rc.d/S99pyTivo.sh start

A lot of steps, but this should be enough to get you enabled. However, there is the question of whether this is even worthwhile given that Synology now has native support for Chromecast.


17 comments:

Anonymous said...

Thank you for the tutorial, it was very helpful.

Anonymous said...

Thanks you, thank you, thank you. I have your blog bookmarked, because every time I update DSM I have to come back to remember how to get pytivo working again.

Evan Barr said...

After updating (or rebooting) DSM, you shouldn't have to do anything. But, I find that I need to manually start pyTivo (step 21). After quite a few updates (including updates to python), I've not needed to do more than that.

Chesterton said...

I have a DS213+, which has a PowerPC QorlQ processor. I can't find an installer for ipkg, but I did get python 3 installed. Is there a way to get pyTivo installed without ipkg?

Evan Barr said...

@Chesterton: Someone would need to create a pyTivo package for Synology. I doubt that will ever happen because it is hard to justify the effort. Tivo is being left in the dust when it comes to serving up external streams and it is because they have not embraced the standards. I just added Chromecast to my setup and it was trivial to get it to work with Synology.

Chesterton said...

Evan, thank you for responding! I got it to work! I found an ipkg installer for my DS213+ here:

http://forum.synology.com/wiki/index.php/Overview_on_modifying_the_Synology_Server,_bootstrap,_ipkg_etc#Installing_compiled.2Fbinary_programs_using_ipkg

I discovered that your pyTivo.sh script above has gotten corrupted. The Echo statement should be lower case (echo), and the double quotes should not be repeated. Here's the script that works for me:

RETVAL=0

start() {
echo -n "Starting pyTivo: "
ps | grep pyTivo.py | grep python
RETVAL=$?
[ $RETVAL -eq 0 ] && echo "pyTivo already running: Exiting" && exit 1

sh /usr/local/pyTivo/start.sh > /dev/null 2>&1 &
RETVAL=$?
[ $RETVAL -eq 0 ] && echo -n "done"
echo
return $RETVAL
}

stop() {
echo -n "Stopping pyTivo: "
kill -9 `ps |grep pyTivo.py |grep python | awk -F" " '{print $1}'`
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && echo -n "done"
echo
return $RETVAL
}

case "$1" in
start)
start
;;
stop)
stop
;;
restart|reload)
stop
sleep 1
start
RETVAL=$?
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
esac
exit $RETVAL

Unknown said...

I've followed every step of this tutorial to the "T", but I can't seem to get my pyTivo to run on my DS214SE. The only thing I changed was the command output text when starting/stopping. When I run the start command, I get this:

Staring pyTivo: pyTivo Started!

But when I browse to the URL, I just get "Unable to connect". If I rerun the start command, and if pyTivo is in fact running, should it not inform me that it is? If I rerun the command, I just get another message that it started.

MySyno> /usr/syno/etc/rc.d/S99pyTivo.sh start
Staring pyTivo: pyTivo Started!
MySyno> /usr/syno/etc/rc.d/S99pyTivo.sh start
Staring pyTivo: pyTivo Started!

I have pyTivo running on my Windows 8.1 laptop and it works fine. (I changed the port in pyTivo.conf to avoid any potential port conflicts)

Any thoughts?

Evan Barr said...

@Steve:
Double-check the pyTivo.conf to make sure that they still have the port set to 9032. Then make sure that port is open between your browser and the nas.

Unknown said...

Thanks for the suggestion Evan, but that wasn't the problem. Turns out that I had to add a few things to the pyTivo.conf file

First, I had to remove the comment from the [_tivo_HD] line. I don't know that it is required, but I also removed it from [_tivo_SD] (This was how I had it setup on my laptops instance)

Second, I had to add my Tivo account information using:
tivo_username=myemail@email.com
tivo_password=mypassword

Once those changes were committed, I was able to get it started and browse the URL, and I can now see all my Tivo recordings, as well as my push folders (Although for some reason I can't seem to get my Music share to show up on the main page, but not really concerned with that as I don't intend to use it).

Now its on to getting tivodecode figured out!

Thanks Evan, btw, for putting together this tutorial...I'm a fairly big noob when it comes to anything unix/linux based and this was a great reference for me to get my hands dirty!

Average White Dad said...

Thanks for the tutorial. It's worked successfully for me for a while now. The only problem is that most of the time, after Synology pushes an OS update, I need to reattach the symlink
ln -s /usr/local/pyTivo/pyTivo.sh /usr/syno/etc/rc.d/S99pyTivo.sh

I thought I could avoid this by putting the symlink in /usr/local/etc/rc.d instead, but it doesn't run. Any ideas why it won't work in that folder? Or any ideas on how to prevent having to fix it after updates? Thanks.

Evan Barr said...

@Average White Dad:
It seems that starting with DSM 5, putting the link under "/usr/local/etc/rc.d/" stopped working. I don't know the specifics but the developer guide still recommends using that directory as the syno directory is supposed to contain Synology's default services (including 3rd party services). It is possible that local boot scripts run before syno and opt scripts which means that the necessary services haven't started yet. The dependencies is the reason for starting the sh name with "S99" so it will execute last. You could test this theory by increasing the delay in "/usr/local/src/pytivo/wmcbrine/start.sh".

Maybe the updates will treat a hard link differently. Try the following (ignoring the line split):
ln -f /usr/local/src/pytivo/wmcbrine/pyTivo.sh /usr/syno/etc/rc.d/S99pyTivo.sh

If the relative paths don't resolve correctly when starting from S99pyTivo.sh, then try the following command (which has only 1% likelihood of success):
ln -f -F /usr/local/pyTivo/pyTivo.sh /usr/syno/etc/rc.d/S99pyTivo.sh

Otherwise, I suppose you could create a physical "/usr/syno/etc/rc.d/S99pyTivo.sh" file that simply executes:
RETVAL=0
/usr/local/pyTivo/pyTivo.sh $1
exit $RETVAL

If you create a physical file, don't forget to
chmod 755 S99pyTivo.sh

Average White Dad said...

Turns out the answer was ridiculously simple. You need to add a shebang line (i.e. "#! /bin/sh") at the top of the pyTivo.sh script. Why Synology requires the shebang for scripts in /usr/local/etc/rc.d but not in /usr/syno.etc/rc.d remains an exercise for the reader (i.e. who the hell knows).

Unknown said...

Would this be similar on a netgear readynas?
I have a RN212 running on OS 6.
I have been able to log in via ssh with puTTy
But didn't know where to go from there until I read all this

Average White Dad said...

I'm not very familiar with the ReadyNAS, but some quick googling leads me to think that there are simpler solutions than trying to adapt the one here.See, for example, http://kb.netgear.com/20900/How-to-archive-and-playback-TiVo-recordings-to-the-ReadyNAS?cid=wmt_netgear_organic

Unknown said...

PyTiVo is far superior to the TiVo Service on ReadyNas. The Tivo service on the readynas is the reason I bought it, but I've been seriously disappointed by what it provides.

Evan Barr said...

Some of the directories on Synology are different from standard Linux and I didn't go into detail about each step. So that will make it harder to convert these instructions to another OS.
Since writing this article, I have converted most of my Tivo video files to mpg which I serve up via Chromecast. It is easier, standards based, and more stable than the pyTivo/Tivo solution.
If you decide to continue down the pyTivo route, please let us know the result.

Anonymous said...
This comment has been removed by a blog administrator.

Post a Comment