This repository was archived by the owner on Feb 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
101 lines (85 loc) · 3.09 KB
/
index.ts
File metadata and controls
101 lines (85 loc) · 3.09 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import path from "path";
import { HelixUser, HelixStream } from "twitch";
import { getStreamOnlyClientId, getTwitchAccessToken, getTwitchLivePlaylistUrl, generateDateName } from "../../twitch/stream";
import { StreamSessionInterface } from "./interface";
import { loadConfig4FFmpeg, loadFFmpegConfig } from "../ffmpeg/config";
import { allocateGPU } from "./gpu";
import { setupStreamerPath } from "../../setup";
import fs from "fs";
const streamSessions: StreamSessionInterface[] = [];
let currentStreamSessionId = 0;
/**
* 현재 스트림 세션들을 봅니다.
*/
export function getStreamSessions() {
return streamSessions;
}
/**
* 스트림세션을 추가합니다. 자동으로 `getStreamSessions` 에서 접근 할 수 있도록 등록됩니다.
*
* @param videosDir 비디오를 저장할 디렉토리를 지정합니다 `./videos/`
* @param streamer 스트리머를 `HelixUser` 형으로 전달 받습니다.
*/
export async function addStreamSession(videosDir: string, streamer: HelixUser): Promise<StreamSessionInterface | null> {
const username = streamer.name;
const playlistUrl = await getTwitchLivePlaylistUrl(username, undefined);
if (!playlistUrl) return null;
const ffmpegConfig = loadFFmpegConfig();
const streamerDir = setupStreamerPath(streamer, videosDir);
const outputDir = path.join(streamerDir, generateDateName());
const outputPath = path.join(outputDir, ffmpegConfig.outputFile);
if (!fs.existsSync(outputDir)) fs.mkdirSync(outputDir);
const isGPULoadBalancingEnabled = ffmpegConfig.gpus && ffmpegConfig.gpus.length > 0;
console.log("[엔당가속] GPU 로드 밸런싱", (isGPULoadBalancingEnabled) ? "활성화":"비활성화");
const allocatedGPU = (isGPULoadBalancingEnabled) ? allocateGPU(currentStreamSessionId) : undefined
const ffmpegConversion
= loadConfig4FFmpeg(playlistUrl, outputPath, ffmpegConfig.conversion, allocatedGPU);
const streamSession: StreamSessionInterface = {
id: currentStreamSessionId++,
gpu: (allocatedGPU) ? {
id: allocatedGPU
} : undefined,
streamer,
stream: await streamer.getStream() as HelixStream,
conversion: ffmpegConversion,
output: {
outputDir,
outputPath
},
status: {
timestamp: "00:00:00.00",
epoch: 0,
lastUpdate: new Date(),
},
plugins: {}
};
streamSessions.push(streamSession);
return streamSession;
}
/**
* 스트림세션을 제거합니다.
* @param streamSession 스트림세션
*/
export function removeStreamSession(streamSession: StreamSessionInterface): void {
return removeStreamSessionById(streamSession.id);
}
/**
* 스트림세션을 아이디로 제거합니다.
* @param id 스트림세션의 아이디
*/
export function removeStreamSessionById(id: number): void {
for (let i = 0; i < streamSessions.length; i++) {
if (id === streamSessions[i].id) {
streamSessions.splice(i, 1);
return;
}
}
return;
}
/**
* 모든 스트림세션의 종료를 기다립니다.
*/
export async function waitForStreamSessionsShutdown(): Promise<void> {
while (getStreamSessions().length !== 0) {}
return;
}