Twiddling Your Thumbs – The MKV Thumbnail Problem in Ubuntu

Ubuntu Lucid is a marvelous OS.

Thought I should just come out and say it. It’s a gem of an OS – fast, stable, works with almost anything out-of-the-box, and to top it all off, an LTS release – meaning it gets supported for two and a half years by the Ubuntu people. In all, I’ve been satisfied with Lucid since when I’d first installed it.

But as always, there are some points that really could do with a bit of improvement – and the most major among them, to me, is the default video thumbnailer.

Lucid uses the thumbnailer that comes with totem, its default movie player. It’s not bad, per se, but it has some serious… limitations. For example, suppose you have a large mkv file (I mean on the order of 400-500 MBs to GBs – common, in the era of 720p/1080p video encodes). Totem might successfully generate a thumbnail for the file, it might not – and the chances dwindle as the file size gets larger. This has something to with how long the thumbnailer can run before it decides to time out and leave in disgust. Also suppose if you have a folder with tens (or worse, hundreds) of such videos, in which case opening it with Nautilus makes it run the thumbnailer for each file, meaning CPU usage can climb to 100% in the blink of an eye – and stay there. The program hogs CPU like me in a coffee shop. 😉

The answer? Of course there is one – this is Linux, after all! The answer is ffmpegthumbnailer, a fast and lightweight thumbnail generator that can speed up thumbnail generation by an order of ten or more. A simple apt-get can install this for you:

sudo apt-get install ffmpegthumbnailer

Now pay attention – you can’t just get Nautilus to use this vanilla program for your videos! Some tweaks are needed, for various reasons:

  1. ffmpegthumbnailer only works with absolute file paths, whereas Nautilus by default supplies file URIs;
  2. totem has some internal logic that makes it seek a frame with useful content in it (e.g. faces), whereas ffmpegthumbnailer has to be provided the actual frame to which it should seek to.

To escape these limitations, we need to use a wrapper over the program. Let’s write a wrapper script (called ffmpegthumbnailer_script for convenience) in /usr/local/bin and make it executable:

coderman@coderman-desktop:~$ sudo su –
[sudo] password for coderman:
root@coderman-desktop:~# cd /usr/local/bin
root@coderman-desktop:~# cat > ffmpegthumbnailer_script
#!/bin/bash

## START OF SCRIPT

## ITERATE THROUGH THE FLAGS
while getopts :s:i:o: OPTION
do
case “$OPTION” in
s) size=”$OPTARG”;;
i) inputURL=”$OPTARG”;;
o) outputImage=”$OPTARG”;;
[?]) exit -1;;
esac
done
shift $(($OPTIND – 1))

## ESCAPE THE SINGLE QUOTES IN FILE URI
inputURLWithEscapedSingleQuote=$(echo “$inputURL” | sed “s/’/\\’/g”)

## CONVERT FILE URI TO ABSOLUTE FILE PATH
inputFile=$(python -c ‘import gio,sys; print(gio.File(sys.argv[1]).get_path())’ “$inputURLWithEscapedSingleQuote”)

## RANDOMIZE THE PLACE THE PROGRAM SHOULD SEEK TO FOR THUMBNAIL GENERATION – between 20%-60%
seekThumbnailAt=$((RANDOM%60 + 20))

## GENERATE THUMBNAIL
ffmpegthumbnailer -f -s “$size” -i “$inputFile” -o “$outputImage” -t “$seekThumbnailAt”

## END OF SCRIPT
root@coderman-desktop:~#chmod +x ffmpegthumbnailer_script

Oh, and you need python installed for the script to work – any version will do. And obviously you can use gedit or vi or any other editor too to write the script. 🙂

Finally fire up gconf-editor from the terminal, then in the GUI go to /desktop/gnome/thumbnailers/video@x-matroska and edit the thumbnailer command to:

/usr/local/bin/ffmpegthumbnailer_script -s %s -i %u -o %o

and check the enable option.

Now sit back, wait and enjoy your fast-and-sweet mkv thumbnails – I know I did! 🙂

Advertisements

About inkthief

A Hindu by birth and an Atheist by choice. An electronics engineer by education (or the lack thereof, but I blame the whiskey, really) and a programmer by vocation. A reader by addiction and a writer by compulsion. I'd like to be a nice person, and sometimes I am. I'd like to be sociable, and sometimes I am. I'd like to be Hemingway, but I'm still not. Waiting for the world to change, so I no longer need to - and so I can claim the credit. Cheers, folks. Worship me, for I can be a tolerant god and will accept beer in lieu of virginal sacrifices. ;-)
This entry was posted in Linux and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s