Skip to content

Instantly share code, notes, and snippets.

@harish2704
Last active April 3, 2020 18:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harish2704/fe2ca40b7fecf37e84c489836aba2fa0 to your computer and use it in GitHub Desktop.
Save harish2704/fe2ca40b7fecf37e84c489836aba2fa0 to your computer and use it in GitHub Desktop.
My utils
#!/usr/bin/env bash
fonts_for_lang(){
fc-list :lang=$1
}
sshInTabs(){
logins="$@"
for i in $logins; do
gnome-terminal --tab -- bash -c "retry=y; while [ \"\$retry\" = 'y' ]; do ssh $i; echo \'Retry? y/n \(n\) ?\'; read retry; done;";
done
}
edit(){
vim ~/.local/Apps/daily-utils/bin/hari-tools.sh
}
genSound(){ echo $1 | espeak --stdout | ffmpeg -i - -ar 8000 -y $2.wav ; }
clearSysRq(){
sudo kbd_mode -s -C /dev/tty7
}
myIp(){
curl 'https://api.ipify.org?format=json'
}
sshConfigDs(){
lst=$(./tools/system_utils/digital_ocean_util.sh listFloatingIps | grep dialer | sed 's/"//g');
for line in $lst; do
cols=(${line//,/ })
f=${cols[0]};
h=${cols[2]};
g=${cols[3]};
p=${cols[4]};
cat<<EOF
Host ${h}.work
HostName $f
User root
Host prive.${h}.work
HostName $p
User root
Host glob.${h}.work
HostName $g
User root
EOF
done
}
block_dev_to_vmdk(){
[ -z "$2" ] && echo 'block_dev_to_vmdk <path/to/block_dev> <path/to/vmdk>' && exit 1
VBoxManage internalcommands createrawvmdk -filename $2 -rawdisk $1
}
sshsha256(){ awk '{print $2}' $1 | base64 -d | sha256sum -b | awk '{print $1}' | xxd -r -p | base64 ; }
githubdl(){
cd ~/Downloads
git clone $1
projName=$(basename $1)
projName=${projName%.*}
mksquashfs ./$projName $projName.sqfs
}
uniqsort(){
awk '!array[$0]++'
}
video_to_webm(){
src="$(readlink -f "$1")"
dest="$(readlink -f "$2")"
deadline=good
cpu_param='--cpu-used=16'
# cpu_param=''
# deadline=best
CODEC=${CODEC:-vp8}
framesCount=$(ffmpeg -i "$src" -vcodec copy -acodec copy -f null /dev/null 2>&1 | grep 'frame=' | sed 's/frame= *\([0-9]*\).*/\1/' )
echo "Frames count = $framesCount . If speed=10 fps, it will take $( python -S -c 'print("%2.2f" % ('$framesCount'/10.0/60))' ) minutes"
if [[ -z "$TWOPASS" ]]; then
ffmpeg -i "$src" -f yuv4mpegpipe - 2>/dev/null | vpxenc ${cpu_param} --codec=$CODEC -p 1 --${deadline} -o "$dest" /dev/stdin
else
pass1File=./video_to_webm_$(date +%s).log
ffmpeg -i "$src" -f yuv4mpegpipe - 2>/dev/null | vpxenc ${cpu_param} --codec=$CODEC -p 2 --${deadline} -o "$dest" --fpf="$pass1File" --pass=1 /dev/stdin
ffmpeg -i "$src" -f yuv4mpegpipe - 2>/dev/null | vpxenc ${cpu_param} --codec=$CODEC -p 2 --${deadline} -o "$dest" --fpf="$pass1File" --pass=2 /dev/stdin
rm $pass1File
fi
# tmpFile=$(date +%s).webm
# tmpdir=~/.cache/hari-utils/video_to_webm
# mkdir -p $tmpdir
# rm $tmpdir/*
# cd $tmpdir
# echo "Running first pass ..."
# ffmpeg -i "$src" -c:v libvpx-vp9 -pass 1 -b:v 1000K -threads 8 -speed 4 \
# -tile-columns 6 -frame-parallel 1 \
# -an -f webm /dev/null
#
# echo "Now Second pass ..."
# ffmpeg -i "$src" -c:v libvpx-vp9 -pass 2 -b:v 1000K -threads 8 -speed 1 \
# -tile-columns 6 -frame-parallel 1 -auto-alt-ref 1 -lag-in-frames 25 \
# -c:a libopus -b:a 64k -f webm "$tmpFile"
# mv "$tmpFile" "$dest"
}
record_desktop(){
ffmpeg -report -f x11grab -draw_mouse 1 -framerate 10 -video_size 1600x900 \
-i :0+0,0 -f alsa -ac 2 -i hw:0,0 -pix_fmt yuv420p -c:v mpeg4 -c:a libmp3lame \
-q:v 1 -s 1600x900 -f mp4 ~/Videos/myscreencast-$(date +%F_%T).mp4
}
list_all_commands(){
typeset -F | cut -d ' ' -f 3 | grep -v '^_'
}
bulk_replace(){
old=$1
new=$2
ag -l "$old" | xargs -l sed -i "s/${old}/${new}/g"
}
psql_csv_table(){
local db=$1
local src=$2
if [[ -z "$src" || -z "$db" ]]; then
echo "$0 <db> <csv file>";
exit
fi
local tbl=$(basename $src)
tbl="${tbl%.*}"
local cols=$( head -n1 $src )
cat<<EOF | psql $db
DROP TABLE IF EXISTS "$tbl";
CREATE TABLE "$tbl" (
$( echo $cols | sed -E -e 's/([^,]*)/"\1" text/g' )
);
EOF
cat "$src" | psql $db -c "COPY \"$tbl\"( $( echo $cols | sed -E -e 's/([^,]*)/"\1"/g' ) ) FROM STDIN CSV HEADER"
}
cmd=$1
shift
allFunctions=$(list_all_commands | tr '\n' ' ')
if [ -n "$cmd" ] && [[ " $allFunctions " =~ " $cmd " ]]; then
$cmd "$@"
else
cat<<EOF
Available commands:
$( list_all_commands | sed 's/^/\t/g' | sort )
EOF
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment