generated from Richienb/typescript-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.ts
183 lines (160 loc) · 3.98 KB
/
index.ts
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
"use strict"
import vlcStatic from "vlc-static"
import uniqueString from "unique-string"
import internalIp from "internal-ip"
import getPort from "get-port"
import execa from "execa"
import got from "got"
import queryString from "query-string"
import joinURL from "url-join"
namespace Result {
export interface Status {
readonly fullscreen: number | boolean
readonly audiodelay: number
readonly apiversion: number
readonly currentplid: number
readonly time: number
readonly volume: number
readonly length: number
readonly random: boolean
readonly audiofilters: AudioFilters
readonly rate: number
readonly videoeffects: VideoEffects
readonly state: VLCPlaylistStatus
readonly loop: boolean
readonly version: string
readonly position: number
readonly date?: string
readonly information?: Information
readonly repeat: boolean
readonly subtitledelay: number
readonly equalizer: Equalizer[]
}
export type VLCPlaylistStatus =
| 'stopped'
| 'playing'
| 'paused'
| 'unknown'
export interface AudioFilters {
filter_0: string
filter_1?: string
filter_2?: string
filter_3?: string
filter_4?: string
}
export interface Equalizer {
presets: Presets
bands: { [key: string]: number }
preamp: number
}
export interface Presets {
"preset id=\"0\"": string
"preset id=\"1\"": string
"preset id=\"2\"": string
"preset id=\"3\"": string
"preset id=\"4\"": string
"preset id=\"5\"": string
"preset id=\"6\"": string
"preset id=\"7\"": string
"preset id=\"8\"": string
"preset id=\"9\"": string
"preset id=\"10\"": string
"preset id=\"11\"": string
"preset id=\"12\"": string
"preset id=\"13\"": string
"preset id=\"14\"": string
"preset id=\"15\"": string
"preset id=\"16\"": string
"preset id=\"17\"": string
}
export interface Information {
chapter: number
chapters: any[]
title: number
category: Category
titles: any[]
}
export interface Category {
"Stream 0": Stream0
"Stream 1": Stream1
meta: Record<string, string | number>
}
export interface Stream0 {
Decoded_format: string
Color_transfer_function: string
Chroma_location: string
Video_resolution: string
Frame_rate: string
Codec: string
Orientation: string
Color_space: string
Type: string
Color_primaries: string
Buffer_dimensions: string
}
export interface Stream1 {
Codec: string
Channels: string
Type: string
Bits_per_sample: string
Language: string
Sample_rate: string
}
export interface VideoEffects {
hue: number
saturation: number
contrast: number
brightness: number
gamma: number
}
export interface Playlist {
ro: string
type: string
name: string
id: string
children: Playlist[]
}
}
export = async function vlc() {
const password = uniqueString()
const ip = await internalIp.v4()
const port = await getPort()
const address = `http://${ip}`
const instance = execa(vlcStatic(), ["--extraintf", "http", "--intf", "wx", "--http-host", ip, "--http-port", port.toString(), "--http-password", password])
return new class VLC {
/** Get the current player status. */
public async info() {
const data = await got<Result.Status>(joinURL(address, "requests", "status.json"), {
port,
password,
}).json()
return data
}
/** Get the current playlist information. */
public async playlist() {
const data = await got<Result.Playlist>(joinURL(address, "requests", "playlist.json"), {
port,
password,
}).json()
return data
}
/**
* Execute a command. See https://wiki.videolan.org/VLC_HTTP_requests#Full_command_list
* @param command The command to execute.
* @param options The data to encode with the command.
*/
public async command(command: string, options?: Record<string, string | number | boolean>) {
await got(joinURL(address, "requests", "status.json", `?${queryString.stringify({
command,
...options,
}).replace(/\+/, "%20")}`), {
port,
password,
})
}
/** Kill the process. */
public kill(): void {
instance.kill()
}
}
}