92 lines
1.8 KiB
Bash
92 lines
1.8 KiB
Bash
#!/usr/bin/env bash
|
|
|
|
PLAYLIST="$1"
|
|
COUNT="$2"
|
|
|
|
set -e
|
|
|
|
PLAYLIST_DIR="$HOME/Music/Playlists"
|
|
|
|
function rand_num() {
|
|
cat /dev/urandom | tr -dc '0-9' | head -c 10 | sed -e 's/^0*//'
|
|
}
|
|
|
|
function pick_song() {
|
|
playlist="$1"
|
|
|
|
playlist_path="$PLAYLIST_DIR/$playlist.pls"
|
|
if [[ ! -f "$playlist_path" ]]
|
|
then
|
|
echo "error: playlist $playlist not found"
|
|
exit 1
|
|
fi
|
|
|
|
songs="$(cat "$playlist_path" | grep '^file' | sed -e 's|^[^=]*=\.\./||')"
|
|
|
|
num=$(( 1 + $(rand_num) % $(echo "$songs" | wc -l) ))
|
|
song="$(echo "$songs" | sed "${num}q;d")"
|
|
echo "$(dirname "$PLAYLIST_DIR")/$song"
|
|
}
|
|
|
|
function get_flags() {
|
|
path="$1"
|
|
meuadata="flac"
|
|
genre="$(ffmpeg -i "${path}" 2>&1 | grep GENRE | sed -e 's/^.*: //')"
|
|
if [[ "$(echo "${path}" | sed -e 's/^.*\.\([^\.]*\)$/\1/')" == "mp3" ]]
|
|
then
|
|
metadata="mp3"
|
|
genre="$(id3v2 -l "${path}" | grep '^TCON' | sed -e 's/^.*: //')"
|
|
fi
|
|
echo $genre
|
|
}
|
|
|
|
function get_like_level() {
|
|
flags="$1"
|
|
|
|
level="$(echo "$flags" | sed -e 's/^.*L\([0-3]\).*$/\1/')"
|
|
if [[ "$level" == "$(echo "$level" | tr -dc '0-3')" ]]
|
|
then
|
|
echo $level
|
|
else
|
|
echo 2 # default like level
|
|
fi
|
|
}
|
|
|
|
function shuffle() {
|
|
playlist="$1"
|
|
|
|
song="$(pick_song "$playlist")"
|
|
flags="$(get_flags "$song")"
|
|
like=$(get_like_level "$flags")
|
|
like_min=$(( 1 + $(rand_num) % 3 ))
|
|
|
|
if [[ "$in_list" == "false" || "$like" -lt "$like_min" ]]
|
|
then
|
|
shuffle "$playlist"
|
|
else
|
|
mpd_song="$(echo "$song" | sed -e 's|.*/Music/||')"
|
|
echo "$mpd_song"
|
|
mpc add "$mpd_song"
|
|
fi
|
|
}
|
|
|
|
if [[ -z "$PLAYLIST" ]]
|
|
then
|
|
PLAYLIST=all
|
|
fi
|
|
|
|
# pick_song "$PLAYLIST"
|
|
|
|
if [[ -z "$COUNT" ]]
|
|
then
|
|
echo "add one song to que from $PLAYLIST"
|
|
echo "$(shuffle $PLAYLIST)"
|
|
else
|
|
echo "add $COUNT songs to que from $PLAYLIST"
|
|
for i in $(seq 1 $COUNT)
|
|
do
|
|
echo "$(shuffle $PLAYLIST)"
|
|
# echo $i
|
|
done
|
|
fi
|