-
Notifications
You must be signed in to change notification settings - Fork 0
/
video.js
44 lines (38 loc) · 1.19 KB
/
video.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
var yt = require('youtube.get-video-info')
exports.handler = function(event, context, callback) {
const videoID = event.queryStringParameters.v
if(!videoID) {
callback('Video ID is required', {statusCode: 500})
return
}
yt.retrieve(videoID, function(err, res) {
if (err) {
callback(err, {statusCode: 500})
return
} else if (res.status === 'fail') {
callback(res.reason, {statusCode: 500})
return
} else {
const playerResponse = JSON.parse(res.player_response)
const captions = playerResponse.captions.playerCaptionsTracklistRenderer.captionTracks.filter(c => c.kind !== 'asr')
const streams = playerResponse.streamingData.formats
const details = playerResponse.videoDetails
callback(null, {
statusCode: 200,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept',
},
body: JSON.stringify({
captions,
streams,
details: {
title: details.title,
author: details.author,
videoId: details.videoId,
}
})
})
}
})
}