| 1 | #!/bin/bash |
| 2 | |
| 3 | # Wrapper for make_playlist into Fvwm-Crystal |
| 4 | # Dominique Michel <[email protected]> 2008-2013 |
| 5 | # |
| 6 | # This script scan each directories and subdirectories for media file |
| 7 | # and write the corresponding playlist files. |
| 8 | # |
| 9 | # Where the playlists are created. Must be writable by the user. |
| 10 | # AudioPlaylists="/home/$(id -un)/PlaylistsAudio" |
| 11 | # VideoPlaylists="/home/$(id -un)/PlaylistsVideo" |
| 12 | # Temporary files, must be into a writable directory. |
| 13 | # |
| 14 | # Syntax: |
| 15 | # PipeRead $[FVWM_SYSTEMDIR]/scripts/make_all_playlists \ |
| 16 | # $[infostore.AudioPlaylists] $[infostore.VideoPlaylists] |
| 17 | |
| 18 | |
| 19 | DirList="/tmp/mkpl_dirlist" |
| 20 | MediaList="/tmp/mkpl_medialist" |
| 21 | |
| 22 | # Create the playlists dir if it doesn't exist |
| 23 | writedir() { |
| 24 | if [[ ! -e "$1" ]]; then |
| 25 | mkdir -p "$1" |
| 26 | echo "Creating $1" |
| 27 | fi |
| 28 | } |
| 29 | mediatype() { |
| 30 | case "$1" in |
| 31 | audio) |
| 32 | writedir ${AudioPlaylists} |
| 33 | ;; |
| 34 | video) |
| 35 | writedir ${VideoPlaylists} |
| 36 | ;; |
| 37 | cdrom) |
| 38 | echo "This is a CDROM, skipping it." |
| 39 | ;; |
| 40 | dvd) |
| 41 | echo "This is a DVD, skipping it." |
| 42 | ;; |
| 43 | *) |
| 44 | echo "$1 is not a valid media type for ${FVWM_USERDIR}/preferences/MediaDirectories." |
| 45 | echo "Please adjust your setting." |
| 46 | ;; |
| 47 | esac |
| 48 | } |
| 49 | |
| 50 | # scan for dir and subdir |
| 51 | scandir() { |
| 52 | find -type d |sort| sed -e 's\.\\' >${DirList} |
| 53 | echo "Scanning $1 for subdirectories." |
| 54 | } |
| 55 | |
| 56 | # scan for audio files |
| 57 | # added ac3, |
| 58 | scanaudiomedia() { |
| 59 | find -maxdepth 1 -regex ".*.[aA][cC][3]\|.*.[aA][pP][eE]\|.*.[fF][lL][aA][cC]\|.*.[mM][pP][3]\|.*.[mM][pP][cC]\|.*.[nN][rR][gG]\|.*.[oO][gG][gG]\|.*.[tT][sS]\|.*.[tT][tT][aA]\|.*.[wW][aA][vV]\|.*.[wW][mM][aA]\|.*.[wW][vV]"|sort |
| 60 | } |
| 61 | |
| 62 | # scan for video files |
| 63 | # added flv, ts |
| 64 | scanvideomedia() { |
| 65 | find -maxdepth 1 -regex ".*.[aA][sS][fF]\|.*.[aA][vV][iI]\|.*.[dD][iI][vV][xX]\|.*.[fF][lL][vV]\|.*.[iI][mM][gG]\|.*.[mM][2][tT]\|.*.[mM][kK][vV]\|.*.[mM][oO][vV]\|.*.[mM][pP][4]\|.*.[mM][pP][eE][gG]\|.*.[mM][pP][gG]\|.*.[nN][rR][gG]\|.*.[oO][gG][mM]\|.*.[rR][aA][mM]\|.*.[rR][mM]\|.*.[rR][mM][vV][bB]\|.*.[tT][sS]\|.*.[wW][eE][bB][mM]\|.*.[vV][oO][bB]\|.*.[wW][mM][vV]"|sort |
| 66 | } |
| 67 | |
| 68 | # write the audio files into the playlist |
| 69 | writeaudio() { |
| 70 | if [[ `scanaudiomedia` != "" ]]; then |
| 71 | scanaudiomedia| sed -e 's\.\\' > ${MediaList} |
| 72 | ## Create the playlist, overwrite the file if it exist |
| 73 | # variable with the first character of the sub-path |
| 74 | subdir="${AudioPlaylists}/`pwd| sed -e "s,$1,,"| sed -e 's,/,,'|sed -e 's,/,_,g'|sed -e 's,\(.\).*,\1,'`" |
| 75 | # Check for main path. Needed because mplayer need a name before ".m3u". And also for this script when multiple calls. |
| 76 | # We also filter out some non wanted characters from the playlist names. |
| 77 | if [[ "$1" == `pwd` ]]; then |
| 78 | echo -n "" >> "${AudioPlaylists}/`echo \"$1\"| sed -e 's,/,,'| sed -e \"s,',_,g\"| sed -e 's,[/"\*\^?\$],_,g'`.m3u" |
| 79 | else |
| 80 | # make sub-dirs |
| 81 | mkdir -p ${subdir} |
| 82 | echo -n "" >> "${subdir}/`pwd| sed -e \"s,$1,,\"| sed -e 's,/,,'| sed -e \"s,',_,g\"|sed -e 's,[/"\*\^?\$],_,g'`".m3u |
| 83 | fi |
| 84 | # Append the audio files into the playlist |
| 85 | while read line; do |
| 86 | if [[ "$1" == `pwd` ]]; then |
| 87 | echo "$(pwd)${line}" >> "${AudioPlaylists}/`echo \"$1\"| sed -e 's,/,,'| sed -e \"s,',_,g\"| sed -e 's,[/"\*\^?\$],_,g'`.m3u" |
| 88 | else |
| 89 | echo "$(pwd)${line}" >> "${subdir}/`pwd| sed -e \"s,$1,,\"|sed -e 's,/,,'| sed -e \"s,',_,g\"| sed -e 's,[/"\*\^?\$],_,g'`".m3u |
| 90 | fi |
| 91 | done <${MediaList} |
| 92 | rm ${MediaList} |
| 93 | fi |
| 94 | } |
| 95 | |
| 96 | # write the video files into the playlist |
| 97 | writevideo() { |
| 98 | if [[ `scanvideomedia` != "" ]]; then |
| 99 | scanvideomedia| sed -e 's\.\\' > ${MediaList} |
| 100 | # Create the playlist, overwrite the file if it exist |
| 101 | if [[ "$1" == `pwd` ]]; then |
| 102 | echo -n "" >> "${VideoPlaylists}/`echo \"$1\"| sed -e 's,/,,'| sed -e \"s,',_,g\"| sed -e 's,[/"\*\^?\$],_,g'`.m3u" |
| 103 | else |
| 104 | echo -n "" >> "${VideoPlaylists}/`pwd| sed -e \"s,$1,,\"| sed -e 's,/,,'| sed -e \"s,',_,g\"| sed -e 's,[/"\*\^?\$],_,g'`".m3u |
| 105 | fi |
| 106 | # Append the video files into the playlist |
| 107 | while read line; do |
| 108 | if [[ "$1" == `pwd` ]]; then |
| 109 | echo "$(pwd)${line}" >> "${VideoPlaylists}/`echo \"$1\"| sed -e 's,/,,'| sed -e \"s,',_,g\"| sed -e 's,[/"\*\^?\$],_,g'`.m3u" |
| 110 | else |
| 111 | echo "$(pwd)${line}" >> "${VideoPlaylists}/`pwd| sed -e \"s,$1,,\"| sed -e 's,/,,'| sed -e \"s,',_,g\"| sed -e 's,[/"\*\^?\$],_,g'`".m3u |
| 112 | fi |
| 113 | done <${MediaList} |
| 114 | rm ${MediaList} |
| 115 | fi |
| 116 | } |
| 117 | |
| 118 | # write the m3u files |
| 119 | writem3u() { |
| 120 | echo -n "Writing the $1 playlists for $2 " |
| 121 | p=0 |
| 122 | while read line; do |
| 123 | if [[ "$p" == 4 ]]; then |
| 124 | p=0 |
| 125 | fi |
| 126 | if [[ "$p" == 0 ]]; then |
| 127 | echo -en "\b|" |
| 128 | fi |
| 129 | if [[ "$p" == 1 ]]; then |
| 130 | echo -en "\b/" |
| 131 | fi |
| 132 | if [[ "$p" == 2 ]]; then |
| 133 | echo -en "\b-" |
| 134 | fi |
| 135 | if [[ "$p" == 3 ]]; then |
| 136 | echo -en "\b\\" |
| 137 | fi |
| 138 | let "p += 1" |
| 139 | basedir=$(pwd) |
| 140 | # echo "${line}" |
| 141 | cd "${basedir}${line}" |
| 142 | case "$1" in |
| 143 | audio) |
| 144 | writeaudio "$2" |
| 145 | ;; |
| 146 | video) |
| 147 | writevideo "$2" |
| 148 | ;; |
| 149 | *) |
| 150 | echo "something wrong" |
| 151 | exit 67 |
| 152 | ;; |
| 153 | esac |
| 154 | cd "${basedir}" |
| 155 | done <${DirList} |
| 156 | rm ${DirList} |
| 157 | echo -en "\b\b...\n" |
| 158 | echo "Done." |
| 159 | } |
| 160 | |
| 161 | usage() { |
| 162 | echo "" |
| 163 | echo "Usage:" |
| 164 | echo " `basename $0` <>audio_playlist_path> <video_playlists_path>" |
| 165 | echo "" |
| 166 | echo "Copyright: This software, `basename $0`, is copyrighted software that is licensed under the GPLv3 or later." |
| 167 | exit 65 |
| 168 | } |
| 169 | |
| 170 | # Do something now |
| 171 | make_playlist() { |
| 172 | mediatype "$3" |
| 173 | if [[ -d "$4" ]]; then |
| 174 | cd "$4" |
| 175 | case "$3" in |
| 176 | audio) |
| 177 | scandir "$4" |
| 178 | writem3u "$3" "$4" |
| 179 | ;; |
| 180 | video) |
| 181 | scandir "$4" |
| 182 | writem3u "$3" "$4" |
| 183 | ;; |
| 184 | cdrom) |
| 185 | echo "Nothing to do." |
| 186 | ;; |
| 187 | dvd) |
| 188 | echo "Nothing to do." |
| 189 | ;; |
| 190 | *) |
| 191 | echo "$1 is not a valid media type for ${FVWM_USERDIR}/preferences/MediaDirectories." |
| 192 | echo "Please adjust your setting." |
| 193 | ;; |
| 194 | esac |
| 195 | else |
| 196 | echo "$4 is not a directory" |
| 197 | fi |
| 198 | } |
| 199 | |
| 200 | # The main program |
| 201 | if [[ $# -ne 4 ]] |
| 202 | then |
| 203 | usage |
| 204 | else |
| 205 | echo "Deleting the old audio play-lists in $1" |
| 206 | rm -fr "$1"/* |
| 207 | echo "Deleting the old video play-lists in $2" |
| 208 | rm -f "$2"/* |
| 209 | echo "Making the user playlist directories when needed." |
| 210 | mkdir -p "$3" |
| 211 | mkdir -p "$4" |
| 212 | echo "Done." |
| 213 | |
| 214 | AudioPlaylists="$1" |
| 215 | VideoPlaylists="$2" |
| 216 | while read line; do |
| 217 | make_playlist "$1" "$2" ${line} |
| 218 | done <"${FVWM_USERDIR}"/preferences/MediaDirectories |
| 219 | fi |