play (3460B)
1 #!/usr/bin/env bash 2 3 rootdir="$MUSIC_DIR" 4 mpv_options="--no-video --really-quiet --volume=50" 5 mpv_options_vis="--no-video --really-quiet --volume=50 --script=$HOME/.config/mpv/visualizer.lua -vo caca" 6 mpv_options_noart="--no-video --volume=50" 7 8 play_track() { 9 trap '' INT 10 vis=0 11 if [ -z "$2" ]; then 12 vis=1 13 fi 14 if [[ "$1" == "all" ]]; then 15 track=$(ls|sort -n|head -1) 16 echo "$track" 17 ffmpeg -i "$track" art.jpg &>/dev/null 18 19 if [ -e "art.jpg" ] && [ $vis -eq 1 ]; then 20 im2a art.jpg -T 21 mpv "$(pwd)" $mpv_options 22 rm art.jpg 23 elif [ $vis -eq 1 ]; then 24 mpv "$(pwd)" $mpv_options_vis 25 else 26 clear 27 mpv "$(pwd)" $mpv_options_noart 28 fi 29 else 30 if [ ! -e "$1" ]; then 31 echo "File does not exist: $1" 32 else 33 ffmpeg -i "$1" art.jpg &>/dev/null 34 if [ -e "art.jpg" ] && [ $vis -eq 1 ] ; then 35 im2a art.jpg -T 36 mpv "$(pwd)/$1" $mpv_options 37 rm art.jpg 38 elif [ $vis -eq 1 ]; then 39 mpv "$(pwd)/$1" $mpv_options_vis 40 else 41 clear 42 mpv "$(pwd)/$1" $mpv_options_noart 43 fi 44 fi 45 fi 46 trap clean_up INT 47 clear 48 } 49 read_letter() { 50 read -sn 1 -p "Continue? [Y/n]" CONF 51 } 52 clean_up() { 53 trap - INT 54 clear 55 exit 0 56 } 57 58 init_check() { 59 deps=(ffmpeg mpv im2a) 60 for dep in ${deps[@]}; do 61 if ! command -v $dep &>/dev/null; then 62 echo "Please install $dep." 63 exit 1 64 fi 65 done 66 } 67 68 init_check 69 cd "$rootdir" 70 trap clean_up INT 71 clear 72 echo "Music Player v1" 73 while :; do 74 read -e -p "> " -a INPUT 75 CMD=${INPUT[0]} 76 ARGS=("${INPUT[@]:1}") 77 case $CMD in 78 "cd") 79 if [ -z "$ARGS" ]; then 80 echo "No directory provided." 81 elif [[ "$ARGS" == "/" ]]; then 82 cd "$rootdir" 83 elif [ ! -d "$ARGS" ]; then 84 echo "Directory does not exist." 85 else 86 pushd "$ARGS" &>/dev/null 87 if [[ $(pwd) == $rootdir* ]]; then 88 popd &>/dev/null 89 cd "$ARGS" 90 else 91 echo "Cannot enter this directory." 92 popd&>/dev/null 93 fi 94 fi 95 ;; 96 "play") 97 if [ -z "$ARGS" ]; then 98 echo "No file provided." 99 else 100 play_track "${ARGS[@]}" 101 fi 102 ;; 103 "ls") 104 if command -v less &> /dev/null; then 105 ls -C | less 106 elif command -v more &> /dev/null; then 107 ls -C | more 108 else 109 ls -C 110 fi 111 ;; 112 "pwd") 113 dir=$(pwd) 114 if [[ $dir == $rootdir ]]; then 115 echo "/" 116 else 117 echo ${dir##$rootdir} 118 fi 119 ;; 120 "q"|"quit") 121 clean_up;; 122 "help"|"h"|"?") 123 echo "Commands:" 124 echo "cd change to artist/album directory" 125 echo "pwd print current directory" 126 echo "ls list contents of current directory" 127 echo "play all play everything in current directory" 128 echo "play [file] play specific track" 129 echo "play [all|file] [something] disable visualiser/art" 130 ;; 131 *) 132 echo "Command does not exist." 133 echo "Type 'help' to show commands." 134 ;; 135 esac 136 done