Skip to content

Instantly share code, notes, and snippets.

@ispedals
Created May 20, 2013 16:19
Show Gist options
  • Save ispedals/5613344 to your computer and use it in GitHub Desktop.
Save ispedals/5613344 to your computer and use it in GitHub Desktop.
Experiment creating thumbnails of MP4 videos using the HTML5 Filereader API. Currently, the loading of videos in rapid succession fails with a decoding error, and if too many videos are processed concurrently, the display driver crashes.
<!DOCTYPE html>
<html lang="en">
<head>
<link href="https://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css" rel="stylesheet">
<script src="http://code.jquery.com/jquery.js"></script>
<script src="https://rawgithub.com/caolan/async/master/lib/async.js"></script>
<script src="https://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/js/bootstrap.min.js"></script>
<script>
window.URL = window.URL || window.webkitURL;
function handleFiles(files) {
async.eachSeries(files, do_video);
}
function do_video(file, callback) {
var url = window.URL.createObjectURL(file);
screenshotOfFile(url, function (elem) {
$('<li class="span4"></li>').appendTo('.thumbnails').append(elem);
callback();
}, callback);
}
function screenshotOfFile(url, callback, error) {
var video = $("video")[0];
video.addEventListener("loadeddata", function () {
this.removeEventListener("loadeddata", arguments.callee);
this.removeEventListener("error", arguments.callee);
var elem = screenshot(this);
callback(elem);
});
video.addEventListener("error", function () {
this.removeEventListener("error", arguments.callee);
console.log(this.error);
this.removeEventListener("loadeddata", arguments.callee);
this.src = '';
error();
});
video.src = url;
}
function screenshot(video) {
var canvas = document.createElement("canvas");
canvas.height = video.videoHeight;
canvas.width = video.videoWidth;
var ctx = canvas.getContext("2d");
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
canvas.style.width = 'inherit';
canvas.style.height = 'inherit';
return canvas;
}
</script>
</head>
<body>
<video id="player"></video>
<input type="file" id="fileElem" multiple onchange="handleFiles(this.files)">
<ul class="thumbnails"> </ul>
</body>
</html>
@osv
Copy link

osv commented Apr 7, 2016

to make it work...

    <script src="https://cdn.jsdelivr.net/async/2.0.0-rc.2/async.js"></script>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment