forked from heygen-com/hyperframes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgpuEncoder.ts
More file actions
58 lines (49 loc) · 1.74 KB
/
Copy pathgpuEncoder.ts
File metadata and controls
58 lines (49 loc) · 1.74 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
* GPU Encoder Detection
*
* Shared GPU encoder detection and naming utilities used by both
* chunkEncoder and streamingEncoder services.
*/
import { spawn } from "child_process";
export type GpuEncoder = "nvenc" | "videotoolbox" | "vaapi" | "qsv" | null;
export async function detectGpuEncoder(): Promise<GpuEncoder> {
return new Promise((resolve) => {
const ffmpeg = spawn("ffmpeg", ["-encoders"], {
stdio: ["pipe", "pipe", "pipe"],
});
let stdout = "";
ffmpeg.stdout.on("data", (data) => {
stdout += data.toString();
});
ffmpeg.on("close", () => {
if (stdout.includes("h264_nvenc")) resolve("nvenc");
else if (stdout.includes("h264_videotoolbox")) resolve("videotoolbox");
else if (stdout.includes("h264_vaapi")) resolve("vaapi");
else if (stdout.includes("h264_qsv")) resolve("qsv");
else resolve(null);
});
ffmpeg.on("error", () => resolve(null));
});
}
let cachedGpuEncoder: GpuEncoder | undefined = undefined;
export async function getCachedGpuEncoder(): Promise<GpuEncoder> {
if (cachedGpuEncoder === undefined) {
cachedGpuEncoder = await detectGpuEncoder();
}
return cachedGpuEncoder;
}
export function getGpuEncoderName(encoder: GpuEncoder, codec: "h264" | "h265"): string {
if (!encoder) return codec === "h264" ? "libx264" : "libx265";
switch (encoder) {
case "nvenc":
return codec === "h264" ? "h264_nvenc" : "hevc_nvenc";
case "videotoolbox":
return codec === "h264" ? "h264_videotoolbox" : "hevc_videotoolbox";
case "vaapi":
return codec === "h264" ? "h264_vaapi" : "hevc_vaapi";
case "qsv":
return codec === "h264" ? "h264_qsv" : "hevc_qsv";
default:
return codec === "h264" ? "libx264" : "libx265";
}
}