Skip to content

Instantly share code, notes, and snippets.

@giannif
Last active August 29, 2015 14:10
Show Gist options
  • Save giannif/63c3e124e826d3e22b0a to your computer and use it in GitHub Desktop.
Save giannif/63c3e124e826d3e22b0a to your computer and use it in GitHub Desktop.
/* global _ */
var Thumbnails = {
// util to get the number of 0s
getTimeString: function(chars) {
var s = "";
_.times(4 - chars, function() {
s += "0";
});
return s;
},
// pass in the mediaGen.image and the current time of the video
getThumbnail: function(images, time) {
// we want the lowest time, e.g. 29.9 = 29
time = Math.floor(time);
// find the image that is in the range
var foundImage = _.find(images, function(image) {
return time >= image.startTime && time < image.endTime;
});
if (foundImage) {
// get the number
var thumbnailNumber = Math.floor((time - foundImage.startTime) / foundImage.keyframeIntervalSeconds),
// get the padding
padding = Thumbnails.getTimeString(thumbnailNumber.toString().length);
// return the src
return foundImage.src.replace("{0:0000}", padding + thumbnailNumber);
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment