Skip to content

Instantly share code, notes, and snippets.

@toriato
Last active August 9, 2022 16:12
Show Gist options
  • Save toriato/f5b77c81e1e94a019664a4bd286afcc7 to your computer and use it in GitHub Desktop.
Save toriato/f5b77c81e1e94a019664a4bd286afcc7 to your computer and use it in GitHub Desktop.
#!/bin/bash
shopt -s expand_aliases
UPLOAD_ENTRYPOINT="https://vmm.pw"
UPLOAD_TOKEN=""
UPLOAD_DISCORD_WEBHOOK=""
fpath="$1"
fname="$(basename $fpath)"
fdir="$(dirname $fpath)"
fmime="$(file --mime-type "$fpath" | cut -d: -f2 | xargs)"
opts=(
'@'
'@discord'
)
if [[ $fmime =~ ^image/ ]]; then
opts+=( 'to WebP' )
[[ $fmime =~ (a?png|gif)$ ]] \
&& opts+=( 'to WebM' ) \
|| opts+=()
fi
if [[ $fmime =~ ^video/ ]]; then
opts+=(
'to MP4'
'to WebP'
'to WebM'
'to GIF'
)
fi
alias tcs="termux-clipboard-set"
alias td="termux-dialog"
alias jqr="jq --raw-output"
function join {
echo "$(IFS=,; echo "$*")"
}
function convert {
pv "$fpath" | ffmpeg -y -v warning -i pipe:0 ${cv_opts[@]} "$HOME/storage/downloads/$fname.$cv_ext" || exit $?
termux-media-scan "$HOME/storage/downloads"
}
# convert bash array to string
opts="$(join "${opts[@]}")"
case "$(td sheet -t 'job' -v "$opts"| jqr .text)" in
"@")
tcs $(curl -sSL \
-F "token=$UPLOAD_TOKEN" \
-F "file=@$1" \
$UPLOAD_ENTRYPOINT)
;;
"@discord")
response="$(curl -sSL \
-X POST \
-F "payload_json=$(jo username="$(whoami)" content="**$(basename $1)**")" \
-F "file1=@$fpath" \
$UPLOAD_DISCORD_WEBHOOK)"
# TODO: error handling
tcs "$(echo $response | jqr .attachments[0].url)"
;;
"to WebP")
cv_ext=webp
convert
;;
"to WebM")
cv_ext=webm
cv_opts=()
# choose video codec
codec="$(td sheet -t 'video codec' -v 'libaom-av1,libvpx,libvpx-vp9' | jqr .text)"
cv_opts+=("-c:v $codec")
case $codec in
"libaom-av1")
cv_opts+=(
"-crf $(td sheet -t 'crf' -v "$(join $(seq 0 63))" | jqr .text)"
"-cpu-used $(nproc)"
'-row-mt 1'
)
;;
*)
cv_opts+=(
"-crf $(td sheet -t 'crf' -v "$(join $(seq 0 63))" | jqr .text)"
"-cpu-used $(nproc)"
'-deadline realtime'
)
esac
# TODO: choose audio codec?
cv_opts+=('-c:a libopus')
convert
;;
"to MP4")
cv_ext=mp4
convert
;;
"to GIF")
cv_ext="gif"
convert
;;
*)
exit 1
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment