1

I've searched for help and tried everything on this thread, but still can't make this work. I'm trying to run the sox (Sound eXchange) command line utility from my PHP script using shell_exec(). I need to concatenate two audio files, both of which are in the same directory as the PHP script (i.e. accessible to apache).

Here's the confusion:

  1. Some sox commands work fine using shell_exec(). For example I can play an audio file or retrieve information about it.
  2. If I echo out the concatenation command (so I know exactly what PHP is sending to the shell) and then copy and paste it into a shell window, it runs perfectly.
  3. My apache user (_www) has full rights to the directory where the PHP script is, as well as the sox directory. I ran "chown -R _www:_www" and "chmod -R 777" on both directories.
  4. I'm using the full path to the sox executable and the audio files.

So this works in the shell:

"/soxpath/sox /filepath/file1.wav /filepath/file2.wav /filepath/combined.wav"

But this doesn't work from PHP:

shell_exec('/soxpath/sox /filepath/file1.wav /filepath/file2.wav /filepath/combined.wav');

Can anyone shed some light on this? What am I missing? Thanks.

Community
  • 1
  • 1
brawlins
  • 184
  • 2
  • 7
  • what does shell_exec() return ? –  Mar 21 '12 at 22:42
  • /soxpath/sox ???? is that the full path or sample .... – Baba Mar 21 '12 at 23:01
  • shell_exec() returns NULL. And "/soxpath/" is just a placeholder for the actual full path. In my case it's "/bin/sox-14.4.0/" – brawlins Mar 22 '12 at 00:14
  • how about: shell_exec('/soxpath/sox /filepath/file1.wav /filepath/file2.wav /filepath/combined.wav >file.txt'); anyhting appear in the file.txt? –  Mar 22 '12 at 01:57
  • That creates file.txt, but it's an empty file (size 0 bytes), and shell_exec() still returns NULL. – brawlins Mar 22 '12 at 02:34
  • It could be that this is an OS-specific issue (I'm running Mac OS 10.7.2). I read a few things about sox not working properly on Mac OS X, but that was a while back and they [released a version that supposedly fixed those problems](http://article.gmane.org/gmane.comp.audio.sox/3472/) over a year ago. I'll have to get our admin to install sox on our production server so I can test it there. – brawlins Mar 22 '12 at 02:43
  • Ok. It doesn't appear to be OS-specific. I have sox installed on our linux production server now and it's behaving the same way. From the PHP script shell_exec() returns NULL, but running the same command from the shell works fine. I'm out of ideas. If anyone has been able to use sox to combine files via a PHP script, I'd love to hear how you did it :) – brawlins Mar 23 '12 at 19:25

1 Answers1

0

Okay, I got it working finally so I thought I should clear up any confusion I created. The problem was something unrelated. So, yes, you can run sox commands from PHP using shell_exec().

I was simply running into a timing issue with javascript. I was using wami recorder to capture audio on the client side and then save the audio file on the server. So my PHP script was in the context of an ajax call handler, which by definition is asynchronous. Should have realized that earlier.

The issue was that the file was not done saving when the sox command to concatenate was run, so naturally it failed because the file didn't exist yet. When I made the call synchronous it worked.

brawlins
  • 184
  • 2
  • 7