omoidasu

a flashcard review system built on oboeru
git clone anongit@rnpnr.xyz:omoidasu.git
Log | Files | Refs | Feed | README | LICENSE

amc (2230B)


      1 #!/bin/sh
      2 
      3 # add media to most recent card
      4 
      5 AUD_BIT="64k"
      6 AUD_FMT="opus"
      7 IMG_FMT="webp"
      8 IMG_QUA="80"
      9 
     10 . ./config
     11 
     12 img=1
     13 aud=1
     14 deck=""
     15 cardid=0
     16 
     17 usage() {
     18 	echo \
     19 "$(basename $0) [-i | -a] [-c id] name.deck
     20 	-i: img only
     21 	-a: audio only
     22 	-c: specify card id"
     23 
     24 	exit 1
     25 }
     26 
     27 die() {
     28 	notify "$@"
     29 	exit 1
     30 }
     31 
     32 getid() {
     33 	awk -F $DELIM '
     34 	BEGIN { max = 0 }
     35 	{
     36 		w = length($1);
     37 		if ($0 > max) {
     38 			max = $0
     39 		}
     40 	}
     41 	END {printf "%0"w"i\n", max}' "$1"
     42 }
     43 
     44 getpath() {
     45 	local path=$(dirname "$deck")
     46 	[ $cardid -eq 0 ] && cardid=$(getid $deck)
     47 	path="$path/$(basename $deck | cut -d '.' -f 1)"
     48 	printf "%s/%s" $path $cardid
     49 }
     50 
     51 geturl() {
     52 	local output=$(mktemp -p /tmp "amc.XXXXXX")
     53 	output=$(mv "$output" "$output".$AUD_FMT && printf "$output".$AUD_FMT)
     54 
     55 	ffmpeg -y \
     56 		-loglevel error \
     57 		-i "$1" \
     58 		-ab "$AUD_BIT" \
     59 		-ac 2 \
     60 		-af 'silenceremove=1:0:-50dB' \
     61 		"$output" 1>/dev/null \
     62 	|| die "Failed to fetch audio"
     63 
     64 	printf "$output"
     65 }
     66 
     67 record() {
     68 	local output=$(mktemp -p /tmp "amc.XXXXXX")
     69 	output=$(mv "$output" "$output".$AUD_FMT && printf "$output".$AUD_FMT)
     70 
     71 	ffmpeg -y \
     72 		-loglevel error \
     73 		-f alsa \
     74 		-i "hw:Loopback,1" \
     75 		-ab "$AUD_BIT" \
     76 		-ac 2 \
     77 		-af 'silenceremove=1:0:-50dB' \
     78 		"$output" 1>/dev/null &
     79 
     80 	local pid=$(echo $!)
     81 	herbe "Recording in Progress" "Close to finish"
     82 	kill -15 $pid
     83 
     84 	printf "$output"
     85 }
     86 
     87 audio() {
     88 	local file
     89 	local out
     90 	local path=$(getpath)
     91 	local url
     92 
     93 	case $(printf "url\nrecord" | dmenu -i -p "Audio:") in
     94 	record) file=$(record) ;;
     95 
     96 	url)	herbe "Copy url to Clipboard" "Close to Continue"
     97 		file=$(geturl $(xclip -o -s clip))
     98 		;;
     99 	*)
    100 		return
    101 		;;
    102 	esac
    103 
    104 	if [ -f "$path"/audio.* ]; then
    105 		out="$path"/audio2.$AUD_FMT
    106 	else
    107 		out="$path"/audio.$AUD_FMT
    108 	fi
    109 	mv "$file" "$out" \
    110 	&& herbe "Wrote audio:" "$out"
    111 }
    112 
    113 img() {
    114 	local file="$(getpath)/img.$IMG_FMT"
    115 
    116 	notify "Image Selction"
    117 	import -window root -quality $IMG_QUA -crop $(slop -f '%g') $IMG_FMT:- \
    118 	>| "$file"
    119 	pkill herbe
    120 	herbe "Wrote image:" "$file"
    121 }
    122 
    123 while getopts "iac:" arg; do
    124 	case "${arg}" in
    125 	i) aud=0 ;;
    126 	a) img=0 ;;
    127 	c) cardid=${OPTARG} ;;
    128 	*) usage ;;
    129 	esac
    130 done
    131 shift $((OPTIND - 1))
    132 
    133 [ $img -eq 0 ] && [ $aud -eq 0 ] && usage
    134 
    135 deck=$1
    136 [ -f "$deck" ] || usage
    137 
    138 [ $aud -eq 1 ] && audio
    139 [ $img -eq 1 ] && img