Skip to content

Commit f37469d

Browse files
Merge pull request #12 from Vonage-Community/DEVX-8860---adds-captions
Devx 8860 adds captions
2 parents 2360946 + 14a5b62 commit f37469d

File tree

3 files changed

+83
-3
lines changed

3 files changed

+83
-3
lines changed

README.md

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,10 +235,51 @@ Examples:
235235
GET /archive // fetch up to 1000 archive objects
236236
GET /archive?sessionId=1_MX42...xQVJmfn4 // fetch archive(s) with session ID
237237
GET /archive?count=10 // fetch the first 10 archive objects
238-
GET /archive?offset=10 // fetch archives but first 10 archive objetcs
238+
GET /archive?offset=10 // fetch archives but first 10 archive objects
239239
GET /archive?count=10&offset=10 // fetch 10 archive objects starting from 11th
240240
```
241241
242+
### Enable [Live Captions](https://developer.vonage.com/en/video/guides/live-caption)
243+
244+
A `POST` request to the `/captions/start` route starts transcribing audio streams and generating real-time captions for a Vonage Video session.
245+
The session ID and token are passed in as JSON data in the body of the request
246+
247+
```javascript
248+
router.post('/captions/start', async (req, res) => {
249+
const sessionId = req.body.sessionId;
250+
const captionsOptions = {
251+
languageCode: 'en-US',
252+
partialCaptions: 'true',
253+
};
254+
try {
255+
const captionsResponse = await vonage.video.enableCaptions(sessionId, req.body.token, captionsOptions);
256+
const captionsId = captionsResponse.captionsId;
257+
res.send({ id: captionsId });
258+
} catch (error) {
259+
console.error("Error starting captions: ",error);
260+
res.status(500).send(`Error starting captions: ${error}`);
261+
}
262+
});
263+
```
264+
265+
### Disable Live Captions
266+
267+
A `POST` request to the `/captions/:captionsId/stop` route stops the live captioning.
268+
The captions ID is returned by the call to the `/captions/start` endpoint.
269+
270+
```javascript
271+
router.post('/captions/:captionsId/stop', async (req, res) => {
272+
const captionsId = req.params.captionsId;
273+
try {
274+
await vonage.video.disableCaptions(captionsId);
275+
res.sendStatus(202)
276+
} catch (error) {
277+
console.error("Error stopping captions: ",error);
278+
res.status(500).send(`Error stopping captions: ${error}`);
279+
}
280+
});
281+
```
282+
242283
## More information
243284
244285
This sample app does not provide client-side functionality

routes/index.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,39 @@ router.all('/admin/clear-conversations', async function (req, res) {
432432
});
433433
});
434434

435+
/**
436+
* POST /captions/start
437+
*/
438+
router.post('/captions/start', async (req, res) => {
439+
const sessionId = req.body.sessionId;
440+
const captionsOptions = {
441+
languageCode: 'en-US',
442+
partialCaptions: 'true',
443+
};
444+
try {
445+
const captionsResponse = await vonage.video.enableCaptions(sessionId, req.body.token, captionsOptions);
446+
const captionsId = captionsResponse.captionsId;
447+
res.send({ id: captionsId });
448+
} catch (error) {
449+
console.error("Error starting captions: ",error);
450+
res.status(500).send(`Error starting captions: ${error}`);
451+
}
452+
});
453+
454+
/**
455+
* POST /captions/:captionsId/stop
456+
*/
457+
router.post('/captions/:captionsId/stop', async (req, res) => {
458+
const captionsId = req.params.captionsId;
459+
try {
460+
await vonage.video.disableCaptions(captionsId);
461+
res.sendStatus(202)
462+
} catch (error) {
463+
console.error("Error stopping captions: ",error);
464+
res.status(500).send(`Error stopping captions: ${error}`);
465+
}
466+
});
467+
435468
router.get('/_/health', async function (req, res) {
436469
res.status(200).send({status: 'OK'});
437470
})

views/index.pug

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ html(lang='en')
1717
body
1818
p
1919
| This is a sample web service for use with Vonage Video Node SDK. See the Vonage
20-
a(href='https://github.com/opentok/learning-opentok-node')
21-
| learning-opentok-node
20+
a(href='https://github.com/Vonage-Community/sample-video-node-learning_server')
21+
| sample-video-node-learning_server
2222
| repo on GitHub.
2323
p
2424
| Resources are defined at the following endpoints:
@@ -29,6 +29,12 @@ html(lang='en')
2929
tr
3030
td GET /room/:name
3131
td Return an Application ID, session ID, and token associated with a room name.
32+
tr
33+
td POST /captions/start
34+
td Starts captions.
35+
tr
36+
td POST /captions/:captionsId/stop
37+
td Stops captions.
3238
tr
3339
td POST /archive/start
3440
td Start an archive for the specified Vonage Video session.

0 commit comments

Comments
 (0)