remove imagemagick dependency and call using exec(), also optimize HLTB thumbnails after download

This commit is contained in:
Wouter Groeneveld 2022-07-26 12:02:19 +02:00
parent bf5acb097c
commit db3cea5c0d
5 changed files with 14 additions and 19 deletions

View File

@ -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). 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: 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! 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: Usage example:
```js ```js

View File

@ -1,6 +1,6 @@
{ {
"name": "jam-my-stack", "name": "jam-my-stack",
"version": "1.0.30", "version": "1.0.31",
"repository": { "repository": {
"url": "https://github.com/wgroeneveld/jam-my-stack", "url": "https://github.com/wgroeneveld/jam-my-stack",
"type": "git" "type": "git"
@ -18,7 +18,6 @@
"fast-xml-parser": "^3.18.0", "fast-xml-parser": "^3.18.0",
"got": "11.8.3", "got": "11.8.3",
"howlongtobeat": "^1.5.1", "howlongtobeat": "^1.5.1",
"imagemagick": "^0.1.3",
"parser-front-matter": "^1.6.4", "parser-front-matter": "^1.6.4",
"youtube-dl-exec": "^1.2.4" "youtube-dl-exec": "^1.2.4"
}, },

View File

@ -8,6 +8,7 @@ const {promisify} = require('util');
const frontMatterParser = require('parser-front-matter'); const frontMatterParser = require('parser-front-matter');
const parse = promisify(frontMatterParser.parse.bind(frontMatterParser)); const parse = promisify(frontMatterParser.parse.bind(frontMatterParser));
const exec = promisify(require('child_process').exec);
const stream = require('stream'); const stream = require('stream');
const pipeline = promisify(stream.pipeline); const pipeline = promisify(stream.pipeline);
@ -52,6 +53,10 @@ async function fillInHowLongToBeat(posts, downloadDir) {
if(downloadDir) { if(downloadDir) {
await downloadThumbnail(game.imageUrl, game.id, 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}`)
}
} }
} }
} }

View File

@ -3,7 +3,7 @@ const got = require("got");
const { createWriteStream, existsSync, unlinkSync } = require("fs"); const { createWriteStream, existsSync, unlinkSync } = require("fs");
const fs = require('fs').promises; const fs = require('fs').promises;
const { getFiles } = require('./../file-utils'); const { getFiles } = require('./../file-utils');
var im = require("imagemagick"); const { exec } = require('child_process');
async function downloadThumbnail(youtubeid, downloadDir, overlayImg) { async function downloadThumbnail(youtubeid, downloadDir, overlayImg) {
const dlLoc = `${downloadDir}/${youtubeid}.jpg` const dlLoc = `${downloadDir}/${youtubeid}.jpg`
@ -18,13 +18,10 @@ async function downloadThumbnail(youtubeid, downloadDir, overlayImg) {
const done = new Promise(function(resolve, reject) { const done = new Promise(function(resolve, reject) {
fStream.on('finish', () => { fStream.on('finish', () => {
var args = [ 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}`)
"-resize", "700x400", dlLoc, child.on('exit', code => {
"+repage", "+page", overlayImg, "-flatten", dlLoc if(code != 0) reject(`Mogrify returned non-zero exit code: ${code}`)
] resolve()
im.convert(args, function(err) {
if(err) reject(err)
resolve()
}) })
}) })

View File

@ -2799,13 +2799,6 @@ fsevents@^2.1.2:
languageName: node languageName: node
linkType: hard 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": "import-local@npm:^3.0.2":
version: 3.0.2 version: 3.0.2
resolution: "import-local@npm:3.0.2" resolution: "import-local@npm:3.0.2"
@ -3214,7 +3207,6 @@ fsevents@^2.1.2:
fast-xml-parser: ^3.18.0 fast-xml-parser: ^3.18.0
got: 11.8.3 got: 11.8.3
howlongtobeat: ^1.5.1 howlongtobeat: ^1.5.1
imagemagick: ^0.1.3
jest: ^26.6.3 jest: ^26.6.3
mockdate: ^3.0.2 mockdate: ^3.0.2
parser-front-matter: ^1.6.4 parser-front-matter: ^1.6.4