-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.js
executable file
·67 lines (57 loc) · 1.99 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
#!/usr/bin/env node
const fs = require('fs');
const GIFEncoder = require('gifencoder');
const path = require('path');
const pngFileStream = require('png-file-stream');
const puppeteer = require('puppeteer');
const tempdir = require('tempdir');
const argv = require('yargs')
.alias('url', 'u').default('url', 'https://giphy.com/search/lol')
.describe('url', 'URL to generate GIF from')
.alias('duration', 'd').default('duration', 10)
.describe('duration', 'GIF duration in seconds')
.alias('output', 'o').default('output', `${process.cwd()}${require('path').sep}web.gif`)
.describe('output', 'Output file name')
.alias('h', 'help')
.alias('V', 'version')
.usage('webgif -u URL -d DURATION [-o OUTFILE]')
.version()
.argv;
(async () => {
const browser = await puppeteer.launch({
ignoreHTTPSErrors: true,
args: ['--allow-running-insecure-content', '--disable-setuid-sandbox', '--no-sandbox', ],
});
const page = await browser.newPage();
const workdir = await tempdir();
page.setViewport({
width: 1024,
height: 768,
});
console.log(`Navigating to URL: ${argv.url}`);
await page.goto(argv.url);
process.stdout.write('Taking screenshots: .');
const screenshotPromises = [];
for (let i = 1; i <= argv.duration; ++i) {
filename = `${workdir}/T${new Date().getTime()}.png`;
process.stdout.write('.');
screenshotPromises.push(page.screenshot({ path: filename, }));
await delay(1000);
}
await delay(1000);
await Promise.all(screenshotPromises);
console.log(`\nEncoding GIF: ${argv.output}`);
const encoder = new GIFEncoder(1024, 768);
await pngFileStream(`${workdir}/T*png`)
.pipe(encoder.createWriteStream({ repeat: 0, delay: 200, quality: 20 }))
.pipe(fs.createWriteStream(`${argv.output}`));
await page.close();
await browser.close();
})();
/* istanbul ignore next */
process.on('unhandledRejection', function(reason, p) {
throw new Error(reason);
});
function delay(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}