]> git.sthu.org Git - shutils.git/blob - mp-play
nsd-dynipwatch: touch watchfile before inotify'ing
[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
10 playeropts="-af-add bs2b"
11
12 optRandom=""
13 optRepeat=""
14 optRecursive=0
15 optRemove=0
16 optQuiet=""
17 optVerbose=0
18 optReverse=0
19 optSorttime=0
20
21
22 function showHelp
23 {
24 echo "Plays mp3 files in all subdirectories."
25 echo "Usage: ./mp-play.sh [-h|--help] [-R|--random] [-r|--repeat] "
26 echo " [-a|-recursive] [--reverse] [--time]"
27 echo " [--remove] [-q|--quiet] [-v|--verbose] [DESTINATION]"
28 echo ""
29 echo "Options:"
30 echo " --time sort by age of file"
31 echo " -R, --random randomize files"
32 echo " -r, --repeat repeat playing infintely"
33 echo " -a, --recursive recursively find files to play"
34 echo " --reverse reverse playing order"
35 echo " --remove ask to remove played files after playing"
36 echo " -q, --quiet do not produce output when playing"
37 echo " -v, --versbose produce more output"
38 }
39
40 #Parse the arguments
41 while [ $# -gt 0 ]
42 do
43 case "$1" in
44 -h|--help) showHelp; exit 0 ;;
45 -R|--random) optRandom="-shuffle" ;;
46 -r|--repeat) optRepeat="-loop 0" ;;
47 -a|--recursive) optRecursive=1 ;;
48 -v|--verbose) optVerbose=1 ;;
49 -q|--quiet) optQuiet="-quiet" ;;
50 --remove) optRemove=1 ;;
51 --reverse) optReverse=1 ;;
52 --time) optSorttime=1 ;;
53 *) break ;;
54 esac
55 shift
56 done
57
58
59
60 if [ "$optRecursive" == "1" ]; then
61 maxlevel=9999
62 else
63 maxlevel=1
64 fi
65
66
67 # Create playlist
68 find "$@" -maxdepth $maxlevel -type f \
69 -iname "*.mp3" -or -iname "*.avi" -or -iname "*.flv" \
70 -or -iname "*.mp4" -or -iname "*.ogg" \
71 | sort > $playlist
72
73 [ "$optSorttime" == "1" ] && cat "$playlist" | xargs ls --sort=time | sponge "$playlist"
74 [ "$optReverse" == "1" ] && cat "$playlist" | tac | sponge "$playlist"
75
76
77 cntfiles=`wc -l $playlist | cut -d " " -f 1`
78
79 #No files found...
80 if [ "$cntfiles" == "0" ]
81 then
82 echo "No files found."
83 exit 1
84 fi
85
86
87
88 echo "Playing $cntfiles files..."
89 echo ""
90
91 # Print playlist, if we ask for removing files later
92 if [ "$optRemove" == "1" -o "$optVerbose" == "1" ]; then
93 cat $playlist
94 fi
95
96
97 # Play it
98 $player $playeropts $optQuiet $optRandom $optRepeat -playlist "$playlist"
99
100
101 # Remove the files in the playlist
102 if [ "$optRemove" == "1" ]; then
103 echo ""
104 echo -n "Removing played files? (y/a/N) "
105 read isdel
106 if [ x"y" == x"$isdel" ]; then
107 rm -v `cat $playlist`
108 elif [ x"a" == x"$isdel" ]; then
109 rm -v -i `cat $playlist`
110 fi
111 fi
112
113 #Remove the playlist itself
114 rm -f $playlist
115