DNS 323 - Funplug : Generate Twonky video thumbnails from XBMC .tbn



In previous articles, I've explained how to install fun_plug extension script and TwonkyMedia server 7 on a DNS-323.

This article will explain how to configure TwonkyMedia server 7 to fully use an existing XBMC movies thumbnail database to generate its own video thumbnails.

In fact, if you are already using your DNS-323 thru a samba share to serve movies to some XBMC clients (Windows or Linux), you probably have already some .tbn thumbnails associated with all your movies and all your subdirectories. XBMC thumbnails with .tbn extension are simply jpeg files renamed with this specific extension.

This article will explain how to setup TwonkyMedia server environment to use these .tbn thumbnails to supply TwonkyMedia video thumbnail mecanism. Is has been tested sucessfully on a LG DLNA connected TV.

As a pre-requisite, you must have installed TwonkyMedia server and its dependencies following DNS 323 - Fun_plug 0.5 : Install Twonkymedia Server 7.

1. Main principle

If you are already using your NAS Samba shares to serve some XBMC clients, you probably have a nice and complete thumbnail database for it.

This thumbnail database is a set of JPEG files side to your video files with this naming convention :

  • your-avi-video-file.avi  =>  your-avi-video-file.tbn
  • your-mkv-video-file.mkv  =>  your-mkv-video-file.tbn
  • your-video-folder  =>  your-video-folder.tbn

So, when TwonkyMedia asks to generate a video thumbnail, we can simply deduce the .tbn filename from the video filename and convert it to the JPEG file needed by TwonkyMedia.

As DNS-323 is very much underpowered, the thumbnail generation process can not be done on-the-fly. It would take too much of time and if you browse your video directory, TwonkyMedia server will end-up hanging because of a huge thumbnail generation queue.

So, the thumbnail generation principle will be as follow :

  1. when Twonky server asks for thumbnail generation, we catch it and save the input data in a temp file
  2. a background thumbnail generation process is launched at the same time as Twonky
  3. it wakes up every 5 seconds and do the thumbnail generation if some temp files are found waiting
  4. the background generation process will be low priority, not to interfere with other processes

The following scripts will interface with TwonkyMedia server to generate the thumbnails needed by Twonky from the .tbn files.

All temporary files will be placed under /tmp so that the background generation process does not interfere with the hard disk power saving features.

2. Catch thumbnail generation event

The script /ffp/opt/twonky/cgi-bin/ffmpeg-video-thumb.desc is provided with TwonkyMedia server.

It is called to generate on-the-fly thumbnails from video files with ffmpeg.

We just need to modify it to call another thumbnail-candidate script, who will do the job of converting .tbn files to .jpg file with the right size.


# generate video thumbs
#(c) 2010 by PacketVideo
#exec: ffmpeg -itsoffset -$videothumbtime -i $infile -vcodec mjpeg -vframes 1 -an -f rawvideo -s $targetgeometry $outfile
exec: thumbnail-candidate $infile $outfile $targetgeometry
# capabilities

3. Declare the thumbnail as candidate

The script /ffp/opt/twonky/cgi-bin/thumbnail-candidate will be called by TwonkyMedia server everytime it needs a thumbnail generation.

Its main role will be to generate a temporary file to declare the thumbnail as candidate for generation.

The temporary file will hold all the data needed to generate the thumbnail :

  • path of the video file
  • path of the thumbnail to generate in the cache
  • geometry needed width x height

All temporary files will be generated under /tmp following the twonky-thumb-xxxxxx canvas.


# $1 : full path of video file
# $2 : full path of thumbnail to generate
# $3 : geometry (ex 160x160)

# go to tmp directory
cd /tmp

# generate temporary filename
TMPFILE=`mktemp twonky-thumb-XXXXXX`

# generate temporary file with thnumbnail information
echo "$1;$2;$3" > "$TMPFILE"

This script needs to be executable :

# chmod +x /ffp/opt/twonky/cgi-bin/thumbnail-candidate

4. Generate thumbnails as a background process

The background thumbnails generation process run as a daemon and it is in charge to periodically :

  • check if some candidates are there
  • get the candidates data
  • generate the thumbnails
  • remove some Twonky flags indicating that thumbnail is not available

