File size: 1,512 Bytes
03b405b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const { config } = require("../config/env");
const { getPreset } = require("./presets");
const { chooseEncoder, isHardwareEncoder } = require("./gpu.detector");

function detectMode(info, requestedMode) {
  if (requestedMode && requestedMode !== "auto") return requestedMode;
  const pixels = info.width * info.height;
  const bitratePerPixel = pixels && info.fps ? info.bitrate / pixels / info.fps : 0;
  if (info.width >= 1400 && info.fps <= 35 && bitratePerPixel < 0.08) return "screen";
  if (info.duration > 20 * 60) return "ultra";
  if (info.width >= 1600 || info.height >= 1000) return "ocr";
  if (info.fps > 50) return "camera";
  return config.video.defaultMode;
}

async function buildPlan(info, requestedMode = "auto") {
  const mode = detectMode(info, requestedMode);
  const preset = getPreset(mode);
  const encoder = await chooseEncoder("h264");
  const hw = isHardwareEncoder(encoder);
  const targetWidth = Math.min(info.width, preset.maxWidth);
  const evenWidth = targetWidth % 2 === 0 ? targetWidth : targetWidth - 1;
  const targetFps = Math.min(Math.round(info.fps || preset.fps), preset.fps);
  return {
    mode,
    preset,
    encoder,
    hardware: hw,
    width: evenWidth > 0 ? evenWidth : -2,
    fps: Math.max(15, targetFps || 30),
    keyint: Math.max(48, Math.round((targetFps || 30) * 2)),
    splitSeconds: Math.max(config.video.chunkMinSeconds, Math.min(config.video.chunkMaxSeconds, info.duration > 15 * 60 ? 120 : 180))
  };
}

module.exports = { detectMode, buildPlan };