Click to See Complete Forum and Search --> : Getting Audio length
hifibee
12-12-2006, 07:29 AM
Hi all
Is there any way to get the length of the audio file (wav file). I am in need of showing the approximate time the wav file will be played.
Thanks,
netbuddy
12-12-2006, 02:54 PM
yeah, if you dig deep on the internet, you will find a php class for wav audio.
pcthug
12-12-2006, 06:46 PM
Well I search around for a while and couldn't find anything, so I ended up putting this together. It will return a 2 decimal float value in seconds.
/*
float getWavFilesize ( string file )
*/
function getWavFilesize($file)
{
if (strtoupper(pathinfo($file, PATHINFO_EXTENSION)) !== 'WAV')
{
trigger_error('File is not a Wav File.');
return false;
}
elseif (!$bin = @file_get_contents($file))
{
trigger_error('File either does not exist or is not readable.');
return false;
}
$wav['SubChunk1Size'] = unpack('V*', substr($bin, 16 ,4));
$wav['NumChannels'] = unpack('v*', substr($bin, 22, 2));
$wav['SampleRate'] = unpack('V*', substr($bin, 24, 4));
$wav['BitsPerSample'] = unpack('v*', substr($bin, 34, 2));
if ($wav['SubChunk1Size'][1] == 16)
{
$wav['SubChunk2Size'] = unpack('V*', substr($bin, 40, 4));
}
elseif ($wav['SubChunk1Size'][1] == 18)
{
$wav['SubChunk2Size'] = unpack('V*', substr($bin, 42, 4));
}
$wav['NumberSamples'] = ($wav['SubChunk2Size'][1] * 8) / ($wav['NumChannels'][1] * $wav['BitsPerSample'][1]);
return round($wav['NumberSamples'] / $wav['SampleRate'][1], 2);
}
hifibee
12-13-2006, 03:43 AM
Hi,
Thanks for ur reply. I've checked ur function with a sample audio file but it returned 0. i have also attached the sample audio file which i tested.
Thanks,
netbuddy
12-13-2006, 06:32 PM
http://www.builderau.com.au/program/php/soa/Create_an_audio_stitching_tool_in_PHP/0,339028448,339176704,00.htm
pcthug
12-14-2006, 02:07 AM
Here is an updated version. It is more scalable as it does not have defined SubChunkSize's.
/*
float getWavFilesize ( string file )
*/
function getWavFilesize($file)
{
if (strtoupper(pathinfo($file, PATHINFO_EXTENSION)) !== 'WAV')
{
trigger_error('File is not a Wav File.');
return false;
}
elseif (!$bin = @file_get_contents($file))
{
trigger_error('File either does not exist or is not readable.');
return false;
}
$wav['SubChunk1Size'] = unpack('V*', substr($bin, 16 ,4));
$wav['NumChannels'] = unpack('v*', substr($bin, 22, 2));
$wav['SampleRate'] = unpack('V*', substr($bin, 24, 4));
$wav['BitsPerSample'] = unpack('v*', substr($bin, 34, 2));
$wav['SubChunk2Size'] = unpack('V*', substr($bin, 24 + $wav['SubChunk1Size'][1], 4));
$wav['NumberSamples'] = ($wav['SubChunk2Size'][1] * 8) / ($wav['NumChannels'][1] * $wav['BitsPerSample'][1]);
return round($wav['NumberSamples'] / $wav['SampleRate'][1], 2);
}
hifibee
12-14-2006, 03:32 AM
Great
Its working fine. Thanks for your valuable support. thanks to netbuddy too.