Merge branch 'master' of https://github.com/lowlighter/metrics
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
//Imports
|
||||
import fs from "fs/promises"
|
||||
import fss from "fs"
|
||||
import os from "os"
|
||||
import paths from "path"
|
||||
import url from "url"
|
||||
@@ -13,6 +14,8 @@
|
||||
import opengraph from "open-graph-scraper"
|
||||
import rss from "rss-parser"
|
||||
import nodechartist from "node-chartist"
|
||||
import GIFEncoder from "gifencoder"
|
||||
import PNG from "png-js"
|
||||
|
||||
//Exports
|
||||
export {fs, os, paths, url, util, processes, axios, git, opengraph, rss}
|
||||
@@ -270,7 +273,7 @@
|
||||
await new Promise(solve => setTimeout(solve, seconds*1000)) //eslint-disable-line no-promise-executor-return
|
||||
}
|
||||
|
||||
/**Create gif from puppeteer browser */
|
||||
/**Create record from puppeteer browser */
|
||||
export async function record({page, width, height, frames, scale = 1, quality = 80, x = 0, y = 0, delay = 150}) {
|
||||
//Register images frames
|
||||
const images = []
|
||||
@@ -284,4 +287,33 @@
|
||||
//Post-processing
|
||||
console.debug("metrics/record > applying post-processing")
|
||||
return Promise.all(images.map(async buffer => (await jimp.read(buffer)).scale(scale).quality(quality).getBase64Async("image/png")))
|
||||
}
|
||||
|
||||
/**Create gif from puppeteer browser*/
|
||||
export async function gif({page, width, height, frames, x = 0, y = 0, repeat = true, delay = 150, quality = 10}) {
|
||||
//Create temporary stream
|
||||
const path = paths.join(os.tmpdir(), `${Math.round(Math.random()*1000000000)}.gif`)
|
||||
console.debug(`metrics/puppeteergif > set write stream to "${path}"`)
|
||||
if (fss.existsSync(path))
|
||||
await fs.unlink(path)
|
||||
//Create encoder
|
||||
const encoder = new GIFEncoder(width, height)
|
||||
encoder.createWriteStream().pipe(fss.createWriteStream(path))
|
||||
encoder.start()
|
||||
encoder.setRepeat(repeat ? 0 : -1)
|
||||
encoder.setDelay(delay)
|
||||
encoder.setQuality(quality)
|
||||
//Register frames
|
||||
for (let i = 0; i < frames; i++) {
|
||||
const buffer = new PNG(await page.screenshot({clip:{width, height, x, y}}))
|
||||
encoder.addFrame(await new Promise(solve => buffer.decode(pixels => solve(pixels)))) //eslint-disable-line no-promise-executor-return
|
||||
if (frames%10 === 0)
|
||||
console.debug(`metrics/puppeteergif > processed ${i}/${frames} frames`)
|
||||
}
|
||||
console.debug(`metrics/puppeteergif > processed ${frames}/${frames} frames`)
|
||||
//Close encoder and convert to base64
|
||||
encoder.finish()
|
||||
const result = await fs.readFile(path, "base64")
|
||||
await fs.unlink(path)
|
||||
return `data:image/gif;base64,${result}`
|
||||
}
|
||||
Reference in New Issue
Block a user