-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
m3u-playlist.ts
199 lines (178 loc) · 4.08 KB
/
m3u-playlist.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import {M3uGenerator} from "./m3u-generator";
export const M3U_COMMENT = '#'
export const DEFAULT_MEDIA_DURATION = -1;
export enum M3uDirectives {
EXTM3U = '#EXTM3U',
EXTINF = '#EXTINF',
PLAYLIST = '#PLAYLIST',
EXTGRP = '#EXTGRP',
EXTBYT = '#EXTBYT',
EXTIMG = '#EXTIMG',
EXTALB = '#EXTALB',
EXTART = '#EXTART',
EXTGENRE = '#EXTGENRE',
EXTATTRFROMURL = '#EXTATTRFROMURL',
EXTHTTP = '#EXTHTTP',
KODIPROP = '#KODIPROP'
}
/**
* M3u playlist object
*/
export class M3uPlaylist {
/**
* Title of playlist
* @example code
* ```ts
* const playlist = new M3uPlaylist();
* playlist.title = 'Test playlist';
* ```
* @example example output in final m3u string
* ```
* #PLAYLIST:Test TV
* ```
*/
title = '';
/**
* Get url-tvg url
* @returns url-tvg url
* @deprecated The method should not be used, use playlist.attributes['url-tvg'] instead
*/
get urlTvg(): string | undefined {
return this.attributes['url-tvg'];
}
/**
* Set url-tvg url
* @param urlTvg - url-tvg url
* @deprecated The method should not be used, use playlist.attributes['url-tvg'] instead
*/
set urlTvg(urlTvg: string | undefined) {
this.attributes = { ...this.attributes, 'url-tvg': urlTvg }
}
/**
* Attributes of of the EXTM3U tag. Default value is empty attributes object.
*/
attributes: M3uAttributes = new M3uAttributes();
/**
* M3u media objects
* @example
* ```ts
* const playlist = new M3uPlaylist();
* const media1 = new M3uMedia('http://my-stream-ulr.com/playlist.m3u8');
* playlist.medias.push(media1);
* ```
*/
medias: M3uMedia[] = [];
/**
* Get m3u string method to get m3u playlist string of current playlist object
* @returns m3u playlist string
*/
getM3uString(): string {
return M3uGenerator.generate(this);
}
}
/**
* M3u media object
* @example code example
* ```ts
* const media1 = new M3uMedia('http://my-stream-ulr.com/playlist.m3u8');
* ```
* @example example output in final m3u string
* ```
* #EXTINF:-1 tvg-id="Test tv 1" tvg-country="CZ" tvg-language="CS" tvg-logo="logo1.png" group-title="Test1" unknown="0",Test tv 1 [CZ]
* #EXTGRP:Test TV group 1
* http://iptv.test1.com/playlist.m3u8
* ```
*/
export class M3uMedia {
/**
* Name of media
*/
name?: string;
/**
* Group of media
*/
group?: string;
/**
* Duration of media. Default value is -1 (infinity).
*/
duration: number = DEFAULT_MEDIA_DURATION;
/**
* Attributes of media. Default value is empty attributes object.
*/
attributes: M3uAttributes = new M3uAttributes();
/**
* Extra attributes from url
*/
extraAttributesFromUrl?: string = undefined;
/**
* Extra HTTP headers
*/
extraHttpHeaders?: unknown = undefined;
/**
* Kodi props
*/
kodiProps?: Map<string, string> = new Map<string, string>();
/**
* Size of media in bytes.
*/
bytes?: number = undefined;
/**
* image (e.g. cover) URL
*/
image?: string = undefined;
/**
* album
*/
album?: string = undefined;
/**
* artist
*/
artist?: string = undefined;
/**
* genre
*/
genre?: string = undefined;
/**
* Constructor
* @param location - location of stream
*/
constructor(public location: string) {}
}
/**
* M3u media attributes. Can contains know attributes, or unknown custom user defined.
* @example
* ```ts
* const media1 = new M3uMedia('http://my-stream-ulr.com/playlist.m3u8');
* media1.attributes = {'tvg-id': '5', 'tvg-language': 'EN', 'unknown': 'my custom attribute'};
* ```
*/
export class M3uAttributes {
/**
* url-tvg attribute, widely used for EPG
*/
'url-tvg'?: string;
/**
* tvg-id attribute, widely used
*/
'tvg-id'?: string;
/**
* tvg-language attribute, widely used
*/
'tvg-language'?: string;
/**
* tvg-country attribute, widely used
*/
'tvg-country'?: string;
/**
* tvg-logo attribute, widely used
*/
'tvg-logo'?: string;
/**
* group-title attribute, widely used
*/
'group-title'?: string;
/**
* unknown user defined attribute
*/
[key: string]: string | undefined;
}