Getting Video Thumbnail in Cocoa

Mar 5, 2017

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:

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:

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.