Jump to content

which function to read an executed program output


davidkierz

Recommended Posts

 

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

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 ???

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 #

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 ....

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.

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

 

 

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.