fix: don't die on missing optional dependency gifencoder
This commit is contained in:
@@ -764,26 +764,32 @@ export async function gif({page, width, height, frames, x = 0, y = 0, repeat = t
|
|||||||
if (fss.existsSync(path))
|
if (fss.existsSync(path))
|
||||||
await fs.unlink(path)
|
await fs.unlink(path)
|
||||||
//Create encoder
|
//Create encoder
|
||||||
const GIFEncoder = (await import("gifencoder")).default
|
try {
|
||||||
const encoder = new GIFEncoder(width, height)
|
const GIFEncoder = (await import("gifencoder")).default
|
||||||
encoder.createWriteStream().pipe(fss.createWriteStream(path))
|
const encoder = new GIFEncoder(width, height)
|
||||||
encoder.start()
|
encoder.createWriteStream().pipe(fss.createWriteStream(path))
|
||||||
encoder.setRepeat(repeat ? 0 : -1)
|
encoder.start()
|
||||||
encoder.setDelay(delay)
|
encoder.setRepeat(repeat ? 0 : -1)
|
||||||
encoder.setQuality(quality)
|
encoder.setDelay(delay)
|
||||||
//Register frames
|
encoder.setQuality(quality)
|
||||||
for (let i = 0; i < frames; i++) {
|
//Register frames
|
||||||
const buffer = new PNG(await page.screenshot({clip: {width, height, x, y}}))
|
for (let i = 0; i < frames; i++) {
|
||||||
encoder.addFrame(await new Promise(solve => buffer.decode(pixels => solve(pixels))))
|
const buffer = new PNG(await page.screenshot({clip: {width, height, x, y}}))
|
||||||
if (frames % 10 === 0)
|
encoder.addFrame(await new Promise(solve => buffer.decode(pixels => solve(pixels))))
|
||||||
console.debug(`metrics/puppeteergif > processed ${i}/${frames} frames`)
|
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}`
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
console.debug(`metrics/puppeteergif > could not create gif: ${error}`)
|
||||||
|
return "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mOcOnfpfwAGfgLYttYINwAAAABJRU5ErkJggg=="
|
||||||
}
|
}
|
||||||
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}`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**D3 node wrapper (loosely based on https://github.com/d3-node/d3-node)*/
|
/**D3 node wrapper (loosely based on https://github.com/d3-node/d3-node)*/
|
||||||
|
|||||||
Reference in New Issue
Block a user