forked from widdix/aws-lambda-youtube-dl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
39 lines (38 loc) · 1.06 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
const stream = require('stream');
const youtubedl = require('youtube-dl');
const AWS = require('aws-sdk');
exports.handler = function(event, context, cb) {
if (!event.videoUrl) {
return cb(new Error('videoUrl missing in event'));
}
const s3 = new AWS.S3({apiVersion: '2006-03-01'});
const passtrough = new stream.PassThrough();
const dl = youtubedl(event.videoUrl, ['--format=best[ext=mp4]'], {maxBuffer: Infinity});
dl.once('error', (err) => {
cb(err);
});
const key = `${context.awsRequestId}.mp4`;
const upload = new AWS.S3.ManagedUpload({
params: {
Bucket: process.env.BUCKET_NAME,
Key: key,
Body: passtrough
},
partSize: 1024 * 1024 * 64 // in bytes
});
upload.on('httpUploadProgress', (progress) => {
console.log(`[${event.videoUrl}] copying video ...`, progress);
});
upload.send((err) => {
if (err) {
cb(err);
} else {
cb(null, {
bucketName: process.env.BUCKET_NAME,
key,
url: `s3://${process.env.BUCKET_NAME}/${key}`
});
}
});
dl.pipe(passtrough);
}