playshuf

Endlessly play music files from a directory in random order. Recursive directories are supported; files inside subdirectories will also be played.

Last modified
Lines 56

Parent directory Download CGIread sitemap Main page

Quick links: (none)

  1. #!/bin/sh
  2. default_music_dir="/media/wrk/music"
  3. player_wrap ()
  4. {
  5.     # .mkv -> Matroska data
  6.     # .ogg -> audio
  7.     # .mp3 -> Audio
  8.     # .webm -> Webm
  9.     # .mp4 -> MP4
  10.     # .m4a -> MPEG v4
  11.     if [ -n "$(file "$1" | grep -E '(WebM|[Aa]udio)|Matroska data|MP(EG v)?4')" ]; then
  12.         cvlc --play-and-exit --no-video "$1" 2>/dev/null || cvlc --play-and-exit "$1"
  13.     else
  14.         file "$1" >&2
  15.     fi
  16. }
  17. mk_playlist ()
  18. {
  19.     # Use du(1) to allow a "structured" playlist.
  20.     du -aL "$music_dir" | cut -f 2 | shuf \
  21.     | while read -r filename; do
  22.         test -d "$filename" || echo "$filename"
  23.     done
  24. }
  25. usage="playshuf [-q] [music-directory]"
  26. quiet=false
  27. if [ "$1" = "-q" ]; then
  28.     quiet=true
  29.     shift
  30. fi
  31. if [ -n "$1" ]; then
  32.     music_dir="$1"
  33. else
  34.     music_dir="$default_music_dir"
  35. fi
  36. # Print the usage line anyway.
  37. $quiet || echo "Usage: $usage" >&2
  38. # Main loop
  39. while true; do
  40.     i=0
  41.     n="$(mk_playlist | wc -l)"
  42.     mk_playlist | while read -r song; do
  43.         i=$(($i + 1))
  44.         $quiet || echo "$i/$n\t$song"
  45.         sleep 2
  46.         player_wrap "$song"
  47.     done
  48. done