UNPKG

guessit-js

Version:

GuessIt JS (WASM) - Extract metadata from video filenames with WebAssembly performance

132 lines (113 loc) 3.68 kB
/** * WebAssembly loader and wrapper for GuessIt */ let wasmModule = null; let wasmReady = false; /** * Initialize the WASM module */ export async function initWasm() { if (wasmReady) return wasmModule; try { // In a real implementation, you'd load the compiled WASM file // For now, we'll simulate WASM functionality with JavaScript wasmModule = { _guessit_wasm: (filenamePtr) => { // Simulate WASM string handling const filename = readString(filenamePtr); const result = guessitWasmSim(filename); return allocateString(result); }, _guessit_version: () => { return allocateString("GuessIt WASM 1.0.0"); }, _malloc: (size) => { // Simulate memory allocation return new ArrayBuffer(size); }, _free: (ptr) => { // Simulate memory deallocation } }; wasmReady = true; return wasmModule; } catch (error) { console.error('Failed to initialize WASM module:', error); throw error; } } /** * WASM-compatible guessit function */ export async function guessitWasm(filename) { if (!wasmReady) { await initWasm(); } if (!wasmModule) { throw new Error('WASM module not initialized'); } try { // For simulation, directly call the simulation function const result = guessitWasmSim(filename); return JSON.parse(result); } catch (error) { console.error('WASM guessit error:', error); throw error; } } /** * Get WASM version */ export async function getWasmVersion() { if (!wasmReady) { await initWasm(); } return "GuessIt WASM 1.0.0"; } // Helper functions for WASM string handling function allocateString(str) { const encoder = new TextEncoder(); const bytes = encoder.encode(str + '\0'); const ptr = wasmModule._malloc(bytes.length); // In real WASM, you'd write to memory here return ptr; } function readString(ptr) { // In real WASM, you'd read from memory here // For simulation, return the actual result from guessitWasmSim return guessitWasmSim(ptr.filename || ''); } // Simulation of the C WASM function function guessitWasmSim(filename) { const result = {}; // Basic pattern matching simulation const seasonEpisode = filename.match(/[Ss](\d+)[Ee](\d+)/); if (seasonEpisode) { result.season = parseInt(seasonEpisode[1], 10); result.episode = parseInt(seasonEpisode[2], 10); } const year = filename.match(/\b(19\d{2}|20\d{2})\b/); if (year) { result.year = parseInt(year[1], 10); } const resolution = filename.match(/\b(720p|1080p|2160p|4K)\b/i); if (resolution) { result.screen_size = resolution[1].toLowerCase() === '4k' ? '2160p' : resolution[1]; } const codec = filename.match(/\b(x264|x265|h264|h265|xvid)\b/i); if (codec) { const codecName = codec[1].toLowerCase(); if (codecName.includes('264')) result.video_codec = 'H.264'; else if (codecName.includes('265')) result.video_codec = 'H.265'; else if (codecName === 'xvid') result.video_codec = 'XviD'; } const source = filename.match(/\b(BluRay|HDTV|WEB|DVD)\b/i); if (source) { result.source = source[1]; } const container = filename.match(/\.(\w+)$/); if (container) { result.container = container[1].toLowerCase(); } return JSON.stringify(result); }