Skip to content

Instantly share code, notes, and snippets.

@kakai248
Created October 3, 2019 10:18
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save kakai248/69ae625ec51d92d0e08f603e55d5f523 to your computer and use it in GitHub Desktop.
const puppeteerLottie = require("puppeteer-lottie");
const fs = require("fs");
const baseInputDir = "lotties/";
const baseOutputDir = "lotties-rendered/";
const outputExtension = '.mp4';
(async () => {
await run();
})();
async function run() {
const files = fs.readdirSync(baseInputDir);
await convertLotties(files);
generateIndex(files);
}
async function convertLotties(files) {
files.forEach(async (file) => {
await renderLottie(baseInputDir + file, baseOutputDir + file);
});
}
async function renderLottie(inputFilePath, outputFilePath) {
await puppeteerLottie({
path: inputFilePath,
output: outputFilePath.replace('.json', outputExtension)
});
}
function generateIndex(files) {
const header = `
<link rel="stylesheet" href="styles/index.css">
<title>Lottie Catalog</title>
`;
var body = '';
files.forEach(file => {
const renderedFile = baseOutputDir + file.replace('.json', outputExtension);
body += `
<div class="lottie">
<p class="lottie-title">` + file + `</p>
<video controls muted autoplay loop width="400">
<source src="` + renderedFile + `" type="video/mp4">
</video>
</div>
`;
});
const html = `
<!DOCTYPE html>
<html>
<head>` + header + `</head>
<body>` + body + `</body>
</html>
`;
const stream = fs.createWriteStream("index.html");
stream.once('open', function(fd) {
stream.end(html);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment