Convert multiple audio/photo files in one step

My wife asked me to convert a number of FLAC files to MP3. Apparently, this was supposed to be very easy to accomplish in Linux. She found a web site with a script that, unfortunately, worked only in very specific cases. I managed to convert the script so it works on all UNIX systems (including OsX), by using the bash shell (terminal).

To actually do the conversion, you need to have flac and lame installed. Nothing else is required. There were several problems I ran into by using the original script. First of all, as many shell scripts, it is written in such a way to accept only file (and folder) names which have no spaces or brakes. This, for a modern user, is not acceptable. I managed to find a way around it using “globstar”. Here is the script:

#!/bin/bash
convert_dir=$1
output_dir=$2
bitrate=$3

# Ensure required parameters are passed in
if [ -z “$convert_dir” ] || [ -z “$output_dir” ];
then
  echo “Usage: flac2mp3 rip_dir output_dir [bitrate]”
  exit
fi

# Default to 320 kbps if bitrate not specified
if [ -z $bitrate ];
then
  bitrate=320
fi

# Get the list of files to convert and convert each file, one at a time
shopt -s globstar
for file in “$convert_dir”/*.flac
do
  outputname=`basename “$file”`
  output=${outputname%\.*}
  flac -dc “$file” | lame -b $bitrate – $output_dir/”$output”.mp3
done

echo “Conversions complete!”

That’s all! To install:

Copy the text above in a file named “fl2mp3” and place it in your /usr/bin folder. Change the permissions on the file to 755 (sudo chmod 755 /usr/bin/fl2mp3). To use the script, simply type (anywhere on the computer you might be):

fl2mp3 input_dir output_dir [bitrate]

where you change “input_dir” with the folder where you have your flac files, and “output_dir” with the folder where you want your mp3 files. These folders may be the same, if you’d like it so. [bitrate] switch is not necessary – the default is 320 mbps, but you can put any number you like.

To extend a bit on this, I will add a nifty way to resize a lot of photo files in a directory. I ran into this problem when I had to upload a bunch of files on flickr, but they were too large. You could modify this script, but there is even an easier way. You have to have imagemagick installed (if you don’t, just type

sudo apt-get install imagemagick

in your terminal. Go (within the terminal) to the directory where you have your files and simply type:

mogrify -resize 50% -format jpg *

In this example, all the files will be resized to 50% of their original size (each dimension, hence, the file size will be 4x smaller than the original). The jpg switch determines the output file format. You may also specify width and height by the following command:

mogrify -resize 800×600 -format jpg *

but this may be unpractical, unless all your original files are in a 4:3 ratio.

This entry was posted in Computing and tagged , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s