For the thumbnail generation, the .tbn search is done in 3 steps :

  1. if a .tbn is available for the video file, it is used
  2. if a .tbn is available for the parent directory, it is used
  3. otherwise, a default ffp/opt/twonky/thumbnail-video.jpg is used (it can be any size but should have a square aspect ratio)

All thumbnail generations are logged in /ffp/var/log/twonky-thumbnail.log


# ------------------------------------------------------------------------
# Daemon script to generate twonky thumbnails
# Conversions to be done are given by
# the files with /tmp/twonky-thumb-* mask
# Revision history :
#  31/07/2012, V1.0 - Creation by N. Bernaerts
#  17/05/2013, V1.1 - Corrections (thanks to Valeriy) and logging added
# ------------------------------------------------------------------------

# set default files (log, thumbnail)

# set separator as line feed

# endless loop
while [ true ]
  # loop through the .waiting files
  for TMPFILE in `find /tmp -name "twonky-thumb-*"`; do
    # read content of thumb file
    CHAIN=`cat "$TMPFILE"`
    rm -f "$TMPFILE"

    # get data out of the content
    FILE_VIDEO=`echo ${CHAIN} | cut -d";" -f1`
    FILE_THUMB=`echo ${CHAIN} | cut -d";" -f2`
    FILE_GEO=`echo ${CHAIN} | cut -d";" -f3`

    # step 1 : generate name of thumbnail .tbn for video file
    FILENAME=`echo "$FILE_VIDEO" | sed 's/^\(.*\)[.].*$/\1\.tbn/g'`

    # step 2 : if thumbnail .tbn doesn't exist, generate name of thumbnail .tbn for parent directory
    if test ! -f "$FILENAME"
      FILENAME=`echo "$FILE_VIDEO" | sed 's/^\(.*\)[\/].*$/\1\.tbn/g'`

    # step 3 : if thumbnail .tbn doesn't exist, use default video icon
    if test ! -f "$FILENAME"

    # generate thumbnail from .tbn with lowest priority
    nice -19 convert -geometry "$FILE_GEO"! "$FILENAME" "$FILE_THUMB"

    # remove .unsupported file
    rm -f "$FILE_THUMB.unsupported"

    # log the thumbnail generation
    echo "$FILE_THUMB generated from $FILENAME" >> $FILE_LOG

  # wait for 5 seconds
  sleep 5

The script needs to be executable :

# chmod +x /ffp/opt/twonky/thumbnail-generate

For the daemon to be started and stopped at the same time as TwonkyMedia server, you need to add the following lines in your original /ffp/start/twonky.sh :



# start twonky server process
/ffp/opt/twonky/twonkystarter -logfile /dev/null -appdata /ffp/opt/twonky/data/ >/dev/null 2>/dev/null

# start the thumbnail generation process
/ffp/opt/twonky/thumbnail-generate &

# kill the thumbnail generation process
killall thumbnail-generate

# kill the twonky processes
killall twonkystarter


5. Test Thumbnail Generation

Everything is ready ... you just need to restart TwonkyMedia server for the video thumbnail generation to be operationnal :

# /ffp/start/twonky.sh restart

You can check that both daemon are running :

# ps -ef | grep twonky
25264 root /ffp/opt/twonky/twonkymediaserver -logfile /dev/null -appdata /ffp/opt/twonky/data/
19800 root /ffp/opt/twonky/twonkyserver -logfile /dev/null -appdata /ffp/opt/twonky/data/
21804 root /ffp/bin/sh /ffp/opt/twonky/thumbnail-generate

When you browse your videos from your TV, the first time you browse a folder you will see a generic icon for every file.

All these files will be declared as candidate for thumbnail generation.

After some time, from seconds to minutes, if you browse back the folder, some thumbnails identical to your XBMC .tbn files will be available.


That's it !

Signature Technoblog

This article is published "as is", without any warranty that it will work for your specific need.
If you think this article needs some complement, or simply if you think it saved you lots of time & trouble,
just let me know at This email address is being protected from spambots. You need JavaScript enabled to view it.. Cheers !

icon linux icon debian icon apache icon mysql icon php icon piwik icon googleplus