Created
December 12, 2024 18:47
-
-
Save tomhermans/527d56ec3345565177f743f1d5de5d72 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// run: node avifconvert.js | |
const fs = require('fs'); | |
const path = require('path'); | |
const sharp = require('sharp'); | |
const readline = require('readline'); | |
const rl = readline.createInterface({ | |
input: process.stdin, | |
output: process.stdout | |
}); | |
function askQuestion(query) { | |
return new Promise(resolve => rl.question(query, resolve)); | |
} | |
async function main() { | |
// Ask for input folder | |
const inputFolder = await askQuestion('Enter the input folder path (press Enter for current folder): ') || '.'; | |
// Ask for output format | |
let outputFormat; | |
do { | |
outputFormat = await askQuestion('Enter the output format (jpg or png): ').then(ans => ans.toLowerCase()); | |
} while (outputFormat !== 'jpg' && outputFormat !== 'png'); | |
// Create output folder inside the input folder | |
const timestamp = new Date().toISOString().replace(/[:.-]/g, '_'); | |
const outputFolder = path.join(inputFolder, `output_${timestamp}`); | |
fs.mkdirSync(outputFolder, { recursive: true }); | |
console.log(`Input folder: ${inputFolder}`); | |
console.log(`Output folder: ${outputFolder}`); | |
console.log(`Output format: ${outputFormat}`); | |
// Process AVIF files | |
fs.readdir(inputFolder, (err, files) => { | |
if (err) { | |
console.error('Error reading input folder:', err); | |
rl.close(); | |
return; | |
} | |
const avifFiles = files.filter(file => path.extname(file).toLowerCase() === '.avif'); | |
console.log(`Found ${avifFiles.length} AVIF files to convert.`); | |
if (avifFiles.length === 0) { | |
console.log('No AVIF files found to process.'); | |
rl.close(); | |
return; | |
} | |
let processedCount = 0; | |
let errorCount = 0; | |
avifFiles.forEach(file => { | |
const inputPath = path.join(inputFolder, file); | |
const outputFileName = `${path.parse(file).name}.${outputFormat}`; | |
const outputPath = path.join(outputFolder, outputFileName); | |
console.log(`Converting: ${file}`); | |
sharp(inputPath) | |
.toFormat(outputFormat, { | |
quality: 90, // You can adjust the quality setting as needed | |
mozjpeg: outputFormat === 'jpg', // Use mozjpeg for better JPEG compression | |
}) | |
.toFile(outputPath) | |
.then(() => { | |
console.log(`Successfully converted: ${file} -> ${outputFileName}`); | |
processedCount++; | |
if (processedCount + errorCount === avifFiles.length) { | |
console.log(`\nConversion complete. ${processedCount} files converted, ${errorCount} errors.`); | |
rl.close(); | |
} | |
}) | |
.catch(err => { | |
console.error(`Error converting ${file}:`, err); | |
errorCount++; | |
if (processedCount + errorCount === avifFiles.length) { | |
console.log(`\nConversion complete. ${processedCount} files converted, ${errorCount} errors.`); | |
rl.close(); | |
} | |
}); | |
}); | |
}); | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment