Spaces:
Configuration error
Configuration error
Download src/ffmpeg/detector.js from UUUZCX/video-optimizer-bot: direct link, hf CLI and curl.
- Browser
- Download file 7 kB
-
https://huggingface.co/spaces/UUUZCX/video-optimizer-bot/resolve/main/src/ffmpeg/detector.js
- Command line
-
hf download hf://spaces/UUUZCX/video-optimizer-bot/src/ffmpeg/detector.js
-
curl -L -o detector.js https://huggingface.co/spaces/UUUZCX/video-optimizer-bot/resolve/main/src/ffmpeg/detector.js
7 kB
| const path = require('path'); | |
| const { createLogger } = require('../monitoring/logger.js'); | |
| const { probe } = require('./ffprobe.js'); | |
| const logger = createLogger('VideoDetector'); | |
| /** | |
| * Automatically detects the video type (anime, camera, screen, ocr) | |
| * using metadata heuristics and filename analysis. | |
| * | |
| * @param {string} filePath - Absolute path to the video file | |
| * @param {Object} [preAnalyzedMeta] - Optional pre-existing analysis metadata to avoid duplicate ffprobe runs | |
| * @returns {Promise<{ type: string, confidence: number, reasoning: string }>} | |
| */ | |
| async function detectVideoType(filePath, preAnalyzedMeta = null) { | |
| logger.info(`Detecting video type for: ${path.basename(filePath)}`); | |
| try { | |
| const meta = preAnalyzedMeta || await probe(filePath); | |
| const fileName = path.basename(filePath).toLowerCase(); | |
| const scores = { | |
| anime: 0, | |
| camera: 0, | |
| screen: 0, | |
| ocr: 0 | |
| }; | |
| const details = { | |
| anime: [], | |
| camera: [], | |
| screen: [], | |
| ocr: [] | |
| }; | |
| const animeKeywords = [ | |
| 'anime', 'ep', 'episode', 'season', 's0', 's1', 's2', 's3', 's4', 's5', 's6', 's7', 's8', 's9', | |
| 'batch', 'ova', 'movie', 'hancodez', 'judas', 'doki', 'horriblesubs', 'subs', 'subbed', 'dubbed', | |
| 'encodes', 'dual-audio', 'manga', 'eriraw', 'raws', 'hevc-10bit' | |
| ]; | |
| const screenKeywords = [ | |
| 'screen', 'screencast', 'recording', 'tutorial', 'desktop', 'zoom', 'meet', 'teams', 'webinar', | |
| 'obs', 'bandicam', 'camtasia', 'course', 'lecture', 'classroom', 'windows', 'macbook', 'linux', | |
| 'terminal', 'github', 'coding', 'programming' | |
| ]; | |
| const ocrKeywords = [ | |
| 'ocr', 'slide', 'presentation', 'powerpoint', 'pdf', 'book', 'text', 'document', 'page', 'scan', | |
| 'read', 'readable', 'invoice', 'receipt', 'legible', 'sheet', 'board', 'whiteboard' | |
| ]; | |
| const cameraKeywords = [ | |
| 'vlog', 'camera', 'phone', 'iphone', 'android', 'gopro', 'dji', 'trip', 'travel', 'movie', | |
| 'film', 'nature', 'party', 'wedding', 'family', 'street', 'walk', 'run', 'live', 'concert', | |
| 'vid_', 'dscn', 'img_', 'mov_' | |
| ]; | |
| animeKeywords.forEach(kw => { | |
| if (fileName.includes(kw)) { | |
| scores.anime += 15; | |
| details.anime.push(`Filename contains keyword: ${kw}`); | |
| } | |
| }); | |
| screenKeywords.forEach(kw => { | |
| if (fileName.includes(kw)) { | |
| scores.screen += 15; | |
| details.screen.push(`Filename contains keyword: ${kw}`); | |
| } | |
| }); | |
| ocrKeywords.forEach(kw => { | |
| if (fileName.includes(kw)) { | |
| scores.ocr += 15; | |
| details.ocr.push(`Filename contains keyword: ${kw}`); | |
| } | |
| }); | |
| cameraKeywords.forEach(kw => { | |
| if (fileName.includes(kw)) { | |
| scores.camera += 15; | |
| details.camera.push(`Filename contains keyword: ${kw}`); | |
| } | |
| }); | |
| if (/\[[a-z0-9\s_-]+\]/i.test(fileName)) { | |
| scores.anime += 20; | |
| details.anime.push('Filename pattern matches fan-sub/release-group brackets "[Group]"'); | |
| } | |
| if (meta.hasVideo && meta.video) { | |
| const v = meta.video; | |
| const totalBitrateKbps = (meta.format?.bitrate || 0) / 1000; | |
| if (v.fps > 0 && v.fps <= 12) { | |
| scores.ocr += 30; | |
| scores.screen += 20; | |
| details.ocr.push(`Extremely low frame rate (${v.fps} FPS) typical of scanned pages/OCR`); | |
| details.screen.push(`Low frame rate (${v.fps} FPS) typical of screen logs`); | |
| } else if (v.fps > 12 && v.fps <= 20) { | |
| scores.screen += 15; | |
| details.screen.push(`Moderately low frame rate (${v.fps} FPS) typical of screens`); | |
| } | |
| if (v.fps === 30 || v.fps === 60 || v.fps === 29.97 || v.fps === 59.94) { | |
| scores.camera += 10; | |
| scores.anime += 5; | |
| details.camera.push(`Standard broadcast frame rate (${v.fps} FPS)`); | |
| } | |
| if (v.fps === 23.976 || v.fps === 24) { | |
| scores.anime += 25; | |
| scores.camera += 10; | |
| details.anime.push(`Cinematic animation frame rate (${v.fps} FPS)`); | |
| } | |
| if (totalBitrateKbps > 0 && v.width && v.height) { | |
| const pixelCount = v.width * v.height; | |
| const fpsCoeff = v.fps || 24; | |
| const bitsPerPixelFrame = (meta.format.bitrate || 0) / (pixelCount * fpsCoeff); | |
| logger.debug(`Bits per pixel-frame: ${bitsPerPixelFrame.toFixed(4)}`); | |
| if (bitsPerPixelFrame < 0.03) { | |
| scores.screen += 30; | |
| scores.ocr += 15; | |
| details.screen.push(`Ultra-low bitrate density (${bitsPerPixelFrame.toFixed(4)} bppf) indicates heavy static frames`); | |
| } else if (bitsPerPixelFrame >= 0.03 && bitsPerPixelFrame < 0.08) { | |
| scores.anime += 15; | |
| scores.screen += 10; | |
| details.anime.push(`Moderate bitrate density (${bitsPerPixelFrame.toFixed(4)} bppf) typical of vector art/anime`); | |
| } else if (bitsPerPixelFrame > 0.15) { | |
| scores.camera += 25; | |
| details.camera.push(`High bitrate density (${bitsPerPixelFrame.toFixed(4)} bppf) indicates high noise/motion complexity`); | |
| } | |
| } | |
| if (meta.metadata && meta.metadata.encoder) { | |
| const encoder = meta.metadata.encoder.toLowerCase(); | |
| if (encoder.includes('obs') || encoder.includes('captura') || encoder.includes('sharex') || encoder.includes('camtasia')) { | |
| scores.screen += 40; | |
| details.screen.push(`Created by screen recorder software: ${encoder}`); | |
| } | |
| } | |
| if (v.width && v.height) { | |
| const ratio = v.width / v.height; | |
| if (ratio < 0.8 || ratio > 2.5) { | |
| scores.ocr += 20; | |
| details.ocr.push(`Non-standard portrait/ultra-wide aspect ratio (${v.aspectRatio || (v.width + 'x' + v.height)}) typical of document scans`); | |
| } | |
| } | |
| } | |
| let detectedType = 'camera'; | |
| let maxScore = 0; | |
| for (const [type, score] of Object.entries(scores)) { | |
| if (score > maxScore) { | |
| maxScore = score; | |
| detectedType = type; | |
| } | |
| } | |
| if (maxScore < 10) { | |
| detectedType = 'camera'; | |
| maxScore = 10; | |
| details.camera.push('Low confidence heuristic triggers default fallback.'); | |
| } | |
| const totalScore = Math.max(1, scores.anime + scores.camera + scores.screen + scores.ocr); | |
| const confidence = Math.min(100, Math.round((maxScore / totalScore) * 100)); | |
| const finalReasoning = details[detectedType].join('; ') || 'Default fallback to camera settings.'; | |
| logger.info(`Detection Result: type=${detectedType.toUpperCase()} confidence=${confidence}%`, { | |
| scores, | |
| reasoning: finalReasoning | |
| }); | |
| return { | |
| type: detectedType, | |
| confidence, | |
| reasoning: finalReasoning | |
| }; | |
| } catch (error) { | |
| logger.error(`Error during content type detection, defaulting to camera: ${error.message}`, { error }); | |
| return { | |
| type: 'camera', | |
| confidence: 100, | |
| reasoning: `Detection error encountered: ${error.message}. Defaulted to generic camera.` | |
| }; | |
| } | |
| } | |
| module.exports = { detectVideoType }; | |