Skip to content

Instantly share code, notes, and snippets.

View rileyjshaw's full-sized avatar
💭
Riley is typing…

Riley Shaw rileyjshaw

💭
Riley is typing…
View GitHub Profile
@rileyjshaw
rileyjshaw / generate-theme.js
Created February 6, 2023 17:37
Convert a list of 10 colors to a Firefox theme manifest.
// Convert a list of 10 colors (eg. generated by https://primer.style/prism/)
// to a Firefox theme manifest.
//
// name: The name of the theme.
// colors: An array of 10 colors.
// id: The ID of the theme. Format: "your-add-on-name@your-domain.com".
// version (optional): The version of the theme. Format: "<major>.<minor>".
//
// Docs: https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/theme
//
@rileyjshaw
rileyjshaw / potato.js
Created January 27, 2023 20:22
Playing Oliver Darkshire’s “Potato” a million times
// Game rules: https://twitter.com/deathbybadger/status/1567425842526945280
const resultCounter = {
whisked: 0,
burrowed: 0,
eaten: 0,
};
const gameState = {};
const roll = () => Math.floor(Math.random() * 6);
@rileyjshaw
rileyjshaw / prepare-video-for-web.sh
Created December 15, 2022 17:50
Prepare a video file for the web
# Source: https://evilmartians.com/chronicles/better-web-video-with-av1-codec
# Encode as H.264. Reduce quality by increasing the CRF value. If you're looking for an output that is roughly "visually lossless" but not technically lossless, use a -crf value of around 17 or 18.
ffmpeg -i <SOURCE> -map_metadata -1 -c:a libfdk_aac -c:v libx264 -crf 28 -preset veryslow -profile:v main -pix_fmt yuv420p -movflags +faststart -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" video.h264.mp4
# Encode as AV1. Reduce quality by increasing the CP value.
ffmpeg -i <SOURCE> -map_metadata -1 -c:a libopus -c:v librav1e -qp 80 -tile-columns 2 -tile-rows 2 -pix_fmt yuv420p -movflags +faststart -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" video.av1.mp4
# Encode as HEVC. Reduce quality by increasing the CRF value.
ffmpeg -i <SOURCE> -map_metadata -1 -c:a libfdk_aac -c:v libx265 -crf 28 -preset veryslow -pix_fmt yuv420p -movflags +faststart -tag:v hvc1 -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" video.hevc.mp4
@rileyjshaw
rileyjshaw / validateSIN.js
Created February 11, 2015 06:23
Validate a Canadian Social Insurance Number (SIN)
/**
* Validate a Canadian Social Insurance Number (SIN)
* @param {num || str} sin A 9-digit Canadian SIN
* @return {bool} Validity of the input SIN
*/
function validateSIN (sin) {
var check, even, tot;
if (typeof sin === 'number') {
sin = sin.toString();
@rileyjshaw
rileyjshaw / add-track-numbers.sh
Last active July 27, 2022 14:28
Adds ID3v1 and ID3v2 track information to a folder of mp3s, based on the output order of `ls`.
#!/bin/bash
FILES=(*.mp3)
for i in "${!FILES[@]}"; do
id3v2 -T "$((i + 1))/${#FILES[@]}" "${FILES[i]}"
done
@rileyjshaw
rileyjshaw / mp3titles.sh
Created July 27, 2022 14:16
MacOS: Enter all subdirectories of the current folder and add "00. Title.mp3", which reads the folder name.
#!/bin/bash
for d in */; do
cd "$d"
say "$d" -o "00. Title.aiff"
lame -m m "00. Title.aiff" "00. Title.mp3"
rm "00. Title.aiff"
cd ..
done
@rileyjshaw
rileyjshaw / mp3numbers.sh
Created July 26, 2022 14:17
Generate a series of MP3s on MacOS. Filenames are left-padded with 0s.
#!/bin/bash
for i in {001..100}; do
say ${i##+(0)} -o "$i.aiff"
lame -m m "$i.aiff" "$i.mp3"
rm "$i.aiff"
done
@rileyjshaw
rileyjshaw / NAND-heart.js
Last active May 25, 2022 23:08
A quick script I wrote to generate a 5-bit heart image (Persistence of Vision) using a 555 timer, a binary counter, 16 NAND gates, and some LEDs.
const ENABLE_IMAGE_ROTATIONS = true;
const ITERATIONS_PER_ROTATION = 10000000;
const MAX_GATES_TOTAL = 20;
const MAX_TREE_DEPTH = 6;
const rand = array => array[Math.floor(Math.random() * array.length)];
const unique = (x, i, array) => array.indexOf(x) === i;
const limit = (name, f, lo, hi = lo) => (...args) => {
const { length: l } = args;
if (l < lo || l > hi)
@rileyjshaw
rileyjshaw / make_woffs.sh
Created May 3, 2021 02:32
A one-liner to convert a folder of TTFs (also works with OTFs) to WOFF and WOFF2
mkdir -p woff && for f in *.ttf; do sfnt2woff "$f"; woff2_compress "$f"; done && mv *.woff* woff
@rileyjshaw
rileyjshaw / renumber.sh
Created February 1, 2022 16:23
Rename files to `01.ext`, `02.ext`, etc. based on output order of `ls`
#!/bin/bash
FILES=($(ls -tUr *.{png,jpeg}))
for i in "${!FILES[@]}"; do
mv "${FILES[i]}" "$(printf "%02d.${FILES[i]##*.}" "$((i + 1))")"
done