bash scripting: playlist maker

Summary

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.

For those who want to dive right in, the script is included here (or see the end of the article for full script):

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.

Bash
  1. #!/bin/bash
  2. # biafra ahanonu
  3. # updated: 2013.03.23
  4. # script to make playlists
  5.  
  6. # Yes/No function
  7. getYesNo(){
  8.         select terminateSignal in "Yes" "No"
  9.         do
  10.                 case $terminateSignal in
  11.                         "Yes" )
  12.                                 return 1;;
  13.                         "No" )
  14.                                 return 0;;
  15.                 esac
  16.         done   
  17. }
  18.  
  19. # Change file separator to allow use of files with spaces
  20. oldIFS=$IFS
  21. IFS=$(echo -en "\n\b")
  22.  
  23. # Ask user for directory
  24. echo "Directory? "
  25. read userDir
  26. echo $userDir
  27.  

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.

Bash
  1.  
  2. # Ask to remove old .m3u files
  3. echo "Remove old .m3u files? "
  4. getYesNo
  5. response=$?
  6. if [[ $response == 1 ]]; then
  7.         find . -regex '.*\.m3u' -delete
  8. fi
  9.  
  10. # Get file extensions to search for
  11. fileExt='mp3,wma,aac,flac,ogg,m4a,m4p,wav'
  12. echo 'Current search file extensions: '$fileExt
  13. echo "Add more extensions? "
  14. getYesNo
  15. response=$?
  16. if [[ $response == 1 ]]; then
  17.         echo "List extensions, separated by a comma: "
  18.         read fileExtUser
  19.         fileExt=$fileExt$fileExtUser
  20. fi
  21.  
  22. # Convert to find ready form
  23. fileExt=$(echo $fileExt | sed 's/,/\\|/g')
  24.  

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!

Bash
  1.  
  2. # Loop over all folders in directory and add playlist to the root folder
  3. for folder in $(ls -d */); do
  4.         playlistName=$(echo $folder | sed 's/\///g;s/ /_/g')
  5.         folderName=$(echo $folder | sed 's/\///g')
  6.         find "$folder" -regex $fileExt | sed "s/${folderName}\///g" > $folder$playlistName".m3u"
  7.         echo $playlistName".m3u"
  8. done
  9.  
  10. # Return file separator to default
  11. 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.

download playlistMaker.sh

Bash
  1. #!/bin/bash
  2. # biafra ahanonu
  3. # updated: 2013.03.23
  4. # script to make playlists
  5.  
  6. # Yes/No function
  7. getYesNo(){
  8.         select terminateSignal in "Yes" "No"
  9.         do
  10.                 case $terminateSignal in
  11.                         "Yes" )
  12.                                 return 1;;
  13.                         "No" )
  14.                                 return 0;;
  15.                 esac
  16.         done   
  17. }
  18.  
  19. # Change file separator to allow use of files with spaces
  20. oldIFS=$IFS
  21. IFS=$(echo -en "\n\b")
  22.  
  23. # Ask user for directory
  24. echo "Directory? "
  25. read userDir
  26. echo $userDir
  27. cd $userDir  
  28.  
  29. # Ask to remove old .m3u files
  30. echo "Remove old .m3u files? "
  31. getYesNo
  32. response=$?
  33. if [[ $response == 1 ]]; then
  34.         find . -regex '.*\.m3u' -delete
  35. fi
  36.  
  37. # Get file extensions to search for
  38. fileExt='mp3,wma,aac,flac,ogg,m4a,m4p,wav'
  39. echo 'Current search file extensions: '$fileExt
  40. echo "Add more extensions? "
  41. getYesNo
  42. response=$?
  43. if [[ $response == 1 ]]; then
  44.         echo "List extensions, separated by a comma: "
  45.         read fileExtUser
  46.         fileExt=$fileExt$fileExtUser
  47. fi
  48.  
  49. # Convert to find ready form
  50. fileExt=$(echo $fileExt | sed 's/,/\\|/g')
  51. fileExt='.*\.\('$fileExt'\)'
  52.  
  53. # Loop over all folders in directory and add playlist to the root folder
  54. for folder in $(ls -d */); do
  55.         playlistName=$(echo $folder | sed 's/\///g;s/ /_/g')
  56.         folderName=$(echo $folder | sed 's/\///g')
  57.         find "$folder" -regex $fileExt | sed "s/${folderName}\///g" > $folder$playlistName".m3u"
  58.         echo $playlistName".m3u"
  59. done
  60.  
  61. # Return file separator to default
  62. IFS=$oldIFS

-biafra
bahanonu [at] alum.mit.edu

additional articles to journey through:

r functions: knitr wrapper
13 july 2013 | programming

Thought i'd share the wrapper function i use to knit files using the excellent knitr[...] package in R. This allows easy creation of an html page that integrates commentary, R code, and the resulting plots (which are base64 encoded, so the html file is fully portable).

wary statistics #1: the tale of cdc mortality
06 april 2020 | statistics

I will briefly discuss properly interpreting data you might see in the mainstream or on social media. The takeaway: if recent data for some[...] measure (e.g. pneumonia deaths) from this year looks to be different than prior years, make sure to check that it is not an artifact of data collection or compilation.

stanford bing concert hall: first impressions
15 december 2012 | stanford

Designed by architect Richard Olcott (Ennead Architects) and sound designer Dr. Yasuhisa T[...]oyota (Nagata Acoustics), the Bing Concert Hall is stunning. Robert Campbell (Fisher Dachs Associates) was on hand during the second sound check (along with Richard and Dr. Toyota) to discuss the philosophy behind the building, a bit of history, and where they hope it will be in the future. This post is my impressions of the place along with notes from their interview.

global history of architecture
08 june 2013 | architecture

One of my favorite classes at MIT was 4.605 (Global History of Architecture), which explored various styles and themes found throughout dif[...]ferent buildings and other types of architecture from the beginning of civilization to the present. In the spirit of sharing my enthusiasm for the course i'll discuss some takeaways from, and have included a couple papers i wrote for, the class.

©2006-2025 | Site created & coded by Biafra Ahanonu | Updated 21 October 2024
biafra ahanonu