I’ve been working on a video streaming macOS app for a while, and I need to find a way to get the thumbnail of a bunch of videos. Just like many other things, there are multiple ways of achieving this. So in this post I will show you the two solutions I found and do some comparison.

### AVAssetImageGenerator

Well Cocoa does offer a native API for this job. Using AVAssetImageGenerator we can easily get the thumbnail of a video:

### qlmanage

qlmanage is the CLI utility for QuickLook in macOS (here‘s the man page). The command we are using is qlmanage -t, as described in the man page:

qlmanage -t displays the Quick Look generated thumbnails (if available) for the specified files

So we can use it to get a video thumbnail like this:

A above command will save a file named video.mp4.png to the destination folder we specified.

To use it in our Cocoa app, we need to wrap it as an NSTask:

### Comparison

I download Rick Astley - Never Gonna Give You Up from YouTube as a .mp4 file for testing. Its file size is 47.3MB.

I implemented two versions of thumbnailWithFilePath: andHandler: using the two methods mentioned above.

AVAssetImageGenerator version:

qlmanage version (same of above):

And here are the thumbnails generated by the two methods:

AVAssetImageGenerator thumbnail:

qlmanage thumbnail:

With above examples, we can instantly see their pros and cons.

AVAssetImageGenerator generate thumbnail without the annoying black border, but since we need to specific the time of the thumbnail, we might be getting thumbnail that doesn’t look so good, like the example above.

On the other hand, with qlmanage you are pretty much guaranteed to get a nice and clear thumbnail every time, but they always come with the black border and shadow.

What about the efficiency of the two solutions? Using two clever macros provided in this SO answer, I did a mini benchmark:

And here’s the result:

• AVAssetImageGenerator: 0.344054s
• qlmanage: 0.891336s

So we see that qlmanage is much slower than AVAssetImageGenerator. I think it’s due to the fact that qlmanage version involves writing the PNG file and deleting it.

### Conclusion

At the end I used the qlmanage method in my app, since most of the time it produces much better thumbnail than using AVAssetImageGenerator. However if efficiency is your main concern, I would recommend using AVAssetImageGenerator.