Skip to content

Instantly share code, notes, and snippets.

@zolotokrylin
Created November 13, 2017 13:41
Show Gist options
  • Save zolotokrylin/0701068350e89f66c0abcd12df15bfd6 to your computer and use it in GitHub Desktop.
Save zolotokrylin/0701068350e89f66c0abcd12df15bfd6 to your computer and use it in GitHub Desktop.
Gif splitter
{
"name": "gif_renamer",
"version": "0.1.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"sharp": "^0.18.2"
}
}
// split files into folder, based on the mime type
const srcFolder = './img_src/';
const outFolder = './img_out/';
const fs = require('fs');
const sharp = require('sharp');
// scan img_src folder and get an array of files
fs.readdir(srcFolder, (err, files) => {
// go through each file and compare file extension with file type (check header)
files.forEach(file => {
var srcPath = srcFolder + file
compareExtVsType(srcPath, file);
});
})
function compareExtVsType(srcPath, file) {
var img = sharp(srcPath)
img.metadata().then(data => {
format = data.format
fileSlices = file.match(/^(.*)\.(.*)$/);
if (fileSlices[2] !== format && format == "gif") {
fs.createReadStream(srcPath).pipe(fs.createWriteStream(`${outFolder}/${fileSlices[1]}.gif`));
// console.log(`${file} has ${format} format, it was copied to ${outFolder}/${fileSlices[1]}.gif`);
console.log(`'${file}',`)
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment