| import { app } from '../../../scripts/app.js' |
|
|
|
|
| function getVideoMetadata(file) { |
| return new Promise((r) => { |
| const reader = new FileReader(); |
| reader.onload = (event) => { |
| const videoData = new Uint8Array(event.target.result); |
| const dataView = new DataView(videoData.buffer); |
|
|
| let decoder = new TextDecoder(); |
| |
| if (dataView.getUint32(0) == 0x1A45DFA3) { |
| |
| |
| |
| |
| |
| |
| |
| |
| let offset = 4 + 8; |
| while(offset < videoData.length-16) { |
| |
| if (dataView.getUint16(offset) == 0x4487) { |
| |
| const name = String.fromCharCode(...videoData.slice(offset-7,offset)); |
| if (name === "COMMENT") { |
| let vint = dataView.getUint32(offset+2); |
| let n_octets = Math.clz32(vint)+1; |
| if (n_octets < 4) { |
| let length = (vint >> (8*(4-n_octets))) & ~(1 << (7*n_octets)); |
| const content = decoder.decode(videoData.slice(offset+2+n_octets, offset+2+n_octets+length)); |
| let json = JSON.parse(content); |
| r(json); |
| return; |
| } |
| } |
| } |
| offset+=1; |
| } |
| } else if (dataView.getUint32(4) == 0x66747970 && dataView.getUint32(8) == 0x69736F6D) { |
| |
| |
| |
| let offset = videoData.length-4; |
| while (offset > 16) { |
| if (dataView.getUint32(offset) == 0x64617461) { |
| if (dataView.getUint32(offset - 8) == 0xa9636d74) { |
| let type = dataView.getUint32(offset+4); |
| let locale = dataView.getUint32(offset+8); |
| let size = dataView.getUint32(offset-4) - 4*4; |
| const content = decoder.decode(videoData.slice(offset+12, offset+12+size)); |
| const json = JSON.parse(content); |
| r(json); |
| return; |
| } |
| } |
|
|
| offset-=1; |
| } |
| } else { |
| console.error("Unknown magic: " + dataView.getUint32(0)) |
| } |
| r(); |
| return; |
| }; |
|
|
| reader.readAsArrayBuffer(file); |
| }); |
| } |
| function isVideoFile(file) { |
| if (file?.name?.endsWith(".webm")) { |
| return true; |
| } |
| if (file?.name?.endsWith(".mp4")) { |
| return true; |
| } |
| if (file?.name?.endsWith(".mkv")) { |
| return true; |
| } |
|
|
| return false; |
| } |
|
|
| let originalHandleFile = app.handleFile; |
| app.handleFile = handleFile; |
| let fileInput = document.getElementById("comfy-file-input") |
| |
| fileInput.accept += ",video/webm,video/mp4,video/x-matroska"; |
|
|
| async function handleFile(file) { |
| if (file?.type?.startsWith("video/") || isVideoFile(file)) { |
| const videoInfo = await getVideoMetadata(file); |
| if (videoInfo?.workflow) { |
| await app.loadGraphData(videoInfo.workflow); |
| return |
| } |
| } |
| return await originalHandleFile.apply(this, arguments); |
| } |
|
|