Last active
December 18, 2018 15:15
-
-
Save tincho/6628bde69c858654d7fb79ae3eacb155 to your computer and use it in GitHub Desktop.
Get an image preview from a video URL with vainilla JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// make a data uri encoded image from a <video> element using canvas | |
// (thank you stackoverflow people) | |
function videoElementToImage(videoElement, width, height) { | |
width = width || 360; | |
height = height || 240; | |
var canvas = document.createElement('canvas'); | |
canvas.width = width; | |
canvas.height = height; | |
var context = canvas.getContext('2d'); | |
context.drawImage(videoElement, 0, 0, canvas.width, canvas.height); | |
var dataURI = canvas.toDataURL('image/jpeg'); | |
// discard created canvas element | |
canvas = null; | |
return dataURI; | |
} | |
// get a data uri encoded image from a video URL | |
function fetchVideoThumb(url, width, height) { | |
width = width || 360; | |
height = height || 240; | |
// will be async as we need to wait some frame to be downloaded | |
return new Promise(function(resolve, reject) { | |
var video = document.createElement('video'); | |
video.width = width; | |
video.height = height; | |
video.attributes['preload'] = 'none'; | |
// first setup the listeners | |
video.addEventListener('loadeddata', function() { | |
var dataURI = videoElementToImage(video, width, height); | |
// cleanup so loading ends | |
video = null; | |
// we're done, here's your thumb: | |
resolve(dataURI); | |
}); | |
video.addEventListener('error', function() { | |
// remove element in case of error | |
video = null; | |
reject(); | |
}); | |
// then set url so load begin | |
video.src = url; | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var someImage = new Image(); | |
var someElement = document.createElement('div'); | |
// usage: | |
fetchVideoThumb('/videos/1234.mp4').then(function(thumb) { | |
someImage.src = thumb; | |
// or | |
someElement.style['background-image'] = thumb; | |
}, function() { | |
console.log('couldnt load the video!'); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
MIT LICENSE
Copyright <2018>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.