]> git.sthu.org Git - shutils.git/blob - mp-play
udev: Add usb_xprofile script and rule
[shutils.git] / mp-play
1 #!/bin/bash
2
3
4 playlist=.playlist.tmp
5 player="/usr/bin/mplayer"
6 if [ -x "/usr/bin/mplayer2" ]; then
7 player="/usr/bin/mplayer2"
8 fi
9 if [ -x "/usr/bin/mpv" ]; then
10 player="/usr/bin/mpv"
11 fi
12
13 playeropts=""
14 $player -af help | grep -q bs2b && playeropts="${playeropts} -af-add bs2b"
15
16
17 optRandom=""
18 optRepeat=""
19 optRecursive=0
20 optRemove=0
21 optQuiet=""
22 optVerbose=0
23 optReverse=0
24 optSorttime=0
25
26
27 function showHelp
28 {
29 echo "Usage: $0 [-h|--help]"
30 echo " $0 [OPTIONS] [--] [DESTINATIONS]"
31 echo ""
32 echo "OPTIONS:"
33 echo " -a, --recursive recursively find files to play"
34 echo " -h, --help show this message"
35 echo " -q, --quiet do not produce output when playing"
36 echo " -r, --repeat repeat playing infintely"
37 echo " -R, --random randomize files"
38 echo " --remove ask to remove played files after playing"
39 echo " --reverse reverse playing order"
40 echo " --time sort by age of file"
41 echo " -v, --verbose produce more output"
42 }
43
44 if ! options=$(getopt -u -o hRravq -l time,random,repeat,recursive,reverse,remove,quiet,verbose -- "$@")
45 then
46 showHelp >&2
47 exit 1
48 fi
49 set -- $options
50
51 #Parse the arguments
52 while [ $# -gt 0 ]
53 do
54 case "$1" in
55 -h|--help) showHelp; exit 0 ;;
56 -R|--random) optRandom="-shuffle" ;;
57 -r|--repeat) optRepeat="-loop 0" ;;
58 -a|--recursive) optRecursive=1 ;;
59 -v|--verbose) optVerbose=1 ;;
60 -q|--quiet) optQuiet="-quiet" ;;
61 --remove) optRemove=1 ;;
62 --reverse) optReverse=1 ;;
63 --time) optSorttime=1 ;;
64 --) shift; break ;;
65 -*) echo "Unrecognized option $1" >&2; showHelp >&2; exit 1 ;;
66 *) break ;;
67 esac
68 shift
69 done
70
71
72
73 if [ "$optRecursive" == "1" ]; then
74 maxlevel=9999
75 else
76 maxlevel=1
77 fi
78
79
80 # Create playlist
81 find "$@" -maxdepth $maxlevel -type f \
82 -iname "*.mp3" -or -iname "*.avi" -or -iname "*.flv" -or -iname "*.flac" \
83 -or -iname "*.mp4" -or -iname "*.ogg" -or -iname "*.m4a" \
84 | sort > $playlist
85
86 [ "$optSorttime" == "1" ] && cat "$playlist" | xargs ls --sort=time | sponge "$playlist"
87 [ "$optReverse" == "1" ] && cat "$playlist" | tac | sponge "$playlist"
88
89
90 cntfiles=`wc -l $playlist | cut -d " " -f 1`
91
92 #No files found...
93 if [ "$cntfiles" == "0" ]
94 then
95 echo "No files found."
96 exit 1
97 fi
98
99
100
101 echo "Playing $cntfiles files..."
102 echo ""
103
104 # Print playlist, if we ask for removing files later
105 if [ "$optRemove" == "1" -o "$optVerbose" == "1" ]; then
106 cat $playlist
107 fi
108
109
110 # Play it
111 $player $playeropts $optQuiet $optRandom $optRepeat -playlist "$playlist"
112
113
114 # Remove the files in the playlist
115 if [ "$optRemove" == "1" ]; then
116 echo ""
117 echo -n "Removing played files? (y/a/N) "
118 read isdel
119 if [ x"y" == x"$isdel" ]; then
120 rm -v `cat $playlist`
121 elif [ x"a" == x"$isdel" ]; then
122 rm -v -i `cat $playlist`
123 fi
124 fi
125
126 #Remove the playlist itself
127 rm -f $playlist
128