dotfiles/.local/bin/scripts/mp

53 lines
2.5 KiB
Plaintext
Raw Normal View History

2022-05-03 09:29:48 +00:00
#!/bin/sh
2022-03-29 11:51:18 +00:00
# [M]edia [P]lay
2023-05-25 20:31:44 +00:00
# Add matchings songs to mpd queue, playing external files through symlinking and recursively resolving playlists
# depends: xargs realpath mpc
2021-10-14 18:26:08 +00:00
# env: MUSIC
2022-06-13 13:47:47 +00:00
# TODO auto-convert unknown types with ffmpeg to flac rather than linking (wav, opus, ...)
2022-12-13 12:25:04 +00:00
MPD_CONF=${XDG_CONFIG_HOME:-$HOME/.config}/mpd/mpd.conf
MUSIC="${MUSIC:-$(cat $MPD_CONF | grep music_directory | cut -d'"' -f2 | sed "s|~|$HOME|")}"
2022-12-22 14:44:02 +00:00
PLAYLISTS="$(cat $MPD_CONF | grep playlist_directory | cut -d'"' -f2 | sed "s|~|$HOME|" || echo "$MUSIC/Playlists")"
2021-10-16 08:46:54 +00:00
LINKS="$MUSIC/links"
if test "$1" = "-r"
then shift
test "$1" = "-v" && verbose=-v && set -x && shift
# TODO this is sooo slow...
for arg
2022-05-03 09:29:48 +00:00
do
2022-12-22 14:44:02 +00:00
matchname="$(basename -- "$arg" | sed 's|\(\[.*\)\]|\\\1\\]|;s|\?|\\?|')"
2022-06-13 13:47:47 +00:00
filepath="$({ find "$(dirname -- "$arg")" -maxdepth 1 -name "$matchname" -exec realpath {} + ||
2022-12-22 14:44:02 +00:00
find "$(dirname -- "$arg")" "$MUSIC/$(dirname -- "$arg")" "$MUSIC/Playlists/$(dirname -- "$arg")" -maxdepth 1 \
-name "$matchname*" -exec realpath {} +; } 2>/dev/null)"
test -n "$verbose" && echo "Scanning path '$filepath' $(
test "$arg" != "$filepath" && echo "from '$arg' ")(MUSIC: '$MUSIC', PWD: '$PWD')" >&2
test $(printf "$filepath" | wc -l) -gt 0 && printf "$filepath" | xargs --delim='\n' "$0" -r $verbose && continue
test -n "$filepath" || continue
2022-01-31 13:39:25 +00:00
if { file -- "$filepath" | grep -i ' playlist' || expr "$filepath" : ".*\.m3u8\?$"; } >/dev/null
then pushd "$(dirname "$filepath")" >/dev/null &&
cat "$filepath" | sed '/#.*/D' | xargs --delim='\n' "$0" -r $verbose &&
2022-01-30 11:33:19 +00:00
popd >/dev/null
else
2022-01-30 11:33:19 +00:00
test -n "$verbose" &&
2022-01-31 13:39:25 +00:00
echo "Running find in path '$filepath' $(test "$arg" != "$filepath" && echo "from '$arg' ")" >&2
2022-03-29 11:51:18 +00:00
find "$filepath" -iname "*.aac" -o -iname "*.flac" -o -iname "*.mp3" -o -iname "*.ogg" -o -iname "*.opus" |
sort | while read file
2022-01-31 13:39:25 +00:00
do case "$file" in
($MUSIC/*) echo "${file#$MUSIC/}";;
2022-01-31 13:39:25 +00:00
(*) test -n "$verbose" && echo "Linking $file into $LINKS" >&2
mkdir -p "$LINKS" &&
2022-01-30 11:33:19 +00:00
ln -fs "$file" "$LINKS/$(basename "$file")" &&
echo "${LINKS#$MUSIC/}/$(basename "$file")";;
esac done
fi
done
else
test "$1" = "-q" && quiet=-q && shift
2022-01-31 09:36:25 +00:00
test -n "$(mpc playlist)" -a "$(mpc status | wc -l)" -gt 1 && next=-q
2022-01-31 13:39:25 +00:00
test "$1" = "--seek" && seek=-q && shift
( "$0" -r "$@" && mpc -q update --wait ) | #tee /tmp/mp 2>&1
xargs --delim='\n' mpc insert
mpc ${quiet:-${next:-$seek}} play
test -z "$next" || mpc ${quiet:-$seek} next
test -z "$seek" || mpc $quiet seek "30%"
2021-10-16 08:46:54 +00:00
fi