From db3cea5c0db5eaec373bedc51318cb74f356a7d2 Mon Sep 17 00:00:00 2001 From: wgroeneveld Date: Tue, 26 Jul 2022 12:02:19 +0200 Subject: [PATCH] remove imagemagick dependency and call using exec(), also optimize HLTB thumbnails after download --- README.md | 4 +++- package.json | 3 +-- src/howlongtobeat/howlong.js | 5 +++++ src/youtube/thumbify.js | 13 +++++-------- yarn.lock | 8 -------- 5 files changed, 14 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 128af5f..b230987 100644 --- a/README.md +++ b/README.md @@ -113,7 +113,7 @@ With Pagefind, there's no need to integrate it into jam-my-stack, greatly simpli Adds https://howlongtobeat.com/ game length (`MainGame`) and an ID to your front matter (keys `howlongtobeat_id` and `howlongtobeat_hrs`), provided you first added a property called `game_name`. (This gets substituted). -It also downloads a thumbnail of the cover image if you provided the dir as an option. +It also downloads a thumbnail of the cover image if you provided the dir as an option. The downloaded thumbnail is automatically optimized for the web using `mogrify` (this will emit a warning if you do not have ImageMagick installed locally). So, Frontmatter like this: @@ -175,6 +175,8 @@ Same as `getWebmentions`. Thanks to ideas from [rubenerd.com](https://rubenerd.com) and his [video.sh script](https://gitlab.com/rubenerd/rubenerd.com/-/blob/trunk/scripts/video.sh). This downloads a thumbnail using `youtube-dl`, smacks a play button on it using `convert`, and stores that in the specified folder. Use in conjunction with a Hugo shortcode to get rid of YouTube's iframes! +This method will fail if you do not have ImageMagick installed locally. + Usage example: ```js diff --git a/package.json b/package.json index b80cb41..a5f1837 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "jam-my-stack", - "version": "1.0.30", + "version": "1.0.31", "repository": { "url": "https://github.com/wgroeneveld/jam-my-stack", "type": "git" @@ -18,7 +18,6 @@ "fast-xml-parser": "^3.18.0", "got": "11.8.3", "howlongtobeat": "^1.5.1", - "imagemagick": "^0.1.3", "parser-front-matter": "^1.6.4", "youtube-dl-exec": "^1.2.4" }, diff --git a/src/howlongtobeat/howlong.js b/src/howlongtobeat/howlong.js index 345787b..2a470f2 100644 --- a/src/howlongtobeat/howlong.js +++ b/src/howlongtobeat/howlong.js @@ -8,6 +8,7 @@ const {promisify} = require('util'); const frontMatterParser = require('parser-front-matter'); const parse = promisify(frontMatterParser.parse.bind(frontMatterParser)); +const exec = promisify(require('child_process').exec); const stream = require('stream'); const pipeline = promisify(stream.pipeline); @@ -52,6 +53,10 @@ async function fillInHowLongToBeat(posts, downloadDir) { if(downloadDir) { await downloadThumbnail(game.imageUrl, game.id, downloadDir) + const { stdout, stderr } = await exec(`mogrify -sampling-factor 4:2:0 -strip -quality 85 -interlace JPEG -format jpg -colorspace sRGB ${downloadDir}/${game.id}.jpg`) + if(stderr) { + console.log(`-- WARN: unable to mogrify downloaded JPG: ${stderr}`) + } } } } diff --git a/src/youtube/thumbify.js b/src/youtube/thumbify.js index 4ac8e4e..b89ac11 100644 --- a/src/youtube/thumbify.js +++ b/src/youtube/thumbify.js @@ -3,7 +3,7 @@ const got = require("got"); const { createWriteStream, existsSync, unlinkSync } = require("fs"); const fs = require('fs').promises; const { getFiles } = require('./../file-utils'); -var im = require("imagemagick"); +const { exec } = require('child_process'); async function downloadThumbnail(youtubeid, downloadDir, overlayImg) { const dlLoc = `${downloadDir}/${youtubeid}.jpg` @@ -18,13 +18,10 @@ async function downloadThumbnail(youtubeid, downloadDir, overlayImg) { const done = new Promise(function(resolve, reject) { fStream.on('finish', () => { - var args = [ - "-resize", "700x400", dlLoc, - "+repage", "+page", overlayImg, "-flatten", dlLoc - ] - im.convert(args, function(err) { - if(err) reject(err) - resolve() + const child = exec(`convert -resize 700x400 -sampling-factor 4:2:0 -strip -quality 85 -interlace JPEG -format jpg -colorspace sRGB ${dlLoc} +repage +page ${overlayImg} -flatten ${dlLoc}`) + child.on('exit', code => { + if(code != 0) reject(`Mogrify returned non-zero exit code: ${code}`) + resolve() }) }) diff --git a/yarn.lock b/yarn.lock index 6085a9b..8cccdb8 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2799,13 +2799,6 @@ fsevents@^2.1.2: languageName: node linkType: hard -"imagemagick@npm:^0.1.3": - version: 0.1.3 - resolution: "imagemagick@npm:0.1.3" - checksum: 49a6479ae08a7b845ab13df835b87ecd46b8bf93a1872c7c0b3ace7858bf1edc7defd1f84dda9fe8e765051877a82487914551557939048236b7465a31d6f783 - languageName: node - linkType: hard - "import-local@npm:^3.0.2": version: 3.0.2 resolution: "import-local@npm:3.0.2" @@ -3214,7 +3207,6 @@ fsevents@^2.1.2: fast-xml-parser: ^3.18.0 got: 11.8.3 howlongtobeat: ^1.5.1 - imagemagick: ^0.1.3 jest: ^26.6.3 mockdate: ^3.0.2 parser-front-matter: ^1.6.4