Using LAME encoder from PHP exec() or shell exec()

software development

#1

I have LAME encoder successfully installed on my web hosting. I know this because if I use Terminal SSH: and run the following I can encode my .mp3 to .wav…

[code]export TMPDIR=$HOME/tmp
export PATH=$HOME/lame/bin:$PATH
export LD_LIBRARY_PATH=$HOME/lame/lib:/usr/local/lib:$LD_LIBRARY_PATH
export CPATH=$HOME/lame/include:/usr/local/include:$CPATH
export LIBRARY_PATH=$HOME/lame/lib:/usr/local/lib:$LIBRARY_PATH

lame -b128 mydomain.com/music/somemusic.mp3 mydomain.com/music/somemusic.wav
[/code]
So I tried a PHP script as follows and I am not having any success:

[code] <?php shell_exec(‘export TMPDIR=$HOME/tmp’);
shell_exec(‘export PATH=$HOME/lame/bin:$PATH’);
shell_exec(‘export LD_LIBRARY_PATH=$HOME/lame/lib:/usr/local/lib:$LD_LIBRARY_PATH’);
shell_exec(‘export CPATH=$HOME/lame/include:/usr/local/include:$CPATH’);
shell_exec(‘export LIBRARY_PATH=$HOME/lame/lib:/usr/local/lib:$LIBRARY_PATH’);

        shell_exec('lame -b128 mydomain.com/music/somemusic.mp3 mydomain.com/music/somemusic.wav');
    ?>[/code]

Could someone please advise me on a simple script to convert .mp3 to .wav using LAME encoder from PHP? Thank you :slight_smile:


#2

Typing on my phone, so sorry it’s brief and without example. But each shellexec() is executing in a different shell and the shell is closing or exiting on completion of the single command. I would put all your commands into one .sh file that you could then call with one shellexec()


#3

I can’t put all of my commands on a .sh line as I will be passing filenames into shell_exec() as PHP string variables. So, based off what you told me I tried the following:

[code]<?php

shell_exec('export TMPDIR=$HOME/tmp
	export PATH=$HOME/lame/bin:$PATH
	export LD_LIBRARY_PATH=$HOME/lame/lib:/usr/local/lib:$LD_LIBRARY_PATH
	export CPATH=$HOME/lame/include:/usr/local/include:$CPATH
	export LIBRARY_PATH=$HOME/lame/lib:/usr/local/lib:$LIBRARY_PATH

	lame -b128 mysite.com/music/somemusic.mp3 mysite.com/music/somemusic.wav');
	
	?>[/code]

No joy with the above code either.

Could you please provide me with a correct example of the proper syntax in order to make this happen. This is one little piece of a four part process and is holding me back from completing a very cool project. So fair I have not had much response here or other forums for that matter. I simply need the syntax to get this to work in PHP.

Thank you for anyone willing to offer any assistance.


#4

First of all, you don’t need the CPATH or LIBRARY_PATH environment variables to run lame. They’re only needed when compiling software.

Second of all, you don’t need /usr/local/lib in LD_LIBRARY_PATH. Normal system library paths like that are always searched, regardless of the value of the variable.

Third, the working directory for processes run by PHP is usually going to be the directory of the PHP script, not your home directory. If in doubt, use an absolute path.

Finally, the best way to set environment variables for a single command like this is using the “env” utility. Try something like:

[php]
shell_exec(‘env TMPDIR=$HOME/tmp PATH=$HOME/lame/bin:$PATH LD_LIBRARY_PATH=$HOME/lame/lib lame -b128 /home/you/mysite.com/music/somemusic.mp3 /home/you/mysite.com/music/somemusic.wav’);
[/php]

NB: ffmpeg is already installed on the server, and may be easier to use, e.g.

[php]
shell_exec(‘ffmpeg -ab 128k -i /path/to/input.wav /path/to/output.mp3’);
[/php]


#5

I had issues with the first example you sent for LAME so I used the second example for ffmpeg and it worked like a charm! Thank you so much for your assistance. My minor head ache has now been replaced with giddiness now that I am able to proceed with my project! :slight_smile: