Home

now

wiki

about

Grimoire

Inspired by the grimoire of Fred Bednarski, this is my place to put assorted snippets of code (which may or may not be cursed, so use at your own risk). Have fun, poke around, but remember that this information may be totally borked.

To change the extensions of audio files in a directory:
bash

for file in *.m4a; do ffmpeg -i "${file}" "${file/%m4a/ogg}" && rm $file; done

More generally, to do something to some files:
bash

for file in $(ls | grep "something"); do echo "match: ${file}"; done

To download the mp3 audio (or the best audio if that fails) of a youtube video:
bash

youtube-dl --add-metadata -i -x -f mp3/bestaudio "video_link"

To download a playlist of music from youtube (more stable for playlists than youtube-dl):
bash

youtube-dlc -o '%(playlist_index)s - %(title)s.%(ext)s' -x -f mp3/bestaudio "[playlist url]"

To forward your port 8388 to a server's port 8384 (can be used to get the web interface for syncthing remotely):
bash

ssh -L 127.0.0.1:8388:127.0.0.1:8384 username@yourserver

To sanitize a file name (the .opus can be any extension):
bash

mv "$file" "$(echo "${file%.opus}" | iconv -cf UTF-8 -t ASCII//TRANSLIT | tr -d '[:punct:]' | tr '[:upper:]' '[:lower:]' | tr ' ' '-' | sed "s/-\+/-/g;s/\(^-\|-\$\)//g").opus"

To sanitize everything in a directory, preserving extensions:
Fish

function sanitize for file in * set ext (string match -r "\.\w*\$" $file | \ string trim --chars=.) set new (echo "{$file}" | \ iconv -cf UTF-8 -t ASCII//TRANSLIT | \ tr -d '[:punct:]' | \ tr '[:upper:]' '[:lower:]' | \ tr ' ' '-' | \ sed "s/-\+/-/g;s/\(^-\|-\$\)//g"); set final (string replace "$ext" ".$ext" "$new") if [ "$file" != "$final" ] mv "$file" "$final" end end end

To push file changes over ssh:
bash

rsync -azP --delete ~/Folder/ user@123.456.8.910:/home/name/Folder

To dither and shrink an image (courtesy of Thomasorus with some modification for personal preference):
bash

mogrify -format gif -filter Triangle -define filter:support=2 -thumbnail $size -monochrome -unsharp 0.25x0.08+8.3+0.045 -dither FloydSteinberg -colors 2 -posterize 136 -quality 82 -define jpeg:fancy-upsampling=off -define png:compression-filter=5 -define png:compression-level=9 -define png:compression-strategy=1 -define png:exclude-chunk=all -interlace none -colorspace gray -normalize $from

To simply dither of an image:
bash

convert image.jpg -colorspace gray -ordered-dither o8x8 image_dithered.png

Super simple, but to make a div element take up the whole height of a veiwport
CSS

height: 100vh;

This is something pretty specific (and simple), but this is how I fixed my audio continuously muting in Manjaro i3:
bash

alsactl restore

To play a streaming file
bash

mplayer -prefer-ipv4 -playlist kexp128.mp3.m3u

Grab some html from the Noita wiki
bash

curl "https://noita.gamepedia.com/Oil_(Spell)" | awk '/table class="spell"/ {for(i=1; i<=69; i++) {getline; print}}'

Find all files which match a given regex
bash

grep -rnw 'dir/' -e 'pattern'

Sorta change the colors of an image? With black and white, it'll change the white to something else (in this case orange)
ImageMagick

convert selfie.jpg +level-colors black,"#ff8000" selfie-spoopy.png

Function to convert a string into a hashed long
C89

long hash(char *str) { long hash = 5381; int c; while ((c = *str++)) hash = ((hash << 5) + hash) + c; return hash; }

Copy an array to a pointer's value
C

int dst[ARRAY_LENGTH]; memcpy(dst, src, sizeof(dst));

Convert an mp4 to an animated gif
ffmpeg

ffmpeg -i crow-walk.mp4 -vf "fps=10,scale=320:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" -loop 0 crow-walk.gif

Get the audio from an mp4
ffmpeg

ffmpeg -i cats.mp4 -q:a 0 -map a audio.mp3

Convert gif to mp4
ffmpeg

ffmpeg -i cats.gif cats-dith.mp4

Combine audio and video files
ffmpeg

ffmpeg -i cats-dith.mp4 -i audio.mp3 -c:v copy -c:a aac output.mp4

Add centered text in white to a video
ffmpeg

ffmpeg -i crow-walk.mp4 -vf "drawtext=text='crow walk:fontcolor=white:fontsize=24:box=1:boxcolor=black@0.0:boxborderw=5:x=(w-text_w)/2:y=(h-text_h)/2" -codec:a copy crow-walk-text.mp4

Convert mkv to mp4 preserving subtitles
ffmpeg

ffmpeg -i input.mkv -c copy -c:s mov_text output.mp4

Find the file with the most lines in a directory
bash

wc -l pages/* 2>/dev/null | grep -v ' total$' | sort -n -k1 | tail -1l

Crop an image and the virtual canvas (so a "normal crop" basically)
ImageMagick

convert in.jpg -crop {width}x{height}+{x-offset}+{y-offset} +repage out.png

Cut a video file with start and end points
ffmpeg

ffmpeg -i input.mp4 -ss 00:00:43 -to 01:15:26 -c copy output.mp4

Combine videos from text file
bash/ffmpeg

> cat files.txt

file 'input1.mp4' \n file 'input2.mp4'

ffmpeg -f concat -safe 0 -i files.txt -c copy output.mp4

Count all the characters in all files in a directory
bash

find ./* -type f -exec cat {} + | wc -m

Get disk information (available, used, etc)
bash

df -h

Prints IP addresses for computer
bash

ip addr show | grep "inet "

Gets all ip addresses on local network. Requires `arp-scan`, and replace `wlp3s0` with your network device (as shown by running `ifconfig`)
bash

sudo arp-scan --interface=wlp3s0 --localnet

Get open ports/ips on local system
bash

nmap -vv -sV localhost -oG - | awk -v OFS=':' ' /open/ { for (i=4;i<=NF;i++) { split($i,a,"/"); if (a[2]=="open") print $2, a[1] } }'

Pretty-print devices across network range
fish

for i in (seq $start $end); printf "\n$i\n" echo "-------------------------------------------" sudo arp-scan --interface=wlp3s0 --localnet | grep -v "Unknown" | grep "100\.64\.$i" end > hosts.txt

Downloads whole website
bash

wget -m -k -K -E -l 7 -t 6 -w 5 -e robots=off "https://website.com"

Dither image while still looking nice
ImageMagick

mogrify -path dithered/ -resize 12% -colors 16 -dither FloydSteinberg -modulate 100,200 *

Grab window (with name including "video2") and put into output.mp4
ffmpeg

echo "ffmpeg -y -v error -f x11grab -video_size $(wmctrl -lG | awk '$8 ~"video2" {printf("%dx%d -i :0.0+%d,%d", $5, $6, $3, $4)}') output.mp4" | bash

In theory, grab video in, pass through complex filter (in this case just add text), and out to virtual webcam
ffmpeg

ffmpeg -i /dev/video2 -vf "drawtext=text='efelbar':fontcolor=white:fontsize=64:box=1:boxcolor=black:boxborderw=5:x=0:y=0" -f v4l2 /dev/video4

Turn the background of an image transparent
ImageMagick

convert tower.jpg -alpha off -fuzz 10% -fill none -draw "alpha 0,0 floodfill" ( +clone -alpha extract -blur 0x2 -level 50x100% \) -alpha off -compose copy_opacity -composite tower.jpg

Find combined file size of specific filetype (in this case html)
Bash

du -h ./*.html | awk '{print $1}' | sed -e 's/K//' | paste -sd+ - | bc

Set i3 desktop background to color
Bash

xsetroot -solid "#f9cca4"

Delete files matching a pattern
Bash

ls | grep -P "^\d{2}\." | xargs -d"\n" rm

Restarts v4l2loopback
Bash

sudo modprobe -r v4l2loopback && sudo modprobe v4l2loopback

Naively remove consecutive duplicate lines in a file
Awk

!p[$0]++

Remove consecutive duplicate lines in a file, preserving empty lines
Awk

BEGIN{ old = "" } { new = $0 } old == new && old != "" { next } { old = $0; print }

Incoming: now wiki