LAI has asked for the wisdom of the Perl Monks concerning the following question:

I'm working on a really fun mini-project. Right now breaks in the warehouse at work are signaled by a .wav of a train piped over the PA system. The workers have been bitching about that train, and I decided to do something different. I spent a couple of hours yesterday collecting a few dozen .wav files and storing them on one of the web servers. I've written a hideously simple script to just choose one at random and pipe it out.

What I need, though, is to be able to output in mp3 format. The machine connected to the PA is a Windows box, and the sndrec32 program has no idea what a URL is. I wouldn't mind doing it with Winamp, but Winamp wants mp3s from its URLs. So far, I haven't been able to find a module or script that will easily encode mp3s on the fly, either here or on CPAN. Has anyone come across something that'll work?

Here's the script I hacked up (Yes, I actually get paid to do this!):

#!/usr/bin/perl -w use strict; open LS, "/bin/ls wav/*.wav |"; @::wavs = <LS>; close LS; local $::wav = $::wavs[rand $#::wavs]; spew_wav(); #spew_mp3(); #redir_wav(); sub spew_wav { print "Content-type: audio/x-wav \n\n"; open WAV,"/bin/cat $::wav |"; print while <WAV>; close WAV; } sub spew_mp3 { print "Content-type: audio/mpeg \n\n"; open WAV,"/usr/local/bin/bladeenc $::wav |"; print while <WAV>; close WAV; } sub redir_wav { print "Location: $::wav \n\n"; }

So I've got three ways of spewing out information. Right now, the two wav methods work fine, because it's just read, spew. Bladeenc doesn't work so well for the mp3ing, though, because the wavs have a few different frequencies and bladeenc wants to know what the frequency is. Also, I'd really like to be able to do the encoding using perl, rather than third-party software.


LAI
:eof

Replies are listed 'Best First'.
Re: On-the-fly Wav to mp3 encoding
by theorbtwo (Prior) on Nov 28, 2002 at 18:02 UTC

    BTW, you'd probably be much better off converting to mp3 offline and then using that, rather then reconverting over and over, which is a waste of CPU. For that matter, you'd probably be better off converting everything to mp3 and then converting to wav on the fly, if and when needed. Much less disk space and mp3->wav is a much less cpu-intensive process.


    Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).

      See, that crossed my mind... but I figured it made too much sense and therefore was not viable :o)

      Yes, that probably is what I will do if nothing else seems to work, although half the point of the exercise was to learn how to do something new. Oh, well.


      LAI
      :eof

        ...although half the point of the exercise was to learn how to do something new.

        Get a file with stupid quotes. Use MBROLA (http://tcts.fpms.ac.be/synthesis/) to produce a Text-To-Speech WAV file, using a different voice every time. Mix the file with your existing file. Encode as MP3. Stream.

        You don't have to solve easy tasks in a complex way. There are enough challenges out there. You just need to look for them.

        And for someone who pays you to do it.

Re: On-the-fly Wav to mp3 encoding
by hawtin (Prior) on Nov 28, 2002 at 17:37 UTC

    I needed to do a similar thing in the end I used a program called lame.exe and just had my perl script run it over the wav files

      The perl script is on a Linux server. It's accessed (ideally) through a Scheduled Task on a WinBox with a command something like:

      c:\Program Files\Winamp\Winamp.exe "http://media.ourcompany.com/rand.wav.pl"

      So as far as encoding stuff goes it should be done with linux-based tools.


      LAI
      :eof
Re: On-the-fly Wav to mp3 encoding
by valdez (Monsignor) on Nov 28, 2002 at 20:56 UTC

    A possible solution is Audio::MPEG that can convert from/to mp3 using lame library. That module, unfortunately, doesn't compile with latest release of lame (v 3.93).

    Ciao, Valerio

Re: On-the-fly Wav to mp3 encoding
by LAI (Hermit) on Nov 28, 2002 at 15:58 UTC

    Erm... minor correction: the "Location:" redirect isn't working either. Oops.


    LAI
    :eof