I have been convertine wav’s to MP3’s for years and saving to an external storage server using a fuse mounted directory, under the “monitor” directory.
Recently I switched to a new-build FreePBX14 (official distro), which I love by the way, but now my scripts are buggy and I am hoping that someone in here will see something that I am missing, and help be get it working correctly…
The problem isn’t huge, but a real PITA because I have to remember to maually execute the job late in the day, or it won’t finish before midnight, and then I have un-converted files.
The Problem is simply:
When step 1 (MP3convet.sh) is executed by crontab, it processes ONE file instead of running all available.
If I manually execute /usr/local/bin/MP3convert, it will run everything available at the time I execute it.
The whole process takes about 12 seconds including logging into the db, loggingq and reporting
Basically I have a .sh that starts the main script, then reports to a couple email addresses:
Step 1 (crontab): */05 * * * * /usr/local/bin/wav2mp3/MP3convert.sh (FYI - I tried 10 minutes, 5 minutes, and 1 minute)
Step2: MP3convert.sh
#!/bin/bash
TODAY=date +%F
echo “
–START-------------
”date
>> /tmp/MP3convert.log
/usr/local/bin/wav2mp3/ls_rec_file.pl $TODAY | tee -a /tmp/MP3convert$TODAY.log | mail -r [email protected] -s “CES MP3 Convert” [email protected]
Step 3: The mack-daddy which handles time stamp, remove silence, convert to mp3, and update CDR to reflect wav to mp3 extension. (note that I added some dots [ ie .#] to stop the post from having bold letters)
#!/usr/bin/perl -w
.# Script to post process recordings in Asterisk / FreePBX
.# You can set in FreePBX the “Run After Record” parameter
.# to be:
.# /var/lib/asterisk/bin/record_runafter.pl ^{UNIQUEID} ^{MIXMONITOR_FILENAME}
$|++;
use strict;
use DBI;
my %config;
my $dbh;
use File::Basename;
use File::Copy;
my $DB = $ENV{‘DEBUG’};
#Get the date from the environment OR ARG[0]
my $QDate = $ENV{‘QDATE’} || $ARGV[0];
die “$0: QDATE not set nor ARG1 set.” unless $QDate;
.# Destination Directory for recordings
$config{‘destDir’} = “/var/spool/asterisk/monitor/”;
.# What app do we use to encode?
$config{‘LAME’} = which lame
;
chomp($config{‘LAME’});
die "$0: Lame not found " unless $config{‘LAME’};
#What app do we use to remove silence
$config{‘RMSILENCE’} = ‘/usr/local/bin/wav2mp3/rmsilenceWAV.sh’;
die “$0: $config{‘RMSILENCE’} not found.” unless -x $config{‘RMSILENCE’};
.# Read FreePBX configuration
open( CONFIG, “</etc/amportal.conf” ) or die(“Could not open /etc/amportal.conf. Aborting…”);
while () {
chomp;
$_ =~ s/^\s+//g;
$_ =~ s/([^#]*)#/$1/g;
$_ =~ s/\s+$//g;
if ($_ ne “”) {
my($key,$val) = split(/=/);
$config{$key}=$val;
}
}
$config{‘cdrHost’} = $config{‘CDRDBHOST’} ?$config{‘CDRDBHOST’}:$config{‘AMPDBHOST’};
$config{‘cdrDBName’} = ‘asteriskcdrdb’;
$config{‘cdrTableName’} = $config{‘CDRDBTABLENAME’} ?$config{‘CDRDBTABLENAME’}:‘cdr’;
$config{‘cdrUser’} = $config{‘CDRDBUSER’} ?$config{‘CDRDBUSER’}:$config{‘AMPDBUSER’};
$config{‘cdrPass’} = $config{‘CDRDBPASS’} ?$config{‘CDRDBPASS’}:$config{‘AMPDBPASS’};
$config{‘monitor_base’} = $config{‘MIXMON_DIR’} ? $config{‘MIXMON_DIR’} : $config{‘ASTSPOOLDIR’} . ‘/monitor’;
#my $uniqueid = $ARGV[0];
#my $directorio_archivo = $ARGV[1];
#my $type = $ARGV[2];
print “\n$=$config{$}” foreach qw/ cdrHost cdrDBName cdrTableName cdrUser cdrPass AMPDBHOST /;
sub connect_db() {
my $return = 0;
my %attr = (
PrintError => 0,
RaiseError => 0,
);
my $dsn = “DBI:mysql:database=$config{‘cdrDBName’};host=$config{‘AMPDBHOST’}”;
$dbh->disconnect if $dbh;
$dbh = DBI->connect( $dsn, $config{‘cdrUser’}, $config{‘cdrPass’}, %attr ) or $return = 1;
return $return;
}
my $time = localtime(time);
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
$mon++;
$year += 1900;
#Change upper time limit to 2 minutes back
my $Today = sprintf ("%4d-%02d-%02d",$year,$mon,$mday);
if ( $min < 2 ){
$hour–;
$min = ‘58’;
} else {
$min–;
}
my $Date2 = $Today . " " . sprintf("%02d:%02d:%02d",$hour,$min,‘0’) ;
# Connect to Database
&connect_db();
print “\nTODAY=$Today QDate=$QDate through $Date2”;
print “\nStarting …”;
my $query = “SELECT calldate,recordingfile,duration,cnam,lastapp
FROM $config{‘cdrTableName’}
WHERE DATE(calldate) = ‘$QDate’ AND calldate < ‘$Date2’ AND recordingfile > ’ ’ AND SUBSTRING(recordingfile, -4) = '.wav’
ORDER BY calldate
limit 4000”;
my $sth = $dbh->prepare($query) or die “SQL-do Error: $DBI::errstr\n”;
$sth->execute or die “SQL Error: $DBI::errstr\n”;
#Let recordings settle before we start if we are processing Today
sleep 10 if $Today eq $QDate;
my $NumRec = 0;
while( my $info = $sth->fetchrow_hashref){
$NumRec++;
my %i2 = %{$info}; #Easier to read
my @subdir = split( ‘-’, substr($i2{‘calldate’},0,10));
my $file = $config{‘monitor_base’} . ‘/’ . join (’/’ , @subdir) . “/$i2{‘recordingfile’}”;
$config{‘archiveDir’} = “/var/spool/asterisk/wavarchive/” . join (’/’ , @subdir) . ‘/’;
my ($archivo, $directorio, $suffix) = fileparse($file , ‘.wav’ );
printf “\n%5.0d %-60s " , $NumRec,”$archivo$suffix" ;#archive=$archivo directory=$directorio suffix=$suffix ";
next unless $suffix eq ‘.wav’;
#print " archive=$archivo directory=$directorio suffix=$suffix “;
if ( -f $file ){
mp3me($dbh,$file,$i2{‘recordingfile’},$i2{‘calldate’},$i2{‘cnam’},$i2{‘lastapp’});
print " Updated to MP3”;
} else {
print “Not Found! Duration $i2{‘duration’}”;
my $NewFile = $file;
$NewFile =~ s/.wav$/.mp3/;
if ( -f $NewFile ){
print “\n$file\n$NewFile\n OH an mp3 exists need to update the database record\n”;
my $Mquery = “
UPDATE cdr
SET recordingfile = REPLACE(recordingfile, ‘.wav’, ‘.mp3’)
WHERE calldate = ‘$i2{‘calldate’}’ AND recordingfile = '$i2{‘recordingfile’}'
LIMIT 3”;
print “\n$Mquery”;
my $numrows = $dbh->do($Mquery);
if ( not defined $numrows) {
print STDERR “ERROR: $DBI::errstr”;
} else {
print STDERR “\tINFO: $numrows rows updated”;
}
}
}
#print "$_ $info->{$_} " foreach keys %i2;
if ( &keystroke == 1 ) {
print “\nAbort. By ‘Q’”;
last;
}
}
$sth->finish();
$dbh->disconnect;
print “\nProcessed $NumRec records”;
exit 0;
sub mp3me{
my $DBH = shift;
my $File = shift;
my $recfile = shift;
my $calldate = shift;
my $lastapp = shift;
my $cnam = shift;
my $Desc = “$lastapp $cnam $calldate $recfile”;
$cnam =~ s/’//g;
$Desc =~ s/’//g;
my $Options = " --silent -m m --ta ‘$cnam PBX’ -b 16 --resample 44.1 “;
my $YEAROPT=”–ty " . substr($calldate,0,4);
my $Full = " $YEAROPT $Options --tt ‘$Desc’ --add-id3v2 $File";
my $NewFile = $File;
my ($Basename, $FullPath, $suffix) = fileparse($File , qr/.[^.]*/ );
$NewFile =~ s/.wav$/.mp3/;
die “Filename problem $NewFile $File” if $File eq $NewFile;
#$Full = " $File";
my ($atime, $mtime ) = (stat($File))[8,9];
print “\n\n $config{‘RMSILENCE’} $Full\n\n” if $DB;
print " rmsilence “;
system(”$config{‘RMSILENCE’} $File");
print “\n\n $config{‘LAME’} $Full\n\n” if $DB;
system("$config{‘LAME’} $Full “);
if ( -f $NewFile ){
print “\nSuccess. Lets move it to $config{‘archiveDir’}”;
system(“mkdir -p $config{‘archiveDir’}”);
#Change permissions on the newly minted .MP3 file
utime($atime,$mtime, $NewFile);
my $UID = getpwnam(‘root’) ;
my $GID = getgrnam(‘root’) ;
chown $UID, $GID, $NewFile;
chmod 0644 , $NewFile;
#Move the old wav file somewhere safe
move( $File , $config{‘archiveDir’} ) or die “Move did not succeed”;
die “No Recording files? Not possible” unless $recfile;
print " SQL”;
my $Mquery = “
UPDATE cdr
SET recordingfile = REPLACE(recordingfile, ‘.wav’, ‘.mp3’)
WHERE recordingfile = ‘$recfile’
”;
#WHERE calldate = ‘$calldate’ AND recordingfile = '$recfile’
my $numrows = $dbh->do($Mquery);
if ( not defined $numrows) {
print STDERR “ERROR: $DBI::errstr”;
}
} else {
die “\nFailure”;
}
print " … ";
#sleep 4 if -f ‘/tmp/lame-slow’ or $DB ;
}
sub keystroke {
my $i = ‘’;
vec($i, fileno(STDIN), 1) = 1;
my $j = select ($i, undef, undef, 0);
}
-That’s it in a nut shell. The Logs just show “1 file processed” because it isn’t an error. Any ideas would be appreciated.
-Christian