Skip to content

Instantly share code, notes, and snippets.

@bastienrobert
Created May 20, 2020 09:50
Show Gist options
  • Save bastienrobert/6506dd6c782a93142bd2c584adbe60f4 to your computer and use it in GitHub Desktop.
Save bastienrobert/6506dd6c782a93142bd2c584adbe60f4 to your computer and use it in GitHub Desktop.
convert folder full of PNG sprite into animated WEBP with alpha
const { exec } = require('child_process')
const fs = require('fs')
const path = require('path')
const args = process.argv.slice(2)
const FPS = 30
const FRAME_DURATION_MS = 1000 / FPS
if (!args[0]) console.log('please set a folder in params')
else {
fs.readdir(args[0], (err, files) => {
const frames = files.map((file) => {
return path.extname(file) === '.png'
? `${args[0]}/${file} -d ${FRAME_DURATION_MS} `
: ''
})
exec(
`img2webp ${frames.join('')} -o ${args[0]}/out.webp`,
(err, stdout) => {
if (err) {
console.error(`exec error: ${err}`)
return
}
console.log(stdout)
}
)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment