Skip to content

Instantly share code, notes, and snippets.

@knugie
knugie / keybase.md
Last active May 25, 2022 19:46
Keybase proof

Keybase proof

I hereby claim:

  • I am knugie on github.
  • I am knugie (https://keybase.io/knugie) on keybase.
  • I have a public key whose fingerprint is 4F54 2255 6028 FAFB 7458 E545 4276 8876 DFB2 3E2B

To claim this, I am signing this object:

@knugie
knugie / mp42gif.sh
Last active May 25, 2022 19:35
Generate a gif from an mp4 file
# Using ffmpeg
ffmpeg -y -i video.mp4 -vf fps=1,scale=600:-1:flags=lanczos,palettegen palette.png
ffmpeg -i video.mp4 -i palette.png -filter_complex "setpts=0.125*PTS,fps=5,scale=600:-1:flags=lanczos[x];[x][1:v]paletteuse" video.gif
# OR
# Using ffmpeg and gifsicle (https://www.lcdf.org/gifsicle/)
ffmpeg -i video.mp4 -s 600x400 -r 3 -f gif - | gifsicle --delay=3 > video.gif
@knugie
knugie / mov2mp4.sh
Last active October 6, 2022 14:22
convert .mov to .mp4 using ffmpeg
ffmpeg -i movie.mov -vcodec copy -acodec copy out.mp4
#!/usr/bin/env ruby
###################################################
## Display screen dimensions in macOS using ruby ##
##################################################
# found https://developer.apple.com/reference/coregraphics/1456395-cgdisplaybounds?language=objc
# --> use CoreGraphics framework
# --> CGRect CGDisplayBounds(CGDirectDisplayID display);
#############################################
@knugie
knugie / vector_cluster.rb
Last active June 29, 2016 19:05
Cluster vectors by their similarity defined by a tolerance vector
require 'matrix'
tolerance = Vector[5, 5, 5]
elements = [Vector[2, 1, 4], Vector[20, 100, 25], Vector[21, 98, 21], Vector[1, 2, 3]]
clusters = []
while(elements.any?)
element = elements.pop
cluster, elements = elements.partition do |elem|
diff = tolerance - (elem - element).map(&:abs)
@knugie
knugie / jpg_to_2048.sh
Last active December 4, 2023 06:20
resize and optimize JPEG images to box-fit 2048x2048 pixels with 80% JPEG quality
#! /usr/bin/env bash
# brew install imagemagick
# brew install jpegoptim
mkdir 2048
for image in *.[jJ][pP][eE]?[gG]; do
convert $image -resize 2048x2048\> ./2048/$image
jpegoptim --strip-all -q --max=80 ./2048/$image
done
@knugie
knugie / poc_meter.rb
Created June 24, 2016 10:51
POC type checking when calculating with unit-based values
class Meter
attr_reader :value, :unit
def initialize(value, unit = 'm')
@value = value
@unit = unit
end
def self.[](value)
self.new(value)
@knugie
knugie / keyboard_layout_switcher
Created June 20, 2016 20:14
Keyboard layout switcher - Xcode project (objective-c, command line tool)
//
// main.m
// keyboard_layout_switcher
//
@import Carbon;
int main(int argc, const char * argv[]) {
@autoreleasepool {
if (argc <= 1){
@knugie
knugie / cluster.rb
Created March 25, 2016 18:38
Cluster numbers by their distance to each other
values = 20.times.map{rand(100)+200}
delta = 10
min, max = values.minmax
offset = min - delta / 2
grouped = values.group_by { |value| (value - offset) / delta }
Hash[grouped.sort]
=begin
@knugie
knugie / count_unique_elements_in_array.rb
Last active May 25, 2022 19:47
Count unique elements in Array
require 'benchmark'
require 'open3'
require 'histogram/array'
array = Array.new(10_000_000) { rand(60) }
puts 'uniq, count:'
uniq_count = Benchmark.measure do
array.uniq.map { |key| array.count(key) }
end