[lug] Shell Scripting Help

Hugh Brown hugh at math.byu.edu
Tue Feb 14 12:54:11 MST 2006


As an alternative I use the attached script to convert from m4a to mp3

I must warn you that I accept no liability for this script (I have used it
to create quite a few mp3's but there are likely still bugs in it and it
is also likely to contain all sorts of novice mistakes.

Hugh

On Tue, 14 Feb 2006, Dan Ferris wrote:

> Ok, using read worked perfectly fine.  Thanks a lot.
>
> Even exporting IFS didn't work.  You learn something new every day.
>
> Dan
>
-------------- next part --------------
#!/usr/bin/perl

# requires that you have faad/libfaad and lame installed

use strict;

my $debug = 0;

# get all the m4a songs in the dir
my $songs=`ls *.m4a`;



# get rid of the last newline
chomp $songs;
# get all the songs into an array
my @songs=split /\n/, $songs;
# step through the array
foreach my $i (@songs) {
# use faad to get info about the song to feed into lame
# faad -i  $i
# this is inefficient and doesn't cope with empty fields
  my %info = ();
  $info{artist} = `faad -i "$i" 2>&1 |grep artist|cut -d":" -f2|cut -c 2-`;
  $info{album} = `faad -i "$i" 2>&1 |grep album|cut -d":" -f2|cut -c 2-`;
  $info{genre} = `faad -i "$i" 2>&1 |grep genre|cut -d":" -f2|cut -c 2-`;
  $info{title} = `faad -i "$i" 2>&1 |grep title|cut -d":" -f2|cut -c 2-`;
  $info{date} = `faad -i "$i" 2>&1 |grep date|cut -d":" -f2|cut -c 2-`;
  $info{track} = `faad -i "$i" 2>&1 |grep track|cut -d":" -f2|cut -c 2-`;
  chomp %info;
  
  if ( $debug == 1 ) {
    print "------------------------\n";
    print "$i\n";
    print "------------------------\n";
    print "Artist: $info{artist}\n";
    print "Album: $info{album}\n";
    print "Genre: $info{genre}\n";
    print "Title: $info{title}\n";
    print "Date: $info{date}\n";
    print "Track: $info{track}\n";
      print "lame --quiet -h -b 256 --tt \"$info{title}\" --ta \"$info{artist}\" --tl \"$info{album}\" --ty \"$info{date}\" --tn \"$info{track}\" --tg \"$info{genre}\" tmp.wav \"$info{artist} - $info{title}.mp3\"\n";
    
  }
  
  if ( $debug == 0 ) {
    print "Decoding: $i\n";
    my $decode_result=system("faad -o tmp.wav \"$i\"");
    if ( $decode_result == 0 ) {
      print "Encoding: $i\n";
      my $encode_result=system("lame --quiet -h -b 256 --tt \"$info{title}\" --ta \"$info{artist}\" --tl \"$info{album}\" --ty \"$info{date}\" --tn \"$info{track}\" --tg \"$info{genre}\" tmp.wav \"$info{artist} - $info{title}.mp3\"");
      if ( $encode_result == 0 ) {
        print "Successfully encoded $i\n";
	system("rm tmp.wav");
      } else {
        print "It borked\n";
      }
    }
  }

  
}
  


More information about the LUG mailing list