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

Lily/Video: Add new 'frame of video (my video) at (0) seconds' block #1536

Closed
wants to merge 3 commits into from
Closed
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
57 changes: 57 additions & 0 deletions extensions/Lily/Video.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,22 @@
},
},
},
{
// Frame Block Added By SharkPool
opcode: "getFrame",
blockType: Scratch.BlockType.REPORTER,
text: Scratch.translate("frame of video [NAME] at [TIME] seconds"),
arguments: {
TIME: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: 0,
},
NAME: {
type: Scratch.ArgumentType.STRING,
defaultValue: "my video",
},
},
},
"---",
{
opcode: "pause",
Expand Down Expand Up @@ -466,6 +482,47 @@
}
}

getFrame(args) {
const time = Cast.toString(args.TIME);
const videoName = Cast.toString(args.NAME);
const videoSkin = this.videos[videoName];
if (!videoSkin) return "";

const videoElement = videoSkin.videoElement;
const canvas = document.createElement("canvas");
const context = canvas.getContext("2d");
if (!context) {
console.warn("2D rendering context not available");
return "";
}
if (videoElement.readyState >= 1) {
return new Promise((resolve, reject) => {
canvas.width = videoElement.videoWidth;
canvas.height = videoElement.videoHeight;
videoElement.currentTime = time;
// Timeout in case browser fails to seek (2 Seconds should be enough?)
const timeout = setTimeout(() => {
console.warn("browser wont perform 'seek' operation");
reject("Couldnt get frame, check console for more info");
}, 2000);
const onSeeked = () => {
clearTimeout(timeout);
try {
context.drawImage(videoElement, 0, 0, canvas.width, canvas.height);
resolve(canvas.toDataURL());
} catch (error) {
console.warn(error);
reject("Couldnt get frame, check console for more info");
} finally {
videoElement.removeEventListener("seeked", onSeeked);
}
};
videoElement.addEventListener("seeked", onSeeked, { once: true });
});
}
return "";
}

pause(args) {
const videoName = Cast.toString(args.NAME);
const videoSkin = this.videos[videoName];
Expand Down
Loading