Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a callback for frame encoded end - useful to show progress in the UI while movie is being generated + Improved .compile method documentation #48

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ That `15` over there is the frame rate. There's a way to set the individual dura

Here, you can add a frame, this happens fairly quickly because basically all it's doing is running `.toDataURL()` on the canvas (which isn't exactly a speed-demon either, but it's acceptable enough most of the time) and plopping the result onto an array (no computation or anything). The actual encoding only happens when you call `.compile()`

encoder.compile(function(output){});
encoder.compile(outputAsArray, function (output) {
//outputAsArray is false -> output is a Blob (of "video/webm" type)
//outputAsArray is true -> output is an Uint8Array
}, frameEncodedCallback);

Here, output is set to a Blob. In order to get a nice URL which you can use to stick in a `<video>` element, you need to send it over to `createObjectURL`

Expand Down
11 changes: 7 additions & 4 deletions whammy.js
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ window.Whammy = (function(){
};

// deferred webp encoding. Draws image data to canvas, then encodes as dataUrl
WhammyVideo.prototype.encodeFrames = function(callback){
WhammyVideo.prototype.encodeFrames = function(callback, frameEncodedCallback){

if(this.frames[0].image instanceof ImageData){

Expand All @@ -531,6 +531,10 @@ window.Whammy = (function(){
var frame = frames[index];
tmpContext.putImageData(frame.image, 0, 0);
frame.image = tmpCanvas.toDataURL('image/webp', this.quality);
frameEncodedCallback({
frameIndex: index + 1,
framesTotal: frames.length
});
if(index < frames.length-1){
setTimeout(function(){ encodeFrame(index + 1); }, 1);
}else{
Expand All @@ -544,7 +548,7 @@ window.Whammy = (function(){
}
};

WhammyVideo.prototype.compile = function(outputAsArray, callback){
WhammyVideo.prototype.compile = function(outputAsArray, callback, frameEncodedCallback){

this.encodeFrames(function(){

Expand All @@ -554,8 +558,7 @@ window.Whammy = (function(){
return webp;
}), outputAsArray);
callback(webm);

}.bind(this));
}.bind(this), frameEncodedCallback);
};

return {
Expand Down