Page 1 of 1

how to get the time length of a sound file

Posted: Tue Dec 14, 2010 2:16 pm
by chrismicro
I am using an upload script to upload sound files to a server, yet I need a way to do this all in 1 shot

basically once the file is selected from the browse button and upload is pressed, I need the php to upload the file, calculate how long the sound file is time wise.

for instance if test.wav is uploaded the script can tell how long the sound file is time wise

ex: test.wav = 42 seconds

how do I do this?

also once the file is uploaded I need it to refresh(getbyelementid) with a time showing and a dropdownbox of the file(s)...I am going to use ajax for this.

here is the current upload php script.

Code: Select all

<?php

include 'vdattcon.php';

 

$userid = $_POST["userid"];
$target_path = "/var/lib/asterisk/sounds/";
$backup_path = "/srv/www/htdocs/k2zwf6jks9yx6zhs6vr06842jj51fx/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
$backup_path .= basename( $_FILES['uploadedfile']['name']);
$filename = $_FILES['uploadedfile']['name'];
if (copy($_FILES['uploadedfile']['tmp_name'], $target_path) && copy($_FILES['uploadedfile']['tmp_name'], $backup_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']).
    " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";

}



//a. connect to audio att table db

 $connatt = mysql_connect($atthost, $attuser, $attpass) or die                      ('Error connecting to mysql audioatt');
mysql_select_db($attname);

//b. Connect insert filename in table

$resultuid = mysql_query("INSERT INTO audioatt VALUES (
'$uuuid',
'$userid',
'$filename'
 )"); 




?>

Re: how to get the time length of a sound file

Posted: Tue Dec 14, 2010 2:21 pm
by Celauran

Re: how to get the time length of a sound file

Posted: Tue Dec 14, 2010 2:56 pm
by chrismicro
That sir is exactly what i need...

what would be the way to utilize this within my code?

Re: how to get the time length of a sound file

Posted: Tue Dec 14, 2010 2:58 pm
by Jonah Bron
As you can see in the snippet, it's a function. All you have to do is place it in your code and call it, passing the path to the .WAV file.

Re: how to get the time length of a sound file

Posted: Tue Dec 14, 2010 3:00 pm
by chrismicro
I know its a function, I am just not sure where I would put the call of it within the code...somewhat confusing.

Please be more specific

Re: how to get the time length of a sound file

Posted: Tue Dec 14, 2010 3:28 pm
by Jonah Bron
If you want to check the length before moving the uploaded file, then like this:

Code: Select all

$length = wavDur($_FILES['uploadedfile']['tmp_name']);

Re: how to get the time length of a sound file

Posted: Tue Dec 14, 2010 3:46 pm
by chrismicro
Keeps giving me the error:

There was an error uploading the file, please try again!

Here is what I have it setup as:

Code: Select all

<?php

include 'vdattcon.php';


   
      function wavDur($file) {
   
      $fp = fopen($file, 'r');
   
      if (fread($fp,4) == "RIFF") {
  
      fseek($fp, 20);
   
      $rawheader = fread($fp, 16);
   
      $header = unpack('vtype/vchannels/Vsamplerate/Vbytespersec/valignment/vbits',$rawheader);
   
      $pos = ftell($fp);
   
      while (fread($fp,4) != "data" && !feof($fp)) {
   
      $pos++;
  
      fseek($fp,$pos);
  
      }
  
      $rawheader = fread($fp, 4);
  
      $data = unpack('Vdatasize',$rawheader);
  
      $sec = $data[datasize]/$header[bytespersec];
  
      $minutes = intval(($sec / 60) % 60);
  
      $seconds = intval($sec % 60);
  
      return str_pad($minutes,2,"0", STR_PAD_LEFT).":".str_pad($seconds,2,"0", STR_PAD_LEFT);
  
      }
  
      }


	  

$userid = $_POST["userid"];
$target_path = "/var/lib/asterisk/sounds/";
$backup_path = "/srv/www/htdocs/k2zwf6jks9yx6zhs6vr06842jj51fx/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
$backup_path .= basename( $_FILES['uploadedfile']['name']);
$filename = $_FILES['uploadedfile']['name'];
$length = wavDur($_FILES['uploadedfile']['tmp_name']);
if (copy($_FILES['uploadedfile']['tmp_name'], $target_path) && copy($_FILES['uploadedfile']['tmp_name'], $backup_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']).
    " has been uploaded<br><br>
	Duration:&nbsp;$length";
} else{
    echo "There was an error uploading the file, please try again!";

}



//a. connect to audio att table db

 $connatt = mysql_connect($atthost, $attuser, $attpass) or die                      ('Error connecting to mysql audioatt');
mysql_select_db($attname);

//b. Connect insert filename in table

$resultuid = mysql_query("INSERT INTO audioatt VALUES (
'$uuuid',
'$userid',
'$filename'
 )"); 




?>

Re: how to get the time length of a sound file

Posted: Tue Dec 14, 2010 4:19 pm
by Jonah Bron
Did it work before you put the function in?

Re: how to get the time length of a sound file

Posted: Tue Dec 14, 2010 4:22 pm
by chrismicro
yes...perfectly

Re: how to get the time length of a sound file

Posted: Tue Dec 14, 2010 4:29 pm
by chrismicro
I got it working:)the above code I showed works......