This script is designed to get a video frame from the input and use it as an image. I know this works, I had this code structured originally in a different way but I want to use a return to get the video thumbnail instead but this section of the code
canvas.toBlob(function(blob){
...
}
Makes it very difficult to get the return value. I tried a global variable and still no luck and I even check this link out on here
access blob value outside of canvas.ToBlob() async function
and many different kind of links but I'm still having difficulties understanding how I can implement those answers to my script. This return is returning as undefined so how can I get that generated video thumbnail link as a return value successfully?
This is my code example.
document.addEventListener('DOMContentLoaded', function(){
document.querySelector('input').addEventListener('change', video_thumbnail_grabber__starter);
function video_thumbnail_grabber__starter(){
var this_input = document.querySelector('input').files[0];
var blob = new Blob([this_input],{type: 'video/mp4'});
var object_url = URL.createObjectURL(blob);
var media_link = object_url;
var thumbnail = video_thumbnail_grabber(
video_source = media_link,
time = 61,
scale_factor = 0.25
);
document.querySelector('img').setAttribute('src', thumbnail);
}
function video_thumbnail_grabber(
video_source,
time,
scale_factor,
for_this_function
){
var video = document.createElement('video');
video.addEventListener('loadeddata', function(){
video.currentTime = time;
});
video.addEventListener('seeked', function(){
//<Best highest quality generation method found>
if(scale_factor == null){
scale_factor = 1;
}
var w = video.videoWidth * scale_factor;
var h = video.videoHeight * scale_factor;
var canvas = document.createElement('canvas');
canvas.width = w;
canvas.height = h;
var ctx = canvas.getContext('2d');
ctx.drawImage(video, 0, 0, w, h);
//</Best highest quality generation method found>
//<Edge and IE polyfill for toBlob>
if(!HTMLCanvasElement.prototype.toBlob){
Object.defineProperty(HTMLCanvasElement.prototype, 'toBlob', {
value: function (callback, type, quality) {
var canvas = this;
setTimeout(function() {
var binStr = atob( canvas.toDataURL(type, quality).split(',')[1] ),
len = binStr.length,
arr = new Uint8Array(len);
for (var i = 0; i < len; i++ ) {
arr[i] = binStr.charCodeAt(i);
}
callback( new Blob( [arr], {type: type || 'image/png'} ) );
});
}
});
}
//</Edge and IE polyfill for toBlob>
canvas.toBlob(function(blob){
var video_thumbnail_link = URL.createObjectURL(blob);
return video_thumbnail_link; //<-This return is not going through.
});
});
video.preload = 'auto';
video.src = video_source;
}
});
<input type='file'>
<img src=''>