how to get the time length of a sound file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
chrismicro
Forum Newbie
Posts: 21
Joined: Fri Nov 12, 2010 7:03 am

how to get the time length of a sound file

Post 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'
 )"); 




?>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post by Celauran »

chrismicro
Forum Newbie
Posts: 21
Joined: Fri Nov 12, 2010 7:03 am

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

Post by chrismicro »

That sir is exactly what i need...

what would be the way to utilize this within my code?
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

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

Post 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.
chrismicro
Forum Newbie
Posts: 21
Joined: Fri Nov 12, 2010 7:03 am

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

Post 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
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

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

Post 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']);
chrismicro
Forum Newbie
Posts: 21
Joined: Fri Nov 12, 2010 7:03 am

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

Post 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'
 )"); 




?>
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

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

Post by Jonah Bron »

Did it work before you put the function in?
chrismicro
Forum Newbie
Posts: 21
Joined: Fri Nov 12, 2010 7:03 am

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

Post by chrismicro »

yes...perfectly
chrismicro
Forum Newbie
Posts: 21
Joined: Fri Nov 12, 2010 7:03 am

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

Post by chrismicro »

I got it working:)the above code I showed works......
Post Reply