File size: 1,370 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
38
39
40
41
const { config } = require("../config/env");
const { run } = require("../utils/shell");

async function probe(filePath) {
  const { stdout } = await run(config.video.ffprobePath, [
    "-v", "error",
    "-print_format", "json",
    "-show_format",
    "-show_streams",
    filePath
  ], { timeoutMs: 60_000 });
  const data = JSON.parse(stdout);
  const video = data.streams.find((stream) => stream.codec_type === "video");
  if (!video) throw new Error("No video stream was found in this file.");
  const duration = Number(data.format.duration || video.duration || 0);
  const bitrate = Number(data.format.bit_rate || video.bit_rate || 0);
  const fpsParts = String(video.avg_frame_rate || video.r_frame_rate || "0/1").split("/").map(Number);
  const fps = fpsParts[1] ? fpsParts[0] / fpsParts[1] : fpsParts[0] || 0;
  return {
    raw: data,
    duration,
    bitrate,
    size: Number(data.format.size || 0),
    width: Number(video.width || 0),
    height: Number(video.height || 0),
    fps,
    codec: video.codec_name,
    pixFmt: video.pix_fmt,
    hasAudio: data.streams.some((stream) => stream.codec_type === "audio")
  };
}

async function verifyMedia(filePath) {
  await run(config.video.ffmpegPath, ["-v", "error", "-xerror", "-i", filePath, "-f", "null", "-"], {
    timeoutMs: 10 * 60_000
  });
  return true;
}

module.exports = { probe, verifyMedia };