forked from dgreif/ring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream-example.ts
60 lines (52 loc) · 1.47 KB
/
stream-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
import 'dotenv/config'
import { RingApi } from '../ring-client-api'
import * as path from 'path'
import { cleanOutputDirectory, outputDirectory } from './util'
/**
* This example streams to files, each with 10 seconds of video.
* The output will be in output/part${part #}.mp4
**/
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
}
await cleanOutputDirectory()
console.log('Starting Video...')
const call = await camera.streamVideo({
// save video 10 second parts so the mp4s are playable and not corrupted:
// https://superuser.com/questions/999400/how-to-use-ffmpeg-to-extract-live-stream-into-a-sequence-of-mp4
output: [
'-flags',
'+global_header',
'-f',
'segment',
'-segment_time',
'10', // 10 seconds
'-segment_format_options',
'movflags=+faststart',
'-reset_timestamps',
'1',
path.join(outputDirectory, 'part%d.mp4'),
],
})
console.log('Video started, streaming to part files...')
call.onCallEnded.subscribe(() => {
console.log('Call has ended')
process.exit()
})
setTimeout(function () {
console.log('Stopping call...')
call.stop()
}, 60 * 1000) // Stop after 1 minute
}
example().catch((e) => {
console.error(e)
process.exit(1)
})