davidkierz Posted November 28, 2007 Share Posted November 28, 2007 here is what i want to do figre out how many seconds long a audio file is # /usr/bin/sox 20071120-014450.ogg -e stat Samples read: 12455072 Length (seconds): 778.442000 Scaled by: 2147483647.0 Maximum amplitude: 0.282318 Minimum amplitude: -0.291840 Midline amplitude: -0.004761 Mean norm: 0.001863 Mean amplitude: -0.000001 RMS amplitude: 0.006938 Maximum delta: 0.147583 Minimum delta: 0.000000 Mean delta: 0.001264 RMS delta: 0.004260 Rough frequency: 1563 Volume adjustment: 3.427 so i an trying to use $sox = popen('/usr/bin/sox 20071120-014450.ogg -e stat',"r"); notice this command takes about 5 seconds to run on the my system.... anyway The only data i want to extract is the length in seconds in shell script i could just | grep seconds...... but im at a loss as to what type of varialbe $sox is or how to manuilapte it as a string.... I am n0bie but i was up all night tryin to figure out this so let me know thanks Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted November 28, 2007 Share Posted November 28, 2007 exec('/usr/bin/sox 20071120-014450.ogg -e stat',$p); print_r($p); try that Quote Link to comment Share on other sites More sharing options...
davidkierz Posted November 28, 2007 Author Share Posted November 28, 2007 firstly #!/usr/bin/php <?php $sox = popen('/usr/bin/sox 20071120-014450.ogg -e stat',"r"); ?> returns sbin # raptest.php Samples read: 12455072 Length (seconds): 778.442000 Scaled by: 2147483647.0 Maximum amplitude: 0.282318 Minimum amplitude: -0.291840 Midline amplitude: -0.004761 Mean norm: 0.001863 Mean amplitude: -0.000001 RMS amplitude: 0.006938 Maximum delta: 0.147583 Minimum delta: 0.000000 Mean delta: 0.001264 RMS delta: 0.004260 Rough frequency: 1563 Volume adjustment: 3.427 i never asked to print sox output to stdout but it does anyway, cant i store each line of output in a array somehow? then #!/usr/bin/php <?php $sox = popen('/usr/bin/sox 20071120-014450.ogg -e stat',"r"); print_r($sox); ?> sbin # raptest.php Resource id #4Samples read: 12455072 Length (seconds): 778.442000 Scaled by: 2147483647.0 Maximum amplitude: 0.282318 Minimum amplitude: -0.291840 Midline amplitude: -0.004761 Mean norm: 0.001863 Mean amplitude: -0.000001 RMS amplitude: 0.006938 Maximum delta: 0.147583 Minimum delta: 0.000000 Mean delta: 0.001264 RMS delta: 0.004260 Rough frequency: 1563 Volume adjustment: 3.427 i get a resource id #4 error i think thats a mismatch data type ??? Quote Link to comment Share on other sites More sharing options...
trq Posted November 28, 2007 Share Posted November 28, 2007 Better still might be.... <?php exec('/usr/bin/sox 20071120-014450.ogg -e stat | grep seconds | awk \'{print $2}\'', $p); echo $p[0]; ?> Quote Link to comment Share on other sites More sharing options...
davidkierz Posted November 28, 2007 Author Share Posted November 28, 2007 ok i tried that...... #!/usr/bin/php <?php exec('/usr/bin/sox 20071120-014450.ogg -e stat | grep seconds | awk \'{print $2}\'', $p); echo $p[0]; ?> but it just prints out the entire output and a blank line for $p sbin # raptest.php Samples read: 12455072 Length (seconds): 778.442000 Scaled by: 2147483647.0 Maximum amplitude: 0.282318 Minimum amplitude: -0.291840 Midline amplitude: -0.004761 Mean norm: 0.001863 Mean amplitude: -0.000001 RMS amplitude: 0.006938 Maximum delta: 0.147583 Minimum delta: 0.000000 Mean delta: 0.001264 RMS delta: 0.004260 Rough frequency: 1563 Volume adjustment: 3.427 sbin # Quote Link to comment Share on other sites More sharing options...
trq Posted November 28, 2007 Share Posted November 28, 2007 If this is a shell script (hence the shebang) why use php at all? Anyway, my mistake, try.... <?php $return = shell_exec('/usr/bin/sox 20071120-014450.ogg -e stat | grep seconds | awk \'{print $2}\''); echo $return; ?> Quote Link to comment Share on other sites More sharing options...
davidkierz Posted November 28, 2007 Author Share Posted November 28, 2007 my project is all php so i want it to be consistent #!/usr/bin/php <?php $return = shell_exec('/usr/bin/sox 20071120-014450.ogg -e stat | grep seconds | awk \'{print $2}\''); echo "return = "; echo $return; echo "\n"; echo "vartype ="; echo gettype($return); echo "\n"; ?> returns sbin # raptest.php Samples read: 12455072 Length (seconds): 778.442000 Scaled by: 2147483647.0 Maximum amplitude: 0.282318 Minimum amplitude: -0.291840 Midline amplitude: -0.004761 Mean norm: 0.001863 Mean amplitude: -0.000001 RMS amplitude: 0.006938 Maximum delta: 0.147583 Minimum delta: 0.000000 Mean delta: 0.001264 RMS delta: 0.004260 Rough frequency: 1563 Volume adjustment: 3.427 return = vartype =NULL I have a feeling this is going to get more complicated .... Quote Link to comment Share on other sites More sharing options...
trq Posted November 28, 2007 Share Posted November 28, 2007 That makes no sense. shell_exec returns a string. it does not output anything. Quote Link to comment Share on other sites More sharing options...
davidkierz Posted November 28, 2007 Author Share Posted November 28, 2007 pardon, me but are you saying there is something wrong with the code or that i am not understanding correctly? Quote Link to comment Share on other sites More sharing options...
davidkierz Posted November 28, 2007 Author Share Posted November 28, 2007 so any ideas anyone? Quote Link to comment Share on other sites More sharing options...
trq Posted November 28, 2007 Share Posted November 28, 2007 Sorry, I actually see one small error with the code... should be..... #!/usr/bin/php <?php $return = shell_exec('/usr/bin/sox 20071120-014450.ogg -e stat | grep seconds | awk \'{print $3}\''); echo "return = "; echo $return; echo "\n"; echo "vartype ="; echo gettype($return); echo "\n"; ?> I always forget that awk columns start at 1 not 0 (i get used to arrays). But anyway, other than that, there is no problem and that code should not output what you say it does. I do not have sox installed but as a test have used your output in a file named.... sox.out Samples read: 12455072 Length (seconds): 778.442000 Scaled by: 2147483647.0 Maximum amplitude: 0.282318 Minimum amplitude: -0.291840 Midline amplitude: -0.004761 Mean norm: 0.001863 Mean amplitude: -0.000001 RMS amplitude: 0.006938 Maximum delta: 0.147583 Minimum delta: 0.000000 Mean delta: 0.001264 RMS delta: 0.004260 Rough frequency: 1563 Volume adjustment: 3.427 I then have a script called.... sox_test.php #!/usr/bin/php <?php $return = shell_exec('cat sox.out | grep seconds | awk \'{print $3}\''); echo $return; ?> This.... $ ./sox_test.php returns.... 778.442000 exactly as I would expect. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted November 28, 2007 Share Posted November 28, 2007 I downloaded and installed the sox program on my host Linux box. It doesn't seem to obey the "normal" Unix style output conventions. If I do either of the following at the shell prompt, the output always shows on my screen and nothing is in the file "x": $ sox filename -e stat > x $ sox filename -e stat | cat > x Ken Quote Link to comment Share on other sites More sharing options...
davidkierz Posted November 28, 2007 Author Share Posted November 28, 2007 can anyone suggest a alternative program to sox for determining the audio length of a audio file Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted November 28, 2007 Share Posted November 28, 2007 This works on my host: <?php $return = shell_exec('sox monkey.au -e stat >& tmp | grep seconds tmp | awk \'{print $3}\''); echo "return = "; echo $return; echo "\n"; echo "vartype ="; echo gettype($return); echo "\n"; ?> It seems the output from the "-e stat" option goes to the error output, not the standard output. That's what the ">&" redirection does... Ken Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.