Organising my music collection with find and ffmpeg

Since I bought a Synology NAS I've been spending time sorting out my music library. Let's say it's been a very relaxing hobby as I get to listen to music I hadn't listened to in years, and it brings back great memories and also brings me to research about "what happened to that indie artist whose protected demos I downloaded from MySpace using Firebug in 2007" and I end up in a YouTube hole looking for remixes and live versions of my favourite tracks, so that's fun.

People always ask me why do I bother keeping an MP3 collection, when "I could just use a service like Spotify". The answer is: I like indie music and obscure music and bootlegs and all sorts of things that the record label won't put on Spotify, mostly because often the record label has either disappeared or there isn't even a record label to start with (as is the case with unsigned artists). So curating this sort of collection is a very interesting hobby--sometimes I feel like I own the last copies of the music of some artists as their myspace pages have disappeared, etc.

But there are things I do not really enjoy doing, namely repetitive stuff.

So whenever I can automate something I will do. The terminal is really great for that--and this is a bit of an exercise to improve my bash skills too! ?

Task 1: remove unneeded files from a folder and its subdirectories

Sometimes you download a bunch of music files and they're organised in directories. And maybe there's a lot of .url or .rtf or .txt files you don't care about. You could go and delete them manually, or you could just use find to remove them in just one command:

For example, to delete all .m3u playlists in a folder and its subdirectories:

find . -iname *.m3u -delete

You could just run it without the -delete switch to make sure you're not deleting too much (there is NO going back with this! no recycle bin!)

find . -iname *.m3u

Task 2: Rename cover [whatever].jpg to folder.jpg

Apparently these Synology devices will use folder.jpg to display the cover art of each album, assuming each album is "a folder" and there is such a named imaged file in the folder. But these files are sometimes not named like that; they might be cover.jpg or random-string-of-letters-and-numbers.jpg. Again, you could rename them manually, or you press the turbo pedal and go megafaster with this:

find . -iname *.jpg -execdir mv {} folder.jpg \;

Warning: this is somehow a bit too "fire and forget", and might fail if there are already existing folder.jpg files or just more than one file in the directory. I haven't tested those scenarios, but what you can do before running the actual renamer is to use find to list the files that match the .jpg pattern. If you find one per folder, then you're good to go:

find -iname *.jpg

Probably an over-the-top improvement would use ImageMagick's convert to list the size of the files and just rename to folder.jpg the largest one, or whatever, but I am not so obsessed with perfection, and besides I'm only running this on one artist's folder. Also: maybe if you're having so much logic, perhaps it's better if you write that logic in an intelligible sort of script such as Python or Node.js. Your future self will thank you forever, unlike if you write Bash code full of back ticks and other mysterious powertricks you find in Stack Overflow and painfully assemble together, only to forget immediately.

Task 3: convert the files to a format you actually like

I've also got music files in formats I don't like. I'm a bit of a savage and don't care so much about FLAC or sound purity (here, I said that), because I don't actually listen to music in high end equipment, and besides I used to listen to music in tapes that had been used and reused so many times you could still listen to the previous three or four recordings, so even the crappiest digital encoding is often like super state of the art for my ears.

I tend to just convert FLAC stuff to 320k MP3, but this also works for converting those horrid M4A files to MP3, and OGG to MP3, or WAV to MP3 (yeah, some artists think that WAV is a good distribution format!).

For example, this will convert all *.wav files in a directory to 320K MP3 while keeping the metadata. You'll need ffmpeg installed (you can install it on a Mac with brew install ffmpeg).

(for FILE in *.wav; do ffmpeg -i "$FILE" -f mp3 -ab 320000 -map_metadata 0 -id3v2_version 3 "`basename "$FILE" .wav`.mp3" || break; done)

Replace .wav with the extension you hate the most: e.g. .m4a, and for grittiest results and even more savage souls than mine, you can drop the file size by reducing the bit rate to 128000 or even worse--use 64000 for a very convincing "Using an MP3 player from the 90s with 64 Megs of RAM with very shitty earphones" feeling.

It just occurred to me that maybe you could also use this one to convert a bunch of videos from talks into "podcasts" (by stripping out the video and just keeping the audio), but I haven't tried that.

If you have cool music power tricks to process audio files do send them my way on the comments section--I'd love to add them to my arsenal :-)