-
-
Notifications
You must be signed in to change notification settings - Fork 170
/
Copy pathbrowser-example.ts
78 lines (68 loc) · 1.75 KB
/
browser-example.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
import 'dotenv/config'
import { RingApi } from 'ring-client-api'
import fs from 'node:fs/promises'
import path from 'node:path'
// @ts-expect-error express is not installed by default so you will need to run `npm i express`
import express from 'express'
/**
* This example creates an hls stream which is viewable in a browser
* It also starts web app to view the stream at http://localhost:3000
**/
async function example() {
const ringApi = new RingApi({
// Replace with your refresh token
refreshToken: process.env.RING_REFRESH_TOKEN!,
debug: true,
}),
[camera] = await ringApi.getCameras()
if (!camera) {
console.log('No cameras found')
return
}
const app = express(),
__dirname = new URL('.', import.meta.url).pathname,
publicOutputDirectory = path.join(__dirname, 'public/output')
app.use('/', express.static(path.join(__dirname, 'public')))
app.listen(3000, () => {
console.log(
'Listening on port 3000. Go to http://localhost:3000 in your browser',
)
})
if (!(await fs.stat(publicOutputDirectory))) {
await fs.mkdir(publicOutputDirectory)
}
const call = await camera.streamVideo({
output: [
'-preset',
'veryfast',
'-g',
'25',
'-sc_threshold',
'0',
'-f',
'hls',
'-hls_time',
'2',
'-hls_list_size',
'6',
'-hls_flags',
'delete_segments',
path.join(publicOutputDirectory, 'stream.m3u8'),
],
})
call.onCallEnded.subscribe(() => {
console.log('Call has ended')
process.exit()
})
setTimeout(
function () {
console.log('Stopping call...')
call.stop()
},
5 * 60 * 1000,
) // Stop after 5 minutes.
}
example().catch((e) => {
console.error(e)
process.exit(1)
})