fixed the logic to extract frames from mp4.

This commit is contained in:
bruefire
2023-12-15 04:30:47 +09:00
parent 4b6b2197a8
commit 83ed22b78e
2 changed files with 15 additions and 12 deletions
+13 -11
View File
@@ -264,31 +264,33 @@ app.registerExtension({
async function getFramesFromVideo(file) {
const imageType = 'image/png';
let frameCount = 0;
let tempCanvas = document.createElement('canvas');
let ctx = tempCanvas.getContext("2d");
let tWidth, tHeight;
let videoUrl = URL.createObjectURL(file);
let vframes = [];
let fCanvases = [];
await getVideoFrames({
videoUrl,
async onFrame(vframe) { // `frame` is a VideoFrame object
vframes.push(vframe);
let fCanvas = document.createElement('canvas');
fCanvas.width = tWidth;
fCanvas.height = tHeight;
fCanvas.getContext('2d').drawImage(vframe, 0, 0, fCanvas.width, fCanvas.height);
vframe.close();
fCanvases.push(fCanvas);
},
onConfig(config) {
tempCanvas.width = config.codedWidth;
tempCanvas.height = config.codedHeight;
tWidth = config.codedWidth;
tHeight = config.codedHeight;
},
onFinish() {
},
});
let frames = await Promise.all(
vframes.map(vframe =>
fCanvases.map(fCanvas =>
new Promise((resolve, reject) => {
ctx.drawImage(vframe, 0, 0, tempCanvas.width, tempCanvas.height);
vframe.close();
tempCanvas.toBlob(blob => {
fCanvas.toBlob(blob => {
if (blob) {
blob.name = ("00000000" + frameCount++).slice(-8) + ".png";
resolve(blob);
+2 -1
View File
@@ -24,7 +24,8 @@ This is in the Image category.
### About "VideoFrameExtractor" Node
Instead of specifying a directory containing frames, you specify a video file. Currently, only mp4 format is supported.
Otherwise, it is the same as the SequentialImageLoader Node.
Otherwise, it is the same as the SequentialImageLoader Node.
(I used [getVideoFrames.js](https://github.com/josephrocca/getVideoFrames.js) for extracting frames from MP4)
### About the Mask Editor
1. After loading the frames, right-click the node and select "Open In MaskEditor".