Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Convert WAV to MP3 through a PHP interface?

Credits go to Arthur Kalliokoski, kazzmir, Matthew Leverton, and OICW for helping out!
This thread is locked; no one can reply to it. rss feed Print
 1   2 
Convert WAV to MP3 through a PHP interface?
Mark Oates
Member #1,146
March 2001
avatar

Hey Guys,

<background>
I'm wanting to streamline some particular component of my work, and it's lead me to some unfamiliar territory. This is something I started to work on about 3 years ago but didn't fully implement.
</background>

I want to convert a WAV file to a MP3 file through a php interface. Assuming the file has already been uploaded, I'm guessing I'll need to run some kind of Linux application on the server using exec(). Since I'm not a Linux user, my question is how do I get a program that I need (will I need to compile something?) onto the server so that it works? Or is there a better way?

I don't anticipate any restrictions on running an executable. I ran the script:

<?php
// outputs the username that owns the running php/httpd process
// (on a system with the "whoami" executable in the path)
echo exec('whoami');
?>

and it output the correct information, so I'll assume the executable functionality is working on my host.

--
Visit CLUBCATT.com for cat shirts, cat mugs, puzzles, art and more <-- coupon code ALLEGRO4LIFE at checkout and get $3 off any order of 3 or more items!

AllegroFlareAllegroFlare DocsAllegroFlare GitHub

kazzmir
Member #1,786
December 2001
avatar

Use the lame mp3 encoder.

$ lame foo.wav bar.mp3

Mark Oates
Member #1,146
March 2001
avatar

