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:
- ffmpegthumbnailer only works with absolute file paths, whereas Nautilus by default supplies file URIs;
- 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
## START OF SCRIPT
## ITERATE THROUGH THE FLAGS
while getopts :s:i:o: OPTION
case “$OPTION” in
[?]) exit -1;;
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).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! 🙂