Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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