Is a linux executable also a .exe file?
(I'm looking at these precompiled binaries)

--
Visit CLUBCATT.com for cat shirts, cat mugs, puzzles, art and more <-- coupon code ALLEGRO4LIFE at checkout and get $3 off any order of 3 or more items!

AllegroFlareAllegroFlare DocsAllegroFlare GitHub

kazzmir
Member #1,786
December 2001
avatar

No, .exe is for windows. There is a project that can run windows executables on linux, called wine, but you probably don't want that.

Do you know what sort of linux server you have, as in what distrobution its running? If you can install stuff on the server then you can try one of these commands

Red hat

$ sudo yum install lame

Debian/ubuntu

$ sudo apt-get install lame

Gentoo (unlikely)

$ sudo emerge lame

Otherwise you can download the lame sources and build it locally. Probably something like

$ tar -xf lame.tar.gz
$ cd lame
lame $ ./configure --prefix=~/local && make && make install

Mark Oates
Member #1,146
March 2001
avatar

kazzmir said:

Do you know what sort of linux server you have, as in what distrobution its running?

I have no idea. I'm using 1and1.com hosting and I just see "Linux Hosting". Everything is remote so I have no interface other than exec()

--
Visit CLUBCATT.com for cat shirts, cat mugs, puzzles, art and more <-- coupon code ALLEGRO4LIFE at checkout and get $3 off any order of 3 or more items!

AllegroFlareAllegroFlare DocsAllegroFlare GitHub

kazzmir
Member #1,786
December 2001
avatar

Get an ssh client like putty, http://putty.very.rulez.org/, and try to connect to your host using the same username/password that you use for ftp (presumably you use ftp to transfer new html/php files to the host).

If that works then you can run those commands.

<edit> Look through this page for more info: http://faq.1and1.com/scripting_languages_supported/ssh_secure_shell/index.html

Arthur Kalliokoski
Second in Command
February 2005
avatar

I've used sox and ffmpeg to convert files to/from different audio formats. Try a "whereis sox" and "whereis ffmpeg" to see if they're installed.

They all watch too much MSNBC... they get ideas.

Mark Oates
Member #1,146
March 2001
avatar

ok, sox and ffmpeg are not installed.

I've downloaded and unzipped lame via get and tar and am now trying to install it but got an error "-bash: lame: command not found". lame.bat is in that directory.

{"name":"601330","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/1\/2\/1223a6720681af5fcbd98d73bb77f276.png","w":694,"h":124,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/1\/2\/1223a6720681af5fcbd98d73bb77f276"}601330

--
Visit CLUBCATT.com for cat shirts, cat mugs, puzzles, art and more <-- coupon code ALLEGRO4LIFE at checkout and get $3 off any order of 3 or more items!

AllegroFlareAllegroFlare DocsAllegroFlare GitHub

Matthew Leverton
Supreme Loser
January 1999
avatar

Just do ./configure --prefix=~/local && make && make install

Arthur Kalliokoski
Second in Command
February 2005
avatar

A file named "lame.bat" sounds like a Windows thing. Use "ls -l" to see which files are executable (the -rwxr-xr-x stuff, the 'x's mean it's executable).

They all watch too much MSNBC... they get ideas.

Mark Oates
Member #1,146
March 2001
avatar

now I get "configure: error: expected an absolute directory name for --prefix: ~/local"

{"name":"601331","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/b\/a\/bae2283dbf42f7061dcdb9208e69c814.png","w":694,"h":124,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/b\/a\/bae2283dbf42f7061dcdb9208e69c814"}601331

--
Visit CLUBCATT.com for cat shirts, cat mugs, puzzles, art and more <-- coupon code ALLEGRO4LIFE at checkout and get $3 off any order of 3 or more items!

AllegroFlareAllegroFlare DocsAllegroFlare GitHub

Matthew Leverton
Supreme Loser
January 1999
avatar

Then replace ~ with your absolute path to your home directory.

Highlighting text in putty automatically copies it...

Mark Oates
Member #1,146
March 2001
avatar

Highlighting text in putty automatically copies it...

nifty.

Ok, I found my abs path with <?php echo realpath(''); ?>, ran the command, a bunch of stuff flew by, and it looked like it compiled. It took about 40 seconds.

I'm assuming it installed correctly as I didn't see any error messages, but I'm not able to use lame as a command.

(uiserver):u49203781:~/lame-3.98.4 > % lame source_file.wav output_file.mp3
-bash: fg: %: no such job

I tried "whereis lame" and it returned "lame:", and made sure source_file.wav is in the lame-3.98.4 directory, but no change.

--
Visit CLUBCATT.com for cat shirts, cat mugs, puzzles, art and more <-- coupon code ALLEGRO4LIFE at checkout and get $3 off any order of 3 or more items!

AllegroFlareAllegroFlare DocsAllegroFlare GitHub

Arthur Kalliokoski
Second in Command
February 2005
avatar

Try a "locate lame | grep bin" and see if that's in the stuff that "set | grep PATH" shows. You might have to open ~/.bashrc (or create it) and insert the line
'export PATH="$PATH:<path to lame>"'

[EDIT]

Doing it this way would enable you to just type "lame <options>" wherever you were in the directory tree.

They all watch too much MSNBC... they get ideas.

Matthew Leverton
Supreme Loser
January 1999
avatar

If in same directory:
./lame source_file.wav output_file.mp3

Else:
~/local/bin/lame source_file.wav output_file.mp3

Or wherever you put it.

kazzmir
Member #1,786
December 2001
avatar

Sorry, I should have been more explicit. When I wrote

$ lame foo.wav bar.mp3

The first '$' just signifies that you are at a shell, you don't actually type it. Furthermore, anything that comes before the $ is just extra information, like your current directory. So putting 'lame' before the $ means you are in the 'lame' directory.

lame $ ./configure ...

Some shells use a different character than $. In your case you have a >, so mentally replace $ with > whenever necessary.

Mark Oates
Member #1,146
March 2001
avatar

Alrightie, I have successfully converted a wav to mp3!

the command I used was:
~/local/bin/lame ./source_file.wav ./output_file.mp3
and I was in the directory containing source_file.wav (obviously)

just for the record, these were some key points I learned:

  • ~ is the root (not the absolute path root, however)

  • . is the current directory

  • installed executables aren't automatically added to any environment-type path.

  • the lame executable is located in the local/bin directory, not the directory that I created when I unzipped the lame.tar.gz file. I guess that any executables that I install should also be located in the bin directory.

and now...
<?php exec("~/local/bin/lame ./source_file.wav ./destination_file.mp3"); ?>
and it works!

Thanks a lot, everybody.

Also, I can now delete the lame.tar.gz and all the stuff in the lame-3.98.4, right? So it's just like I downloaded an installer, like I would on Windows, and now that it's installed I can erase it, right?

I'm a newb, in this territory, btw. ;)

--
Visit CLUBCATT.com for cat shirts, cat mugs, puzzles, art and more <-- coupon code ALLEGRO4LIFE at checkout and get $3 off any order of 3 or more items!

AllegroFlareAllegroFlare DocsAllegroFlare GitHub

Arthur Kalliokoski
Second in Command
February 2005
avatar

~ is the root (not the absolute path root, however)

You should refer to that as your "home" directory.

They all watch too much MSNBC... they get ideas.

Matthew Leverton
Supreme Loser
January 1999
avatar

Quote:

<?php exec("~/local/bin/lame ./source_file.wav ./destination_file.mp3"); ?>

Arguments are understood to be relative to the current directory, so the ./ is extraneous on them. The point worth noting is that the local directory is not in the search path for executables unless you explicitly add it. (And few people rarely do that.)

So it's just like I downloaded an installer, like I would on Windows, and now that it's installed I can erase it, right?

It's just like you downloaded the source, compiled it, and moved the executable to another directory on Windows. ;) So yes, you can delete the source and the tar file.

If you had root privileges, you wouldn't have had to set the prefix to a local directory, and it would have been installed in /usr/local/bin which would have been part of the search path.

OICW
Member #4,069
November 2003
avatar

Quite late I presume, but you could also have a look on php5-ffmpeg module, which allows you to manipulate audio/video within the php code. I would expect that you could convert .wav to .mp3 with that as well. But it depends on two things - one if you are able to install new php modules on your server, two if the module does have that functionality.

That would be my 0.02$, I remembered that I've seen this module in repository few days ago when reinstalling ffmpeg. I don't have any experience with it, but could be helpful.

[My website][CppReference][Pixelate][Allegators worldwide][Who's online]
"Final Fantasy XIV, I feel that anything I could say will be repeating myself, so I'm just gonna express my feelings with a strangled noise from the back of my throat. Graaarghhhh..." - Yahtzee
"Uhm... this is a.cc. Did you honestly think this thread WOULDN'T be derailed and ruined?" - BAF
"You can discuss it, you can dislike it, you can disagree with it, but that's all what you can do with it"

Matthew Leverton
Supreme Loser
January 1999
avatar

I think that extension is only useful for querying media information.

Mark Oates
Member #1,146
March 2001
avatar

It's just like you downloaded the source, compiled it, and moved the executable to another directory on Windows. ;)

That was kind of fun. Any other useful command-line Linux apps out there I should know about?

Quote:

If you had root privileges, you wouldn't have had to set the prefix to a local directory, and it would have been installed in /usr/local/bin which would have been part of the search path.

Ok, that makes sense.

Thanks again.

[edit] A little foot note, I set it up to run as a background process (via this tutorial)

--
Visit CLUBCATT.com for cat shirts, cat mugs, puzzles, art and more <-- coupon code ALLEGRO4LIFE at checkout and get $3 off any order of 3 or more items!

AllegroFlareAllegroFlare DocsAllegroFlare GitHub

LennyLen
Member #5,313
December 2004
avatar

Any other useful command-line Linux apps out there I should know about?

xeyes

BAF
Member #2,981
December 2002
avatar

xeyes isn't a command-line app, it's an X app.

Thomas Fjellstrom
Member #476
June 2000
avatar

Any other useful command-line Linux apps out there I should know about?

Um, it really depends on what you want to do. Most of them have specific purposes.

BAF said:

xeyes isn't a command-line app, it's an X app.

Over ssh, hopefully X forwarding is on, and it'd work just fine ;D not that you'd want to.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

 1   2 


Go to: