-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
88 lines (70 loc) · 1.77 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
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
79
80
81
82
83
84
85
86
87
88
'use strict';
require('dotenv').config();
const express = require('express');
const app = express();
const AWS = require('aws-sdk');
const urlencode = require('urlencode');
const s3 = new AWS.S3();
const fs = require('fs');
AWS.config.update({ region: process.env.S3_REGION });
app.get('/', function (req, res) {
console.log("got req for /");
res.send('Snapa - thumbnail API');
});
app.get('/snap', function (req, res) {
console.log("got request for /snap");
const v = req.query.v;
const signedUrlExpireSeconds = 60 * 20;
var signedUrl = s3.getSignedUrl('getObject', {
Bucket: process.env.VIDEO_S3_BUCKET_NAME,
Key: 'test/' + v,
Expires: signedUrlExpireSeconds
});
signedUrl = urlencode.decode(signedUrl);
const fname = 'tmpthumb/' + v + '.jpg';
const
spawn = require('child_process').spawn,
f = spawn('ffmpeg', [
'-i',
signedUrl,
'-vf',
'thumbnail,scale=500:300',
'-frames:v',
1,
fname,
'-y'
]);
f.stdout.on('data', data => {
console.log(`stdout: ${data}`);
});
f.stderr.on('data', data => {
console.log(`stderr: ${data}`);
});
f.on('close', code => {
console.log(`child process exited with code ${code}`);
if(code == 0) {
console.log("Successfully generated thumbnail");
const fp = "tmpthumb/" + v + ".jpg";
const uploadParams = {
Bucket: process.env.THUMBNAIL_S3_BUCKET_NAME,
Key: "test/"+ v +".jpg",
Body: fs.createReadStream(fp),
ContentType: 'image/jpeg'
};
s3.upload(uploadParams, function(err, data){
if(err) {
console.log(err);
res.send("fail");
} else {
res.send(data.Location);
}
});
} else if(code == 1) {
console.log("ffmpeg issue");
res.send("fail");
}
});
});
app.listen(3000, function () {
console.log('Snapa app listening on port 3000');
});