Sunday, September 1, 2013

Encoding audio to MP3 with LAME, OSX and a right mouse click

I always render WAV / AIFF

Ableton Live 9...for some reason, I thought encoding to mp3 was going to be part of the v9 release.  Sadly Ableton Live 9 still has no way to render mp3 audio directly.

However--turns out, this is not the end of the world for me.  When I use Reaper, I rarely encode to MP3 directly from Reaper either.  Every now and then, the encoder will distort a render, causing an "burble".  UNSATISFACTORY!   So as a matter of habit, I pretty much only render to WAV or AIFF regardless if Live or Reaper.

Still, MP3 is pretty hand format.  How do I get from WAV to MP3 on a Mac?

Audio to MP3 workaround: MAX

I had been using MAX ( http://sbooth.org/Max/ ) and that worked well enough.  However, I am growing weary of the repetitive nature of find rendered file, drop onto max, click convert, etc etc.

Using LAME

I had some time, so I decided to setup a LAME encoder based workflow.  I want the Author, Album, Genre, etc to all be very specific, static things: mainly, me!   So encoders like MAX, or even iTunes--too much repetitive work!   The gold standard mp3 encoder for the home hobbiest is Lame.  Lame doesn't come with OSX by default.

 That means installing Lame.   Hmm..don't want to type in lame commands each time, so that means we'll use PERL to execute the LAME command line.  We'll want to know when the MP3 is created, so we'll use "notifier" to let us know when encoding is done.  And finally, we'll use Apple's Automator, to create a service that will allow us to right click on an audio file (or a bunch of them!) and encode right from the finder. 

Preliminary Steps

1. Setup OSX to run Perl Scripts
Follow along from a prior post:

2. Install "Notifier" in OSX
Follow along from a prior post:

Install LAME

The politics and explanation behind LAME makes my eyes bleed.  We'll jump to the point: for OSX, snag LAME from here:  http://www.thalictrum.com/en/products/lame.html  

Go ahead and install that bad boy using the bog standard OSX installer found on that link (hopefully link still works). When done, in a terminal window, key in "lame" to see if lame installed ok!

Setup PERL Script


1. I created a new Perl script in ~/Documents/Projects/Perl called "encode_to_mp3.pl"

--- BEGIN PERL ... snip below this line ---

#!/usr/bin/perl

use File::Basename;
use Cwd 'abs_path';

######## SETUP ##############

# Get Date
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);

# MP3 tag setup
$mp3_tag{"ARTIST"}="Crufty";
$mp3_tag{"ALBUM"}="Home";
$mp3_tag{"YEAR"}=$year+1900;
$mp3_tag{"GENRE"}="Homemade";


$notifier='~/Documents/Projects/Perl/terminal-notifier.app/Contents/MacOS/terminal-notifier';

$lame_bin='/usr/local/bin/lame';
$lame_encoding='insane';

######## BEGIN ##############

### COMMAND LINE PARAM HANDLING

# 1. cmd line param check
if ($#ARGV!=0) {
#no input file provided
$script=basename($0);
print "Usage: $script < input file >\n";
exit 1;
}

# 2. input file check
$arg_input_file=$ARGV[0];
if (!-e $arg_input_file) {
#input file not found
print "Error: $arg_input_file not found.\n";
exit 2;
}

### INPUT/OUTPUT FILENAMING

#output mp3 file
$input_file=abs_path($arg_input_file);

($fname, $fpath, $fext) = fileparse($input_file, qr/\.[^.]*/);
$output_file=$fpath.$fname.".mp3";


### ENCODE TO MP3!!
#Encode
system("$notifier -title 'Encode to MP3' -message 'Convert to mp3: $fname'");

#encode input to mp3
system("$lame_bin --preset $lame_encoding --ta '$mp3_tag{ARTIST}' --tl '$mp3_tag{ALBUM}' --ty '$mp3_tag{YEAR}' --tg '$mp3_tag{GENRE}' \'$input_file\' \'$output_file\'");

#Check results
$rv=$?;
if ($rv!=0) {
system("$notifier -title 'Encode to MP3' -message 'Conversion error ( $fname) - $rv'");
exit 3;
}
else {
system("$notifier -title 'Encode to MP3' -message 'Conversion complete: $fname'");
}


exit 0;

--- end PERL ... snip above this line ---


2.  Remember, perl scripts need permission bits!  So from terminal: chmod a+x ~/Documents/Projects/Perl/encode_to_mp3.pl

3. Some customization & tweak notes


In the PERL script, remember to update the below items to read what makes sense for you.  I put everything into one "HOME" album,  using genre "Homemade".  That keeps my stuff well removed from the rest of my music collection:

$mp3_tag{"ARTIST"}="Crufty";
$mp3_tag{"ALBUM"}="Home";
$mp3_tag{"GENRE"}="Homemade";



You will want to change these for your own nefarious needs!

You can easily change the MP3 "quality" by changing the lame preset, from 'insane' to 'medium', 'standard', or 'extreme', etc:
$lame_encoding='insane';

Create Automator Service

1. First, start automator--the type of document you want to create is "Service"

2. I saved my Automator service as "Encode to mp3"

3. In my "Encode to mp3" service, I added one "Run Shell Script" action as follows:

The script itself is:
for f in "$@"
do
~/Documents/Projects/Perl/encode_to_mp3.pl "$f"
done


Try it out

1. Select a WAV or AIFF in finder, right click, and pick "Encode to mp3"...you should get a notification when encoding begins, and when its complete!



Next Level Stuff

For me, this is all I need.  Still--I could see how it would be handy to have the automator script ask for author, title, genre, album, etc, maybe w/defaults already in mind.  So that could be a good exercise for the reader. 

I could also see how it might be handy for the automator script to automatically add an encoded mp3 to iTunes...however, for me, this would result in a lot of junk mp3s cluttering up my "sacrosanct"  iTunes lib (hur hur), and for now, it is just as easy to double click and "auto import" mp3s that pass--well not the gold, silver or bronze standard...but at least the copper standard :)

Hope this helps someone out there...

 






6 comments:

  1. This is exactly what I was looking for! Thank you so much for the information. I have one question- I have set up everything in the way I was told in the instruction, but I do not see the "Encode to mp3" in my right click menu. Is there a way to check if everything is setup correctly? I use OS X Maverick. Thank you.

    ReplyDelete
  2. Keep the balls rolling!! Nice posts you have given for us.
    HifiBeats.com

    ReplyDelete
  3. I think this is an informative post and it is very beneficial and knowledgeable. Therefore, I would like to thank you for the endeavors that you have made in writing this article. All the content is absolutely well-researched. Thanks... takie ndou babe ngisite/ngiyabonga

    ReplyDelete
  4. Wow, What an Outstanding post. I found this too much informatics. It is what I was seeking for. I would like to recommend you that please keep sharing such type of info.If possible, Thanks. ntencane wawuthembeni mp3

    ReplyDelete
  5. I sent your articles links to all my contacts and they all adore it including me.
    Carnao Beats

    ReplyDelete


  6. Thanks for sharing these awesome tips.4K Stogram

    ReplyDelete

Write what you think. If things get spammy, review time.