Skip to content

Instantly share code, notes, and snippets.

@adrian154
Created March 27, 2022 19:59
Show Gist options
  • Select an option

  • Save adrian154/5c1863b1192acb4233d7125a01f1a02b to your computer and use it in GitHub Desktop.

Select an option

Save adrian154/5c1863b1192acb4233d7125a01f1a02b to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
// quick and dirty script for downloading pixiv ugoira animations
// dependencies: node-fetch, 7zip (external)
const fetch = require("node-fetch");
const util = require("util");
const fs = require("fs");
const exec = util.promisify(require("child_process").exec);
(async () => {
// parse id
const PHPSESSID = process.argv[3];
const id = process.argv[2]?.match(/\d+/);
if(!id || !PHPSESSID) {
throw new Error("Usage: download-ugoira <pixiv URL> <PHPSESSIONID>");
}
// get meta
const metaResp = await fetch(`https://www.pixiv.net/ajax/illust/${id}/ugoira_meta?lang=en`, {
headers: {
"cookie": `PHPSESSID=${PHPSESSID}`,
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36"
}
});
if(!metaResp.ok) {
throw new Error("Failed to get ugoira meta: HTTP status " + resp.status);
}
const meta = await metaResp.json();
const src = meta.body.originalSrc;
console.log("Downloading ugoira files...");
const zipResp = await fetch(src, {
headers: {"referer": "https://www.pixiv.net/"}
});
if(!zipResp.ok) {
throw new Error("Failed to get ugoira ZIP: HTTP status " + resp.status);
}
zipResp.body.pipe(fs.createWriteStream(id + ".zip"));
await new Promise((resolve, reject) => {
zipResp.body.on("close", resolve);
zipResp.body.on("error", reject);
});
console.log("Downloaded!");
// unzip
console.log("Extracting...");
await exec(`7z x ${id}.zip -o${id}`);
// calculate fps
const fps = Math.round(1000 / meta.body.frames[1].delay);
console.log("Generating video at " + fps + " fps...");
// generate video
await exec(`ffmpeg -i ${id}/%06d.jpg -c:v libx264 -vf fps=${fps} -pix_fmt yuv420p ${id}.mp4`);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment