Skip to content

Instantly share code, notes, and snippets.

@robalni
Created July 11, 2017 21:57
Show Gist options
  • Select an option

  • Save robalni/33dbeaf38d5e7a34589ddf756a958c15 to your computer and use it in GitHub Desktop.

Select an option

Save robalni/33dbeaf38d5e7a34589ddf756a958c15 to your computer and use it in GitHub Desktop.
Plays music randomly with some random pauses
#!/bin/bash
rand()
{
low=0
high=0
if [[ $# -eq 1 ]]; then
high=$1
elif [[ $# -eq 2 ]]; then
low=$1
high=$2
fi
echo $(( RANDOM % (high - low + 1) + low ))
}
s_to_hms()
{
if [[ $# -ne 1 ]]; then
echo "s_to_hms: I need 1 argument: seconds" 1>&2
return 1
fi
diff_s=$1
h=$((diff_s/3600))
diff_s=$((diff_s - diff_s/3600*3600))
m=$(tail -c 3 <<< 0$((diff_s/60)))
diff_s=$((diff_s - diff_s/60*60))
s=$(tail -c 3 <<< 0$diff_s)
echo "$h:$m:$s"
}
if [[ $# -eq 0 ]]; then
echo "I want one or more arguments: filename(s)" 1>&2
exit
fi
mpv_args=''
if [[ -n "$volume" ]]; then
mpv_args="$mpv_args --volume=$volume"
fi
space_low=0$(echo $space | grep -o '^[0-9]*')
space_high=0$(echo $space | grep -o '[0-9]*$')
pause_low=0$(echo $pause | grep -o '^[0-9]*')
pause_high=0$(echo $pause | grep -o '[0-9]*$')
duration=0$duration
if [[ $space_high -eq 0 ]]; then
space_low=1
space_high=1
fi
if [[ $pause_high -eq 0 ]]; then
pause_low=60
pause_high=120
fi
if [[ $duration -eq 0 ]]; then
duration=$((60*15))
fi
files=''
while [[ $# -gt 0 ]]; do
files="$files"$(find -L "$1" -type f)$'\n'
shift
done
last_pause=$(date +%s)
while true; do
random_file=$(echo -n "$files" | sort -R | head -n1)
mpv $mpv_args "$random_file"
played_time=$(($(date +%s)-last_pause))
if [[ $played_time -gt $duration ]]; then
echo "That was $(s_to_hms $played_time) played" 1>&2
delay=$(rand $pause_low $pause_high)
echo "Pausing $(s_to_hms $delay)" 1>&2
sleep $delay
last_pause=$(date +%s)
else
delay=$(rand $space_low $space_high)
sleep $delay
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment