Video Thumbnail Generation

So after I fixed my Vista Media Center yesterday, I got tired of how long it took to generate thumbnails for the video files in Media Center. I understand it has to open each video file and create a thumbnail from that and since all my videos are located on a file server (even though it’s on a gigabit network), I can see why it’s taking awhile. Of course it caches it and stores it into ehThumbs.db, but once in awhile, it decides that those thumbnails are out of date and clears and redoes everything.

I had learnt a trick that if you have a jpg with the same file name (i.e. video1.avi and video1.jpg), Media Center will load that image instead of generating its own. I’m actually not sure if Media Center 2005 supports this, but I know Vista Media Center does. So I decided to check how fast it took to load all those images on a test directory and BOY WAS IT FAST! I had just taken a random image and copied it a bunch of times after naming it after the video files in that directory.

So now I needed a video thumbnail generation software, hopefully something that will automatically go through directories, create these jpgs images, and then name it correctly. Sounded like a very specific program and a brief search resulted in nothing. It did however give me an idea that instead of generating a thumbnail, I can maybe go with a screenshot. I know Media Player Classic and a couple other video software I have allows me to take screenshots, but having almost 1,000 video files, doing that manually would really suck.

So I talked to RayAlome and we decided the best way to do was find a program that can take screenshots from videos via a command line, which I can then script to automatically run that line on all the videos under a certain directory and subdirectories. RayAlome suggested virtualdub, but I decided to take a look at my Gallery and see what it uses to generate thumbnails for the video files. It uses this thing called FFmpeg, which somehow led me to MPlayer. Apparently MPlayer allows you to save screenshots via command line after reading this: Re: Video output to jpeg/png file and Tools for extracting individual frames of an AVI movie file.

So I decided to give it a try:
mplayer.exe -vo jpeg -frames 1 -ss 300 X:\video1.avi

and it worked! I got an image named 00000001.jpg in the current directory. Renaming and moving the file should be simple enough. What was funny was the fact when it created the screenshot, audio was played for about a second. I knew that’d get annoying if it was processing hundreds or thousands of video files, so I added the -nosound option to it.

I got off starting to write a script. I did mine in Perl, though any scripting language would’ve worked. I realized my Perl knowledge was quite lacking after not using it for so long, but searching for Perl commands online wasn’t that bad. Interestingly enough, Perl doesn’t have a way for you to check if an item exists in an array (or none that I saw) and you had to inverse the array (as a hashtable) and check by calling the hashtable’s key. Another weird thing is that their way of defining methods and functions really sucks. They’re actually called sub (short for subroutines) and looks rather ugly.

Anyway, what the final script does is take a starting directory (current working directory if no starting directory is specified) and finds all the files with the preset list of extensions (changeable) and generates thumbnails for it. You’ll need to download MPlayer for Windows and update the path to it in the script.

There’s also a few other defaults you can change. I set the default time to take a screenshot at 300 seconds (5 minutes).

Another thing I added was to skip processing videos that already had an associating screenshot/thumbnail. Since I was testing this script out quite a bit, I realized that it was redoing a lot of screenshots that already existed. However, you can pass in the -f option and it’ll force it to regenerate everything under the starting directory.

One thing you must realize is that if the video file is not long enough to generate a screenshot at the given time, it just skips it. My HD trailers typically run from about 30 seconds to 2 minutes and having a 5 minute capture time basically skips over all of those. I then reran the script with a screenshot time of 30 seconds in that directory and everything came out fine.

Since I feel that others may be in my situation, I’ve decided to share the script here in case anyone wants to use it:
Generate Video Thumbnails for Media Center Perl Script

Usage: generateThumbnails.pl [-OPTIONS [-MORE_OPTIONS]]

OPTIONS:
-d StartingDirectory Sets the starting directory [cwd]
-f Forces Thumbnail Generation [off]
-t TimeInSecs Time in video to grab screenshot [300s]

Use this script at your own risk. I will not take any responsibility for any damage it causes. Of course to use this, you’ll need perl for your system. I also have cygwin installed, so I’m not sure if there’s any environmental differences.

As noted earlier, these are generating screenshots, not thumbnails. So depending on the resolution of your video, it can result in a screenshot that varies from about 30KB to 100KB. I’ve thought about finding a command line utility to shrink the screenshots to thumbnail sizes, but for now, this will work and until either it becomes intolerably slow again or if someone has knowledge of how to do so and is willing to share it with me, I’m probably not going to add this feature any time soon.

After running this script on all my video files, I will have to say that browsing videos in Media Center became a lot quicker and faster. Enjoy!

6 Replies to “Video Thumbnail Generation”

  1. Hi Krunk,
    thanks a lot for this, I have been looking for a solution adressing this problem since I migrated from MCE 2005 to Vista MCE…I have a lot of videos and browsing thru the vids directory has become a nightmare in Vista.

    I have just tested it using ActivePerl and it just worked like a charm.
    Again, thanks, thanks, thanks a lot for your work!

  2. You’re very welcome. I’ve been updating the script to support skipping over videos that already have images and and automatically decreasing the screenshot time if the video is too short.

    When I get the chance, I’ll upload that.

  3. Hi,

    Just another thank you note…this works really well and fixes a problem with Vista that has cost me 4/5 days of resarching fixes, install reg fix`s and codec packs to no avail..if only I had spotted this earlier !!

    Having used MCE with almost no problems to have this thumbnail issue finally fixed means I can now stop fiddling around and actual start watching my content !!!

    Thanks again.

  4. To scale it down to thumbnail size, add “-vf scale=sizex:sizey”

    ie. mplayer -vo jpeg -frames 1 -ss 30 -nosound -vf scale=320:240 test.mpeg

Leave a Reply to vodka Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.