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