Question: I am trying to implement a video retrieval system and I need to first extract key frames from a video, ideally I want to have a library for automatically detecting those key frames, one key frame from each shot. Bonus if I can configure which key frame to extract(first, middle, or last of the continuous frames in a shot).
Is there an open source implementation for this?

Answer: 
What you're after is called shot segmentation. While it's a pretty active research area, I think you're unlikely to find any complete libraries that solve the problem for you out-of-the-box. Your best option may be reading up on the topic, selecting an approach that best fits your requirements and coding it up yourself.

One approach is to calculate the chi-squared distance between the color histograms of adjacent frames. When this distance rises above a user-specified threshold, you are at a shot boundary. The approach is explained in [this paper](http://dl.acm.org/citation.cfm?id=719786):

> 
> A. Nagasaka and Y. Tanaka, "Automatic video indexing and full-video
>  search for object appearances", Journal of Information Processing
>  archive, Volume 15, Issue 2 (1992), Page 316
> 
> 
> 

I've played around with it, with some success. Notable failures are sudden light changes within a single shot (caused by camera flash, etc) and blending shot changes.

Once you know the shot boundaries, picking a keyframe from each shot will be trivial, as other people have pointed out.
If you have as an input an uncompressed video, you can download the ffmpeg from [www.ffmpeg.org] and use this executable to decompress your video stream into its frames.

Subsequently, in order to detect your shot boundaries you have to extract some features from the video keyframes. An efficient technique for shot boundary detection is proposed in [MediaMixer Deliverable, D1.1.2](http://www.mediamixer.eu/wordpress/wp-content/uploads/2012/11/D1.1.2-final_Update1.pdf). In addition, in MediaMixer Webinars you can find the lecture [“Fragmenting your Media Assets meaningfully – media analysis for fragment detection and extraction”](http://videolectures.net/mediamixer_mezaris_media_assets/) where this shot-boundary detection implementation is presented, while you can also visit the MediaMixer demonstrator [http://www.mediamixer.eu/automatic-fragmentation-and-annotation-for-improved-access-to-lecture-videos/] where the results of video shot segmentation and concept detection are visualized.