[lug] Shell Scripting Help

Dan Ferris dan at usrsbin.com
Tue Feb 14 20:59:46 MST 2006


Thanks for that script, it's MUCH better than what I was doing.

I've modified it some so that it will go through a directory recursively 
and scan for m4a files.  It's definatly hacked and probably not perfect 
but it seems to work:

Dan
====

#!/usr/bin/perl

use Cwd; # module for finding the current working directory
$|=1;    # turn off I/O buffering

sub ScanDirectory {
    my ($workdir) = shift;

    my($startdir) = &cwd; # keep track of where we began

    chdir($workdir) or print "Unable to enter dir $workdir:$!\n";

    opendir(DIR, ".") or print "Unable to open $workdir:$!\n";
    my @names = readdir(DIR);
    closedir(DIR);

    foreach my $name (@names){
        next if ($name eq ".");
        next if ($name eq "..");

        if (-d $name){                     # is this a directory?
            &ScanDirectory($name);
            next;
        }
        unless (&CheckFile($name)){
            #print &cwd."/".$name."\n"; # print the bad filename
        }
    }
    chdir($startdir) or print "Unable to change to dir $startdir:$!\n";
}

sub CheckFile{
# get all the m4a songs in the dir

my $debug = 0;
my $name = shift;

if ($name =~ /^.*m4a$/)
{
        my $song=$name;
        # get rid of the last newline
        chomp $song;
        # get all the songs into an array
        my @songs=split /\n/, $song;
        # 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 -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 -h -b 256 --verbose --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\"");
                $i =~ s/\.m4a/\.mp3/g;
              my $encode_result=system("lame -h -b 256 --verbose --tt 
\"$info{title}\" --ta \"$info{artist}\" --tl \"$info{album}\" --ty 
\"$info{date}\" --tn \"$info{track}\" --tg \"$info{genre}\" tmp.wav 
\"$i\"");
              if ( $encode_result == 0 ) {
                print "Successfully encoded $i\n";
                system("rm -f tmp.wav");
                system("rm -f \"$i\"");
              } else {
                print "It borked\n";
              }
            }
          }
        }
            return 1;
        }
}
&ScanDirectory(".");

Hugh Brown wrote:

>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
>>    
>>
>>
>
>------------------------------------------------------------------------
>
>#!/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";
>      }
>    }
>  }
>
>  
>}
>  
>  
>
>------------------------------------------------------------------------
>
>_______________________________________________
>Web Page:  http://lug.boulder.co.us
>Mailing List: http://lists.lug.boulder.co.us/mailman/listinfo/lug
>Join us on IRC: lug.boulder.co.us port=6667 channel=#colug
>




More information about the LUG mailing list