-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
343 lines (313 loc) · 11.7 KB
/
index.js
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
var fs = require('fs');
var readline = require('readline');
var google = require('googleapis');
var googleAuth = require('google-auth-library');
var ffmpeg = require('./ffmpeg');
var each = require('sync-each');
// Initalize console
require('console-stamp')(console, {
pattern: 'mm/dd/yyyy | HH:MM:ss.l',
colors: {
stamp: 'yellow',
label: 'white'
}
});
// Initalize configuration
process.env.NODE_CONFIG_DIR = __dirname;
var config = require('config');
// Authorize Google API
var service = google.youtube('v3');
var scopes = ['https://www.googleapis.com/auth/youtube'];
var tokenDir = __dirname + '/credentials/';
var tokenPath = tokenDir + 'token.json';
var auth = null;
var streams = [];
var broadcasts = [];
fs.readFile('client_secret.json', function(error, content) {
if (error) {
console.error('Error while loading client secret file: ' + error);
return;
} else authorize(JSON.parse(content), function(token) {
auth = token;
start();
});
})
function authorize(credentials, callback) {
var clientSecret = credentials.installed.client_secret;
var clientID = credentials.installed.client_id;
var redirectURL = credentials.installed.redirect_uris[0];
var auth = new googleAuth();
var authClient = new auth.OAuth2(clientID, clientSecret, redirectURL);
fs.readFile(tokenPath, function(error, token) {
if (error) {
getNewToken(authClient, callback);
} else {
authClient.credentials = JSON.parse(token);
callback(authClient);
}
});
}
function getNewToken(authClient, callback) {
var authUrl = authClient.generateAuthUrl({
access_type: 'offline',
scope: scopes
});
console.log('----------------========[GOOGLE]========----------------');
console.log('Please authorize this app by visiting the following');
console.log('url and pasting the code in the prompt below.');
console.log('- ' + authUrl);
console.log('----------------========================----------------');
var r1 = readline.createInterface({
input: process.stdin,
output: process.stdout
});
r1.question('Access code: ', function(code) {
r1.close();
authClient.getToken(code, function(error, token) {
if (error) {
console.error('Error while trying to retrieve access token.');
console.error(error.stack);
return;
} else {
console.log('Client has been successfully authorized.');
authClient.credentials = token;
storeToken(token);
callback(authClient);
}
})
})
}
function storeToken(token) {
try {
fs.mkdir(tokenDir);
} catch (error) {
if (err.code != 'EEXIST') throw error;
}
fs.writeFile(tokenPath, JSON.stringify(token));
console.log('Stored client token to ' + tokenPath + '.');
}
// Remove possible duplicates from previous run
function cleanup(streamCount, streamTitle, callback) {
deleteBroadcasts(function(count, total) {
console.log('[' + streamCount + '] Deleted ' + count + '/' + total + ' duplicate broadcasts.');
deleteStreams(function(count, total) {
console.log('[' + streamCount + '] Deleted ' + count + '/' + total + ' duplicate streams.');
callback();
})
})
function deleteBroadcasts(callback) {
service.liveBroadcasts.list({
auth: auth,
part: 'id,snippet',
mine: true,
maxResults: 50
}, function (error, response) {
if (error) {
console.error('[' + streamCount + '] Error while retrieving existing broadcasts.');
console.error('[' + streamCount + '] ' + error.stack);
} else {
var broadcasts = response.items;
var deleted = 0;
each(broadcasts, function(broadcast, next) {
var title = broadcast.snippet.title;
var id = broadcast.id;
if (title === streamTitle) {
service.liveBroadcasts.delete({
auth: auth,
id: id
}, function (error, response) {
if (error) {
console.error('[' + streamCount + '] Error while deleting broadcast: ' + id + '.');
console.error('[' + streamCount + '] ' + error.stack);
} else {
deleted++;
}
next();
})
} else next();
}, function() {
callback(deleted, broadcasts.length)
});
}
})
}
function deleteStreams(callback) {
service.liveStreams.list({
auth: auth,
part: 'id,snippet',
mine: true,
maxResults: 50
}, function (error, response) {
if (error) {
console.error('Error while retrieving existing streams.');
console.error('[' + streamCount + '] ' + error.stack);
} else {
var streams = response.items;
var deleted = 0;
each(streams, function(stream, next) {
var title = stream.snippet.title;
var id = stream.id;
if (title === streamTitle) {
service.liveStreams.delete({
auth: auth,
id: id
}, function (error, response) {
if (error) {
console.error('Error while deleting stream: ' + id + '.');
console.error('[' + streamCount + '] ' + error.stack);
} else {
deleted++;
}
next();
})
} else next();
}, function() {
callback(deleted, streams.length);
});
}
})
}
}
function start() {
var stream = null;
var broadcast = null;
var streamID = null;
var broadcastID = null;
var ffmpegService = null;
var streams = config.get('streams');
console.log('Loaded ' + streams.length + ' stream configurations.');
var streamCount = 1;
streams.forEach(function(stream) {
var title = stream.title;
var desc = stream.description;
var format = stream.format;
var rtsp = stream.rtsp;
cleanup(streamCount, title, function() {
createStream(streamCount, title, desc, format, rtsp);
streamCount++
});
});
}
function createStream(streamCount, title, desc, format, rtsp) {
stream = service.liveStreams.insert({
auth: auth,
part: 'snippet,cdn',
resource: {
snippet: {
title: title
},
cdn: {
format: format,
ingestionType: 'rtmp'
}
}
}, function (error, response) {
if (error) {
console.error('[' + streamCount + '] Error while initalizing livestream service.');
console.error('[' + streamCount + '] ' + error.stack);
return;
} else {
var streamID = response.id;
streams.push(streamID);
var ingestionInfo = response.cdn.ingestionInfo;
var ingestionAddress = ingestionInfo.ingestionAddress + '/' + ingestionInfo.streamName;
console.log('[' + streamCount + '] Created livestream service: ' + streamID + '.');
ffmpeg.start(streamCount, rtsp, ingestionAddress, function(service) {
createBroadcast(streamCount, title, desc, format, streamID);
});
}
})
};
function createBroadcast(streamCount, title, desc, format, streamID) {
service.liveBroadcasts.insert({
auth: auth,
part: 'snippet,status,contentDetails',
resource: {
snippet: {
title: title,
description: desc,
scheduledStartTime: new Date()
},
status: {
privacyStatus: 'public'
},
contentDetails: {
monitorStream: {
enableMonitorStream: false
},
enableDvr: false
}
}
}, function (error, response) {
if (error) {
console.error('[' + streamCount + '] Error while initalizing broadcasting service.');
if (error.errors[0].reason === "insufficientLivePermissions") {
console.error('[' + streamCount + '] This is often the YouTube anti-spam system. Try manually creating a broadcast and read the error that appears, if any.');
}
console.error('[' + streamCount + '] ' + error.stack);
return;
} else {
var broadcastID = response.id;
broadcasts.push(broadcastID);
console.log('[' + streamCount + '] Created broadcast service: ' + broadcastID + '.');
service.liveBroadcasts.bind({
auth: auth,
id: broadcastID,
part: 'id',
streamId: streamID
}, function (error, response) {
if (error) {
console.error('[' + streamCount + '] Error while binding livestream service to broadcast service.');
console.error('[' + streamCount + '] ' + error.stack);
return;
} else {
console.log('[' + streamCount + '] Bound livestream service to broadcast service.');
checkStream(streamCount, streamID, broadcastID);
}
})
}
})
}
function checkStream(streamCount, streamID, broadcastID) {
process.stdout.write('[' + streamCount + '] Waiting for stream to become active.');
var interval = setInterval(function() {
service.liveStreams.list({
auth: auth,
id: streamID,
part: 'id,status'
}, function (error, response) {
if (error) {
process.stdout.write('\n');
console.error('[' + streamCount + '] Error while requesting livestream service update.');
console.error('[' + streamCount + '] ' + error.stack);
return;
} else {
if (response.items[0].status.streamStatus === 'active') {
process.stdout.write('\n');
console.log('[' + streamCount + '] Stream has been activated - ready to go live.');
goLive(streamCount, broadcastID);
clearInterval(interval);
} else {
process.stdout.write('.');
}
}
})
}, 1000);
}
function goLive(streamCount, broadcastID) {
service.liveBroadcasts.transition({
auth: auth,
broadcastStatus: 'live',
id: broadcastID,
part: 'status,id'
}, function (error, response) {
if (error) {
console.error('[' + streamCount + '] Error while updating broadcast service to "live."');
console.error('[' + streamCount + '] ' + error.stack);
return;
} else {
console.log('[' + streamCount + '] Broadcast is now live.');
console.info('[' + streamCount + '] - http://youtube.com/watch?v=' + broadcastID);
}
})
}