Sunday, January 31, 2010

rmToMp3.pl

Here is the perl script to convert .rm files to .mp3 format on linux. Needed this script since my DVD player cannot play .rm files. Script needs the following software to work
  1. Perl
  2. mplayer
  3. lame
  4. wget
The script can download rm or ram files and then process the download .rm files to produce the .mp3 file.
#!/usr/bin/perl -w
use strict;

BEGIN{
$\="\n";
}

&usage if @ARGV!=1;
my $fileName=shift;
&usage if ($fileName !~ m#\.(ram|rm)$#);
&process_ram($fileName) if ($fileName =~ m|\.ram$|);
&process_rm($fileName) if ($fileName =~ m|\.rm$|);


sub usage(){
print STDERR "usage: $0 filename.rm/ram\n";
exit 1;
}

sub process_ram(){
my $fileName=shift;
$fileName=&processUrl($fileName);
open RAM_FILE, $fileName || die "Failed To Process File: $!";
while(){
chomp;
chop if ~ m/\r$/;
$_ = &processUrl($_);
&convertToMp3($_);
}
close RAM_FILE;
}

sub process_rm(){
my $fileName=shift;
$fileName=&processUrl($fileName);
&convertToMp3($fileName);
}

sub convertToMp3(){
my $fileName=shift;
chomp($fileName);
chop($fileName) if $fileName =~ m/\r$/;
print "Processing File...$fileName";
my ($dirName,$baseName)=&dirname($fileName);
$baseName =~ s/\.rm/.mp3/;
die "Invalid File: ${fileName}" unless(-e $fileName);
`mplayer $fileName -ao pcm 2>/dev/null`;
`lame -h -b 128 audiodump.wav $dirName/$baseName`;
`rm audiodump.wav`;
}

sub dirname(){
return (".",$_[0]) unless $_[0] =~ m|^/|;
return $_[0]=~ m|(.*/)(.*)$|;
}
sub processUrl(){
my $fileName=shift;
chomp($fileName);
if(&isUrl($fileName)){
&download($fileName);
return $fileName =~ m|.*/(.*)$|,$1;
}
return $fileName;
}

sub isUrl(){
$_[0] =~ m/^http.*/;
}

sub download(){
print "Downloading File...$_[0]";
system("wget -c $_[0]")==0 || die "Failed to download file: $_[0]";
}