The swiss army knife of lossless video/audio editing
releaseImage: always text on the left, darken as much as the photo needs, smart crop
Claudedf65eaafe37c
diff --git a/AGENTS.md b/AGENTS.mdindex b99961cb..8cba65b5 100644--- a/AGENTS.md+++ b/AGENTS.md@@ -41,5 +41,5 @@ After a GitHub release is published, draft one post per channel from `versions/<- Start with "LosslessCut x.y.z is out", then the 1–3 most interesting changes for users, then the release link (https://github.com/mifi/lossless-cut/releases/tag/vx.y.z). Discord can take the full list.- Plain text, no issue references. The link counts toward the limit (on X as 23 chars). A patch release that only fixes one platform can be a single line.-- Attach an image made with `node script/releaseImage.ts <version> [--bg photo.jpg] [--caption "..."] [--square] "highlight 1" "highlight 2" "highlight 3"` (1600×900 PNG in `dist/`, or 1080×1080 with `--square`, e.g. for Instagram). Ask the user for a recent photo of theirs to use as the background (optional; the text goes on its darker half, over a blurred and darkened area) and a short caption for it (where/what, shown small in the corner). Keep highlights short so they fit on one line, and look at the result before using it. Never commit the photo or the image. Add alt text, e.g. "LosslessCut 3.70.1: <highlights>".+- Attach an image made with `node script/releaseImage.ts <version> [--bg photo.jpg] [--position top|bottom|left|right|center] [--caption "..."] [--square] "highlight 1" "highlight 2" "highlight 3"` (1600×900 PNG in `dist/`, or 1080×1080 with `--square`, e.g. for Instagram). Ask the user for a recent photo of theirs to use as the background (optional) and a short caption for it (where/what, shown small in the corner). The photo is cropped to keep its most interesting part; look at the result, and if the subject is cut off (e.g. a portrait photo) or hidden behind the text, try `--position`. Keep highlights short so they fit on one line. Never commit the photo or the image. Add alt text, e.g. "LosslessCut 3.70.1: <highlights>".- Post through the user's logged-in browser. Fill in the post and let the user confirm before clicking Post.diff --git a/script/releaseImage.ts b/script/releaseImage.tsindex 651c1fb6..36707322 100644--- a/script/releaseImage.ts+++ b/script/releaseImage.ts@@ -1,14 +1,14 @@// Generates an image for release announcements, see AGENTS.md "Release announcement"-// Usage: node script/releaseImage.ts <version> [--bg photo.jpg] [--caption "photo caption"] [--square] [--out file.png] "highlight 1" "highlight 2" ...+// Usage: node script/releaseImage.ts <version> [--bg photo.jpg] [--position top|bottom|left|right|...] [--caption "photo caption"] [--square] [--out file.png] "highlight 1" "highlight 2" ...import assert from 'node:assert';import { parseArgs } from 'node:util';import sharp from 'sharp';-const { values: { bg, caption, square, out }, positionals: [version, ...highlights] } = parseArgs({+const { values: { bg, position, caption, square, out }, positionals: [version, ...highlights] } = parseArgs({allowPositionals: true,- options: { bg: { type: 'string' }, caption: { type: 'string' }, square: { type: 'boolean' }, out: { type: 'string' } },+ options: { bg: { type: 'string' }, position: { type: 'string' }, caption: { type: 'string' }, square: { type: 'boolean' }, out: { type: 'string' } },});-assert(version != null && highlights.length <= 3, 'Usage: releaseImage.ts <version> [--bg photo.jpg] [--caption "photo caption"] [--square] [--out file.png] [up to 3 highlights]');+assert(version != null && highlights.length <= 3, 'Usage: releaseImage.ts <version> [--bg photo.jpg] [--position top|bottom|...] [--caption "photo caption"] [--square] [--out file.png] [up to 3 highlights]');// 16:9 for X/Bluesky/Mastodon/Discord, or square for Instagramconst width = square ? 1080 : 1600;@@ -19,7 +19,8 @@ const scale = square ? 0.8 : 1;const escapeXml = (str: string) => str.replaceAll(/[<>&'"]/g, (c) => `&#${c.codePointAt(0)};`);const svg = (content: string) => Buffer.from(`<svg width="${width}" height="${height}" xmlns="http://www.w3.org/2000/svg">${content}</svg>`);-const photo = bg != null ? await sharp(bg).rotate().resize(width, height, { fit: 'cover' }).toBuffer() : undefined;+// Crop the photo (e.g. a portrait) to the image's aspect ratio. By default keep the most interesting part (sharp's attention strategy), or the part given by --position (e.g. top, bottom, left, right, center)+const photo = bg != null ? await sharp(bg).rotate().resize(width, height, { fit: 'cover', position: position ?? sharp.strategy.attention }).toBuffer() : undefined;// Accent color: the photo's dominant color, or else a hue derived from the version, so every release looks a bit differentasync function getAccentHue() {@@ -39,35 +40,35 @@ async function getAccentHue() {const hue = await getAccentHue();const accent = `hsl(${hue}, 80%, 65%)`;-// Put the text on the darker half of the photo-async function getMeanBrightness(left: number) {- // stats() reads the input, so the extracted half has to be rendered first- const half = await sharp(photo).extract({ left, top: 0, width: width / 2, height }).greyscale().toBuffer();- const { channels } = await sharp(half).stats();+// Darken the text area (left side) enough for the text to stay readable, also on bright photos+async function getTextAreaBrightness() {+ if (photo == null) return 0;+ // stats() reads the input, so the extracted area has to be rendered first+ const area = await sharp(photo).extract({ left: 0, top: 0, width: Math.round(width * 0.6), height }).greyscale().toBuffer();+ const { channels } = await sharp(area).stats();return channels[0]!.mean;}-const textOnRight = photo != null && square !== true && await getMeanBrightness(width / 2) < await getMeanBrightness(0);-const x = textOnRight ? width - margin : margin;-const anchor = textOnRight ? 'end' : 'start';+const brightness = await getTextAreaBrightness();+// Opacity that brings the area's mean brightness down to about 40 (of 255)+const shadeOpacity = Math.min(0.95, Math.max(0.7, 1 - 40 / Math.max(brightness, 1)));-// Darkens (and blurs) the text side, fading out towards the other side-const fade = (id: string, stops: [number, number][]) => `<linearGradient id="${id}" x1="${textOnRight ? 1 : 0}" x2="${textOnRight ? 0 : 1}" y1="0" y2="0">${stops.map(([offset, opacity]) => `<stop offset="${offset}" stop-color="hsl(${hue}, 60%, 7%)" stop-opacity="${opacity}" />`).join('')}</linearGradient>`;+// Darkens (and blurs) the left side, fading out towards the right+const fade = (id: string, stops: [number, number][]) => `<linearGradient id="${id}" x1="0" x2="1" y1="0" y2="0">${stops.map(([offset, opacity]) => `<stop offset="${offset}" stop-color="hsl(${hue}, 60%, 7%)" stop-opacity="${opacity}" />`).join('')}</linearGradient>`;const font = 'Helvetica Neue, Helvetica, Arial, sans-serif';-const text = (textX: number, y: number, size: number, attrs: string, content: string, textAnchor = anchor) => `<text x="${textX}" y="${y}" text-anchor="${textAnchor}" font-family="${font}" font-size="${size}" ${attrs}>${content}</text>`;+const text = (x: number, y: number, size: number, attrs: string, content: string, anchor = 'start') => `<text x="${x}" y="${y}" text-anchor="${anchor}" font-family="${font}" font-size="${size}" ${attrs}>${content}</text>`;const iconSize = Math.round(110 * scale);const top = square ? 180 : 125;const lineY = (offset: number) => top + offset * scale;-const titleX = textOnRight ? x - iconSize - 30 * scale : x + iconSize + 30 * scale;const overlay = svg(`- <defs>${fade('shade', [[0, 0.9], [0.6, 0.6], [1, 0.15]])}</defs>+ <defs>${fade('shade', [[0, shadeOpacity], [0.55, shadeOpacity * 0.8], [1, 0.15]])}</defs><rect width="100%" height="100%" fill="url(#shade)" />- ${text(titleX, lineY(80), 64 * scale, 'font-weight="300" fill="#fff"', 'LosslessCut')}- ${text(x, lineY(275), 150 * scale, `font-weight="700" fill="${accent}"`, escapeXml(version))}- ${highlights.map((highlight, i) => text(x, lineY(385 + i * 70), 44 * scale, 'fill="#fff" fill-opacity="0.92"', `<tspan fill="${accent}">•</tspan> ${escapeXml(highlight)}`)).join('\n ')}- ${text(x, height - 80, 36 * scale, 'fill="#fff" fill-opacity="0.75"', 'losslesscut.app')}- ${caption != null ? text(textOnRight ? margin : width - margin, height - 40, 20, 'fill="#fff" fill-opacity="0.6"', escapeXml(caption), textOnRight ? 'start' : 'end') : ''}+ ${text(margin + iconSize + 30 * scale, lineY(80), 64 * scale, 'font-weight="300" fill="#fff"', 'LosslessCut')}+ ${text(margin, lineY(275), 150 * scale, `font-weight="700" fill="${accent}"`, escapeXml(version))}+ ${highlights.map((highlight, i) => text(margin, lineY(385 + i * 70), 44 * scale, 'fill="#fff" fill-opacity="0.92"', `<tspan fill="${accent}">•</tspan> ${escapeXml(highlight)}`)).join('\n ')}+ ${text(margin, height - 80, 36 * scale, 'fill="#fff" fill-opacity="0.75"', 'losslesscut.app')}+ ${caption != null ? text(width - margin, height - 40, 20, 'fill="#fff" fill-opacity="0.85" stroke="#000" stroke-opacity="0.6" stroke-width="3" paint-order="stroke"', escapeXml(caption), 'end') : ''}`);async function getBackground() {@@ -85,7 +86,7 @@ const icon = await sharp('src/renderer/src/icon.svg').resize(iconSize, iconSize)const outPath = out ?? `dist/release-${version}${square ? '-square' : ''}.png`;await sharp(await getBackground())- .composite([{ input: overlay }, { input: icon, left: textOnRight ? x - iconSize : x, top }])+ .composite([{ input: overlay }, { input: icon, left: margin, top }]).png().toFile(outPath);console.log('Wrote', outPath);