Script to convert flac to mp3

My first script.

I know this script has been made a hundred times before. However I have quite specific needs and all the scripts out there would only convert files within your current directory. And I want to convert a lot of files, in lots of different directories, plus I'm constantly adding CD's to my collection. So I wanted a script that would scan my flac folder and mirror it in my mp3 folder, whenever I added a new flac file it would update my mp3 folder. I also have folder art for every album and artist and so I also wanted to copy these files over if they haven't been already.

Re: Script to convert flac to mp3

If you just want a copy of all of your flac files in mp3 format then take a look at mp3fs.

It creates a virtual mp3 directory which mirrors your flac directory, but without taking up any space. Whenever you access anything in the mp3 directory it is transcoded on the fly. I've been using this for years on my server with great results.

Re: Script to convert flac to mp3

Sounds interesting, but I want my complete music directory in mp3 format so I can copy to a usb flash drive and use in my car and to an SD card for my tablet. Although I would question why you need it done on the fly, why would you willing use an mp3 when you have a flac file sitting there. I need to because the computer in my car doesn't support flac and there is not enough room on the tablet for all my flac files.

Re: Script to convert flac to mp3

That is exactly what you use mp3fs for.

My original CD rips live in /mnt/data/music/flac and additionally I have it all in mp3 format in /mnt/data/music/mp3.
My mp3 directory is only used to share files across the network to devices that don't support flac, or for copying music to portable devices.

The only difference between my setup and yours is that your mp3 files take up additional space on your hard drive and you have to manually keep your directories in sync, but mine don't take up any space at all and are always in sync (due to the use of mp3fs).

Even though they appear to be normal files, none of the music in my mp3 directory actually exists on the hard drive. Mp3fs makes it look like it does but it is transcoding the flac files into mp3's on the fly when you access them.

Re: Script to convert flac to mp3

if it can have spaces, wrap it up in "". If for whatever reason you had 2 spaces next to each other, they would get compressed to 1. I know you have the dir hardcoded but adding quotes costs you nothing and in case you even wanted to make the script parametrized you would be good to go right off the bat against any directory name you can throw in.

simple substitutions like these can and should be done without resorting to external programs (which mean additional overhead).
if you want to trim something from the right side you can use ${variable%pattern}, eg

Code:

mp3_loc=${i%flac}mp3

(you even use this feature to get mp3_path) or use replacement ${variable/pattern/replacement}, eg

Code:

mp3_loc=${i/%flac/mp3} # additional % forces the pattern to much at the very end
mp3_files=$(find "${flac_loc//flac/mp3}" -name '*.mp3' | wc -l) # // equivalent to sed s///g

Be more precise with your find-and-replace's. Your sed would substitute not only the extension (flac$) but every other 'flac' there is. It would be ok with your hardcoded dir, not so much with files (you have no guarantee you'll always have files that have no flac in their name).

find -name '*.flac' | wc -l is not really reliable if you want to count files. Newline is a totally legit character to be used in filenames and it would easily throw your script off.
use something like this instead

Code:

find "$flac_loc" -name '*.flac' -printf "x" | wc -c

Another way to keep the list of files with their count for free would be to use arrays.

Re: Script to convert flac to mp3

I was using sed as I needed to substitute flac for mp3 within the middle part of the dir. So I have three parts base directory / music folders / music file. I've decided to set the base directories in the beginning, I'm assuming there might be some cost savings rather than doing it everytime within the loop. I can then use pattern substitution for the file extension.

Re: Script to convert flac to mp3

Re: Script to convert flac to mp3

Shell substitution is done by the shell itself. It spares you a sed invokation, a pipe, and a command substitution. So it is definitely better. But it is more limited than sed. In this particular case, where you substitute a fixed substring with another fixed substring, there is no need going the sed route.

Re: Script to convert flac to mp3

just like schragge said, if it's a relatively trivial substitution and you can do it with parameter expansion, do it. Parameter expansion is pretty much for free.
Use sed when you need fullblown regular expressions or solution in pure bash is simply too complex. Standard expansion doesn't really deal with things like 'any sequence of digits' which is trivial in regexes.

bash can be made to support slightly more powerful syntax of patterns where +, * and ? work like in regexes.