Decided to take another stab at converting small programs to bash. This time, the python playlist maker was updated to bash and greatly simplified, no longer need to do sketchy recursions.
bash playlist maker
A little bit ago, I posted a short python script to automatically create music playlists. It was a bit clunky and relied on a rather sketchy recursion implementation, i.e. there was no real base case (it just assumed you didn't have an infinitely large directory). I wanted to fix this and transfer the script to a more general language that could be used on most computers. So I turned to bash scripting. Terminal (Unix/OSX), cygwin (Windows), and other shell environments will run bash scripts without any trouble and no extra libraries or software is needed besides the base OS (or in the case of cygwin, the base install).
The script itself is rather simple, its core components is only around eight lines of code, the rest is just options for the user. I have a short yes/no function to standardize asking for input and then change the internal file separator to allow compatibility for people who like to litter spaces throughout their filenames and folders. The directory is requested from the user; I would suggest that you put the script in the root folder and then just type a dot to indicate use of the current directory.
- #!/bin/bash
- # biafra ahanonu
- # updated: 2013.03.23
- # script to make playlists
- # Yes/No function
- getYesNo(){
- select terminateSignal in "Yes" "No"
- do
- case $terminateSignal in
- "Yes" )
- return 1;;
- "No" )
- return 0;;
- esac
- done
- }
- # Change file separator to allow use of files with spaces
- oldIFS=$IFS
- IFS=$(echo -en "\n\b")
- # Ask user for directory
- echo "Directory? "
- read userDir
- echo $userDir
Next, the user is asked to delete old .m3u files and then choose the audio file extensions that their library consist of. Any new extensions are requested are added to the old one and then I use sed to convert the list into a form that is readily interpreted by find.
- # Ask to remove old .m3u files
- echo "Remove old .m3u files? "
- getYesNo
- response=$?
- if [[ $response == 1 ]]; then
- find . -regex '.*\.m3u' -delete
- fi
- # Get file extensions to search for
- fileExt='mp3,wma,aac,flac,ogg,m4a,m4p,wav'
- echo 'Current search file extensions: '$fileExt
- echo "Add more extensions? "
- getYesNo
- response=$?
- if [[ $response == 1 ]]; then
- echo "List extensions, separated by a comma: "
- read fileExtUser
- fileExt=$fileExt$fileExtUser
- fi
- # Convert to find ready form
- fileExt=$(echo $fileExt | sed 's/,/\\|/g')
find allows recursive searching for files matching a regular expression within a folder tree. It is much more robust and elegantly implemented than my use of python and is compatible with all the common operating systems. I loop over all folders in a directory and find their files recursively. The folder name is removed from the path and then all the files are saved to a .m3u file in that folder in a single line. neato!
- # Loop over all folders in directory and add playlist to the root folder
- for folder in $(ls -d */); do
- playlistName=$(echo $folder | sed 's/\///g;s/ /_/g')
- folderName=$(echo $folder | sed 's/\///g')
- find "$folder" -regex $fileExt | sed "s/${folderName}\///g" > $folder$playlistName".m3u"
- echo $playlistName".m3u"
- done
- # Return file separator to default
- IFS=$oldIFS
While different error handling and other code could be added to flesh this out, I think the current implementation is a nice and cheap way to make playlist in an entire library very quickly without installing any additional software. Adding extended M3U directives could be included, but normally music players read the information directly from the files after the playlist has loaded and I thus found the added expense and complexity not worth the effort.
The script is compatible with more obscure symbols that python trips over and sed, grep, and others can be used to further modify output via pipping, much easier than doing it all in python. Overall a fun little exercise in make a script more useful by taking advantage of both bash scripting and pre-existing programs.
- #!/bin/bash
- # biafra ahanonu
- # updated: 2013.03.23
- # script to make playlists
- # Yes/No function
- getYesNo(){
- select terminateSignal in "Yes" "No"
- do
- case $terminateSignal in
- "Yes" )
- return 1;;
- "No" )
- return 0;;
- esac
- done
- }
- # Change file separator to allow use of files with spaces
- oldIFS=$IFS
- IFS=$(echo -en "\n\b")
- # Ask user for directory
- echo "Directory? "
- read userDir
- echo $userDir
- cd $userDir
- # Ask to remove old .m3u files
- echo "Remove old .m3u files? "
- getYesNo
- response=$?
- if [[ $response == 1 ]]; then
- find . -regex '.*\.m3u' -delete
- fi
- # Get file extensions to search for
- fileExt='mp3,wma,aac,flac,ogg,m4a,m4p,wav'
- echo 'Current search file extensions: '$fileExt
- echo "Add more extensions? "
- getYesNo
- response=$?
- if [[ $response == 1 ]]; then
- echo "List extensions, separated by a comma: "
- read fileExtUser
- fileExt=$fileExt$fileExtUser
- fi
- # Convert to find ready form
- fileExt=$(echo $fileExt | sed 's/,/\\|/g')
- fileExt='.*\.\('$fileExt'\)'
- # Loop over all folders in directory and add playlist to the root folder
- for folder in $(ls -d */); do
- playlistName=$(echo $folder | sed 's/\///g;s/ /_/g')
- folderName=$(echo $folder | sed 's/\///g')
- find "$folder" -regex $fileExt | sed "s/${folderName}\///g" > $folder$playlistName".m3u"
- echo $playlistName".m3u"
- done
- # Return file separator to default
- IFS=$oldIFS