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 -- Clamp Volume Between 0 and 1, & Add "get frame of video at (seconds)" #1595

Merged
merged 4 commits into from
Aug 6, 2024
Merged
Changes from 1 commit
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
59 changes: 58 additions & 1 deletion extensions/Lily/Video.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// ID: lmsVideo
// Description: Play videos from URLs.
// By: LilyMakesThings <https://scratch.mit.edu/users/LilyMakesThings/>
// Contributed to by: SharkPool
// License: MIT AND LGPL-3.0

// Attribution is not required, but greatly appreciated.
Expand Down Expand Up @@ -239,6 +240,21 @@
},
},
},
{
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 Expand Up @@ -500,7 +557,7 @@
const videoSkin = this.videos[videoName];
if (!videoSkin) return;

videoSkin.videoElement.volume = value / 100;
videoSkin.videoElement.volume = Math.min(1, Math.max(0, value / 100));
}

/** @returns {VM.Target|undefined} */
Expand Down
Loading