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

This is a continuation of the previous node, posted as a new node so people will actually see it (as the old one has dropped off newest node.) Anyways.

From my last node, i received a lot of responses detailing how i could interface with winamp. While this would probably work very nicely, during my researches i found mpg123 (and now having a version that works) i've become slightly obsessed with just using a simple command line program to play mp3s, one that would 'waste' all the resources that using winamp would entail. My first idea was that i could make a primitive stop/start system just by calling mpg123 with the name of the song, then killing the process when i want it to stop. However, the process will not die when i manually start it in the background (mpg123 /mp3/X.mp3 &), then any attempt to call kill on its process id meets with utter failure. However, when i start it in 'foreground' mode (Without the apersand) then pressing ctrl-c will kill it nicely. Which leads me to

Question one: Is there anyway i could easily start/stop a process from perl?

However after my attempts of that nature failed, i hit upon the idea of attempting to just feed the mp3 player via stdin. However, then i hit a new snag, namely perl will not print an mp3 file. Every attempt to read a mp3 file and print it, either to a file or to the console or whatever, has met with complete and utter failure. In a response to my previous node, someone mentioned something about control charcters in the mp3 file. Which is thus

Question two: Is there anyway to convince perl to read/write mp3 files without dying horribly?

Replies are listed 'Best First'.
Re: Mp3 PLayer. Part Two!
by panix (Monk) on Sep 20, 2002 at 01:23 UTC
    Question one: Is there anyway i could easily start/stop a process from perl?

    Isn't the problem that you don't know the pid to kill?

    Just a suggestion - maybe it would be easier to have perl streaming the current mp3 to a fifo, and have mpg123 play that? See the .signature example in perlipc for a working example.

    Question two: Is there anyway to convince perl to read/write mp3 files without dying

    You were already on the right track :)

    Here's an example of what I meant in 199077:

    adam@2cb:~$ perl -e 'open F, "nofx - falling in love.mp3"; binmode F; +open G, ">bleh"; binmode G; while(<F>) { print G; } ' adam@2cb:~$ ls -la nofx\ -\ falling\ in\ love.mp3 bleh -rw-r--r-- 1 adam users 5011330 Sep 20 13:00 bleh -rw-r--r-- 1 adam users 5011330 Jan 24 2002 nofx\ -\ falli +ng\ in\ love.mp3 adam@2cb:~$ cksum nofx\ -\ falling\ in\ love.mp3 bleh 2095826934 5011330 nofx - falling in love.mp3 2095826934 5011330 bleh

    The original/new files end up exactly the same - perl is handling the binary data just fine.

    update: added binmode G;. example was only working without it because i'm a linux weenie?

      Don't you need a binmode G; in there?

      >>"Isn't the problem that you don't know the pid to kill?"
      Er, when i call kill on the pid for mpg123, nothing happens, it just continues on merrily. (maybe b/c its cygwin or something)

      As to the printing thing, you're right, i just didnt have enough binmodes in there. I'll try again using binmode.

      And quick update to my root node, where it says "one that would 'waste' all the resources", should read "would not waste..", as that actually makes sense.
        Sounds like you are using Win9x instead of a flavor of NT... Kill does not work on 9x flavors of Windows.