Compare commits

...

5 Commits

Author SHA1 Message Date
yosh 1648d3d20b n: cleanup 2023-09-21 10:06:30 -04:00
yosh 73837d24cc nsxiv-rifle: delete, using upstream now 2023-09-21 10:06:30 -04:00
yosh 71bcf3ed5b unityextract: use posix find 2023-09-21 10:06:30 -04:00
yosh 097ba6851f twitch-notify: fix incorrect syntax 2023-09-21 10:06:30 -04:00
yosh 0ef45f29ad albumsetup: just a fuckton of polish 2023-09-21 10:06:30 -04:00
5 changed files with 84 additions and 113 deletions

View File

@ -1,21 +1,19 @@
#!/bin/sh
set -euf
set -eu
EXT="flac" # default
NL="
"
IFS="$NL"
errecho() { echo "$*" >&2 ; }
die() { errecho "$*" && exit 1 ; }
clean() {
trap 'exit' INT HUP QUIT
trap 'exit' INT HUP QUIT EXIT
[ -f "${DEFAULT_PICTURE:-}" ] && rm -f "$DEFAULT_PICTURE"
[ -f "${COVER_PICTURE:-}" ] && rm -f "$COVER_PICTURE"
[ -f "${JSON:-}" ] && rm -f "$JSON"
}
# wrapper to grab a specific tag from flac/opus/mp3
grabtag() {
grab_tag() {
case ${1##*.} in
flac) metaflac --show-tag="$2" "$1" | cut -d '=' -f 2- ;;
opus) opusinfo "$1" | awk -v RS='\n' -v FS='=' -v OFS='' "/^\\t$2=/"'{$1=""; print $0}' ;;
@ -35,20 +33,19 @@ grabtag() {
# grab track info, make tracknumber 3-padded just in case for long albums
# (no album has > 999 tracks. I think.)
grabinfo() {
grab_info() {
set +e
DURATION="$(ffprobe -v quiet -of csv=p=0 -show_entries format=duration "$1")"
ARTIST="$(grabtag "$1" ARTIST)"
ALBUM="$(grabtag "$1" ALBUM)"
TITLE="$(grabtag "$1" TITLE)"
TRACKNUMBER="$(printf '%03g' "$(grabtag "$1" TRACKNUMBER)")"
ARTIST="$(grab_tag "$1" ARTIST)"
ALBUM="$(grab_tag "$1" ALBUM)"
TITLE="$(grab_tag "$1" TITLE)"
TRACKNUMBER="$(printf '%03g' "$(grab_tag "$1" TRACKNUMBER)")"
set -e
}
ffconv() {
IFS=" "
ffmpeg ${VERBOSE--loglevel error} -y -loop 1 -framerate 4 -f image2 -i "$1" ${FULLALBUM:+-safe 0 -f concat} -i "$2" \
-t "${TOTALTIME:-$DURATION}" \
convert_vid() {
ffmpeg ${VERBOSE--loglevel error} -y -loop 1 -framerate 4 -f image2 -i "$1" -i "$2" \
-t "$DURATION" \
-pix_fmt yuv420p \
${acopy:--c:a libopus -b:a 256k} \
-r 4 \
@ -60,6 +57,8 @@ ffconv() {
errecho "Successfully converted $2"
}
# extracts cover from $1 to $2
# returns 0 if $2 (output) exists, 1 if not
extract_cover() {
set +e
{
@ -70,21 +69,37 @@ extract_cover() {
*) die 'unknown filetype' ;;
esac
} >/dev/null
[ -f "$2" ] && magick mogrify -resize 1280x720 -background black -gravity center -extent 722x720 "$2"
set -e
# resize within the bounds of 1280x720
# if it's square then make it 722x720 so youtube doesn't make it a short
[ -f "$2" ] && magick mogrify -resize 1280x720 -background black -gravity center -extent 722x720 "$2"
}
# GET OPTIONS #
# sets COVER_PIC for use in convert_vid
set_cover() {
tmp="$(mktemp -u -t ALBUMSETUP_COVER_XXXX)"
extract_cover "$1" "$tmp"
if [ -f "$tmp" ]; then
COVER_PICTURE="$tmp"
elif [ -f "track$TRACKNUMBER.png" ]; then
COVER_PICTURE="track$TRACKNUMBER.png"
else
COVER_PICTURE="$DEFAULT_PICTURE"
fi
}
# SCRIPT START #
trap 'clean' INT HUP QUIT EXIT
SONG="" FULLALBUM="" BANDCAMP="" NO_CONVERT=""
OUTDIR="/tmp/albumsetup/${PWD##*/}"
while getopts :ovnd:e:p:s:b: OPT; do
while getopts :vnd:e:p:s:b: OPT; do
case "$OPT" in
o) FULLALBUM=1 ;;
v) VERBOSE="" ;; # can use ${var-rep} for this
d) cd "$OPTARG" && OUTDIR="${OUTDIR%/*}/${PWD##*/}" ;;
e) EXT="$OPTARG" ;;
p) tmpimg="$OPTARG" ;;
p) tmpcover="$OPTARG" ;;
s) SONG="$OPTARG" && OUTDIR="${OUTDIR%/*}" ;; # individual song
b) BANDCAMP="$OPTARG" ;; # bandcamp link to extract data
n) NO_CONVERT=1 ;; # don't convert, just get metadata.txt (only affects default albumsetup)
@ -93,69 +108,69 @@ while getopts :ovnd:e:p:s:b: OPT; do
done
shift "$((OPTIND - 1))"
[ ! -f "$tmpimg" ] && die "Default image not specified!"
[ ! -f "$tmpcover" ] && die "Default cover not specified! (-p IMG)"
mkdir -p "$OUTDIR"
# convert cover image. it has to be wider than 1:1 so youtube doesn't convert it to a short
# if image is wider than 1:1, just resize height. if not, pad with black
# default cover image, fallback if tracks don't have embedded art
DEFAULT_PICTURE="$(mktemp -u ALBUMSETUP_DEFAULT_PIC.XXXX)"
magick convert "$tmpimg" -resize 1280x720 -background black -gravity center -extent 722x720 "$DEFAULT_PICTURE"
magick convert "$tmpcover" -resize 1280x720 -background black -gravity center -extent 722x720 "$DEFAULT_PICTURE"
trap 'clean' INT HUP QUIT
[ "$EXT" = "opus" ] && acopy="-c:a copy" # copy audio codec if opus since output codec is opus
# copy audio codec if opus since output codec is opus
[ "$EXT" = "opus" ] && acopy="-c:a copy"
# INDIVIDUAL SONG #
if [ -n "$SONG" ]; then
grabinfo "$SONG"
ffconv "$DEFAULT_PICTURE" "$SONG" "$OUTDIR/SONG ${ARTIST%%/*} - ${TITLE%%/*}.webm" # substitution mods are to not create directories
grab_info "$SONG"
set_cover "$SONG"
# CONTINUOUS VIDEO OF WHOLE ALBUM #
elif [ -n "$FULLALBUM" ]; then
TOTALTIME=0 # keeping track of timestamps
for f in $(fd -d 1 -e "$EXT"); do
grabinfo "$f"
# lots of %%/* as to not accidentally create directories
convert_vid "$COVER_PICTURE" "$SONG" "$OUTDIR/SONG ${ARTIST%%/*} - ${TITLE%%/*}.webm"
# make timestamp
printf '%02d:%02d:%02d - %s\n' \
"$((${TOTALTIME%%.*} / 3600))" "$((${TOTALTIME%%.*} % 3600 / 60))" "$((${TOTALTIME%%.*} % 60))" \
"$ARTIST - $TITLE" >> "$OUTDIR/metadata.txt"
# add to total time, but this is as a float remember that
TOTALTIME="$(printf '%s' "$TOTALTIME + $DURATION" | bc)"
# build ffmpeg concat metadata
sf="$(printf '%s' "$f" | sed "s/'/'\\\\''/g")" # make safe filename for ffmpeg concat; replace all ' with '\''
echo "file '${PWD}/$sf'" >> "$OUTDIR/ffmpeg_tracklist.txt"
done
[ ! -f "$OUTDIR/ffmpeg_tracklist.txt" ] && die "No files found!"
ffconv "$DEFAULT_PICTURE" "$OUTDIR/tracklist.txt" "$OUTDIR/$ARTIST - $ALBUM.webm"
rm "$OUTDIR/ffmpeg_tracklist.txt"
# INDIVIDUAL TRACKS FOR FULL ALBUM (default) #
# INDIVIDUAL TRACKS FOR FULL ALBUM + COMPLETE VID #
else
for f in $(fd -d 1 -e "$EXT"); do
grabinfo "$f"
total_time=0 # this var helps for more precise timestamps
for f in ./*."$EXT"; do
grab_info "$f"
set_cover "$f"
output_file="$OUTDIR/$TRACKNUMBER ${ARTIST%%/*} - ${TITLE%%/*}.webm"
# metadata print
# STUFF FOR INDIVIDUAL VID #
errecho "Converting $f"
[ -z "$NO_CONVERT" ] && convert_vid "$COVER_PICTURE" "$f" "$output_file"
rm -f "$COVER_PICTURE"
# metadata print to send to metadata.txt
printf '%s %s - %s - %s\n' "${TRACKNUMBER:-000}" "${ARTIST:-unknown artist}" "${ALBUM:-unknown album}" "${TITLE:-unknown title}"
# try making auto pic
PICTURE="$(mktemp -u -t ALBUMSETUP_COVER_XXXX)"
extract_cover "$f" "$PICTURE"
[ ! -f "$PICTURE" ] && PICTURE="$(fd -e jpg -e png "^track$TRACKNUMBER")"
[ ! -f "$PICTURE" ] && PICTURE="$DEFAULT_PICTURE"
# STUFF FOR FULL VID #
# metadata for full vid
printf '%02d:%02d:%02d - %s\n' \
"$((${total_time%%.*} / 3600))" "$((${total_time%%.*} % 3600 / 60))" "$((${total_time%%.*} % 60))" \
"$ARTIST - $TITLE" >> "$OUTDIR/metadata_fullvid.txt"
# add to total time, but this is as a float remember that
total_time="$(printf '%s\n' "$total_time + $DURATION" | bc)"
errecho "Converting $f"
[ -z "$NO_CONVERT" ] && ffconv "$PICTURE" "$f" "$OUTDIR/$TRACKNUMBER ${ARTIST%%/*} - ${TITLE%%/*}.webm"
rm -f "$PICTURE"
# build ffmpeg concat metadata
# make safe filename for ffmpeg concat; replace all ' with '\''
sf="$(printf '%s' "$output_file" | sed "s/'/'\\\\''/g")"
echo "file '$sf'" >> "$OUTDIR/ffmpeg_tracklist.txt"
done > "$OUTDIR/metadata.txt"
[ ! -f "$OUTDIR/metadata.txt" ] && die "No files found!"
# sort by correct track numbers then remove track numbers later
sort -o "$OUTDIR/metadata.txt.sorted" "$OUTDIR/metadata.txt"
while read -r line; do
printf '%s' "$line" | cut -d ' ' -f 2-
while read -r track name; do
printf '%s\n' "$name"
done <"$OUTDIR/metadata.txt.sorted" >"$OUTDIR/metadata.txt"
rm "$OUTDIR/metadata.txt.sorted"
if [ -z "$NO_CONVERT" ]; then
errecho "Building full video..."
ffmpeg -safe 0 -f concat -i "$OUTDIR/ffmpeg_tracklist.txt" -c copy "$OUTDIR/$ARTIST - $ALBUM.webm"
fi
rm "$OUTDIR/ffmpeg_tracklist.txt"
# Bandcamp check and info retrieval
if [ -n "$BANDCAMP" ]; then

7
n
View File

@ -2,21 +2,16 @@
# nnn previewer wrapper
set -euf
if [ "${NNNLVL:-0}" -ge 1 ]; then
echo "don't nest nnn!" && exit
fi
[ -n "${TMUX:-}" ] && exec nnn
vars="NNN_BATTHEME=Dracula
NNN_BATSTYLE=full
NNN_TERMINAL=urxvt
"
IFS='
'
varstring=""
for env in $vars; do
export "${env?}"
export "$env"
varstring="$varstring -e $env"
done; unset IFS

View File

@ -1,41 +0,0 @@
#!/bin/sh
TMPDIR="${XDG_RUNTIME_DIR}"
tmp="$TMPDIR/nsxiv_rifle_$$"
is_img_extension() {
rg -i '\.(jpe?g|png|gif|svg|webp|tiff|heif|avif|ico|bmp|jxl)$'
}
listfiles() {
fd . -L -d 1 -t f "$1" |
is_img_extension | sort | tee "$tmp"
}
open_img() {
file="$1"; shift;
# only go through listfiles() if the file has a valid img extension
if echo "$file" | is_img_extension >/dev/null 2>&1; then
trap 'rm -f "$tmp"' EXIT
count="$(listfiles "${file%/*}" | rg -nF "$file")"
fi
if [ -n "$count" ]; then
nsxiv -i -n "${count%%:*}" "$@" -- < "$tmp"
else
# fallback incase file didn't have a valid extension, or we couldn't
# find it inside the list
nsxiv "$@" -- "$file"
fi
}
[ "$1" = '--' ] && shift
case "$1" in
"") echo "Usage: ${0##*/} PICTURES" >&2; exit 1 ;;
/*) open_img "$1" ;;
"~"/*) open_img "$HOME/${1#"~"/}" ;;
trash:///*)
trash_dir="${XDG_DATA_HOME:-$HOME/.local/share}/Trash/files"
open_img "${trash_dir}$(uri2path "$1")" -N "nsxiv_trash"
;;
*) open_img "$PWD/$1" ;;
esac

View File

@ -28,12 +28,12 @@ check() {
tmphtml="$2"
max_tries=2
trap 'innerclean "$tmphtml"' INT HUP TERM EXIT
curl -s -o "$2" -L "https://twitch.tv/$1"
curl --connect-timeout 10 -s -o "$2" -L "https://twitch.tv/$1"
if [ -n "$(pup 'script[type="application/ld+json"]' < "$2")" ]; then
if [ ! -f "$DIR/$1" ]; then
touch "$DIR/$1"
IMG="$(mktemp -t "twitch-notify-img.XXXX")"
curl -s -o "$IMG" -L "$(pup 'meta[property="og:image"]' 'attr{content}' < "$2")"
curl --connect-timeout 10 -s -o "$IMG" -L "$(pup 'meta[property="og:image"]' 'attr{content}' < "$2")"
TITLE="$(pup 'script[type="application/ld+json"] text{}' < "$2" | jq -r '.[0].description')"
CODE="$(notify-send -A "watch" -A "copy" -t 10000 -u normal -i "$IMG" "$1 is online!$NL$NL\"$TITLE\"")"
case "${CODE:-999}" in

View File

@ -1,5 +1,7 @@
#!/bin/sh
set -euf
tar xf "$1"
fd '^asset$' -x sh -c 'SRC="$1"; DESTFILE="$(cat "${SRC%/*}/pathname")"; mkdir -p "UNITYEXTRACT/${DESTFILE%/*}"; mv "$SRC" "UNITYEXTRACT/$DESTFILE"' sh
tmp="$(mktemp -d)"
tar -x -C "$tmp" -f "$1"
find "$tmp" -name 'asset' -exec sh -c 'SRC="$1"; DESTFILE="$(cat "${SRC%/*}/pathname")"; mkdir -p "UNITYEXTRACT/${DESTFILE%/*}"; mv "$SRC" "UNITYEXTRACT/$DESTFILE"' sh {} \;
rm -rf "$tmp"