Extend Transmission Torrent with Scripts

Transmission is one of my favourite bittorrent client. However, it’s lack the feature of running command(s) before or after you have finished downloading. Luckily we can improvise this with the help of transmission-remote, a command based client for transmission, and along with some Linux bash scripting.

Below I have created a simple bash script. What it does is, converting all of the finished avi’s to wmv. The conversion is via ffmpeg. The script below is easily modified to suit your needs, such as:
i – Copying the finished torrent to another location.
ii – Doing any conversion to your media.
iii – Send you mail notifying the torrent have finished downloading.
iv – etc, etc.

So, let’s get started.

1. Install transmission-remote
sudo apt-get install transmission-remote

2. Copy the script below, and save as my_script_name.sh , (or whatever name you prefer, but note the .sh extension).
This is among my early attempts of shell script programming, so go easy on me and feel free to give feedbacks. :)

#!/bin/sh

# The absolute path where your torrents are downloaded
# For example: /tmp/download
BASEDIR=/home/myuser/mytorrentpath
LOGPATH=/home/myuser/mywmv.log

# The absolute path to and including the transmission-remote binary
# For example: /usr/bin/transmission-remote
TRANSREMOTE=/usr/bin/transmission-remote
TRANSOPTS=" 127.0.0.1:my_port_number -n my_username:my_password "

DoConvert ()
{
  _AVINAME=$1
  WMVNAME=${_AVINAME%.*}".wmv"
  if [ ! -f "$WMVNAME" ]; then
    #Do conversion here
    echo "converting $_AVINAME" >> $LOGPATH
    ffmpeg -sameq -i "$_AVINAME" -vcodec wmv2 -acodec wmav2 "$WMVNAME" \
    < /dev/null 2> /dev/null
  fi
}

cd $BASEDIR

echo "Starting at `date`" >> $LOGPATH

# Find the first completed torrent
$TRANSREMOTE $TRANSOPTS -l | grep "100%" | grep "Done  " | cut -b71- | while read LINE
do
  if [ -e "$LINE" ]; then
    if [ -h "$LINE" ]; then
      echo "$LINE is already moved" >> $LOGPATH
    else
      if [ -d "$LINE" ]; then
        cd "${LINE}"
        ls -1f *.avi 2>/dev/null | while read AVINAME
	  do
	    DoConvert "$AVINAME"
	  done
        cd -
      else
	DoConvert "$LINE"
      fi
    fi
  else
    echo "torrent $LINE is removed" >> $LOGPATH
  fi
done

echo "-----------------" >> $LOGPATH

Save the script on any location you prefer.

3. Create a cron job to run the script. Open the entry to crontab:
crontab -e

4. Let say you want to run the script every hour, by the hour, the entry on your cron job should be as below:
0 * * * * sh /home/myuser/my_script_name.sh

You can also test the script you’ve created by executing the command:
sh /home/myuser/my_script_name.sh

Enjoy.

Related Posts:

3 Comments

  1. How would you adjust this script to make it zip files after it is finished?

  2. How would you adjust this script to make it zip files and folders after it is finished?

  3. I’ve never tried that before, but “zip” command should do it:
    zip squash.zip file1 file2 file3

Leave a Comment



  • recently written