2

I'm trying to work out a method of capturing a live MP3 audio stream, i.e. internet radio/Icecast or Shoutcast, and saving the captured audio in defined file lengths, so 1 hour audio file, or 2 hour audio file.

I found something on this thread using bash and wget (Record radio / audio stream (asx/asf) with a webserver), but I want to build a PHP script that is more customisable, and I can trigger via cron job.

I figured using something like stream_get_contents(), but I've never used it before.

Any ideas?

Thanks

Community
  • 1
  • 1
mattauckland
  • 483
  • 1
  • 7
  • 17

2 Answers2

2

This is bash script that i have been using for a while

#!/bin/bash                                                                                                                                                                           

ls *asf &> /dev/null                                                            
if [[ $? -eq 0 ]]; then                                                         
    CNT=$(ls *asf | tail -1 | perl -p -i -e 's/recording_0*(\d*).*/$1/');  
else                                                                            
    CNT=0;                                                                      
fi                                                                              

while true; do                                                                  
    NAME=recording_$(printf "%03d" $CNT);                                  
    if [[ -f $NAME.asf ]]; then                                                 
        CNT=$[ $CNT + 1 ];                                                      
        continue;                                                               
    fi                                                                          
    echo "recording $NAME";                                                     
    mplayer "http://your.stream.goes.here" -dumpstream -dumpfile $NAME.asf &> $NAME.log &
    sleep 2h;                                                                   
    killall mplayer;                                                            
    CNT=$[ $CNT + 1 ];                                                          
done

this will produce recordings with the following names recording_000.asf recording_001.asf etc and also a log file with mplayers output (in case you need it). I have hardcoded the period into to 2h but you can easily change that.

user2599522
  • 3,005
  • 2
  • 23
  • 40
  • I did come across this on the site, but I'm looking for a pure PHP solution to the problem, as the security software I run on my servers restrict bash. Also as a pure PHP solution it can be worked into a much larger project. Thanks anyway :) – mattauckland Oct 24 '13 at 14:55
0

PHP supports the Ogg/Vorbis audio protocol for streaming as discussed here and here. In order to work MP3 into a stream, I suspect you will need to use the stream_wrapper_register function, access files like mp3://domain.com/myfile.mp3, and create a class to process the stream like the example at http://www.php.net/manual/en/function.stream-wrapper-register.php#77055.

Jeremy Harris
  • 24,318
  • 13
  • 79
  • 133
  • Thanks, but the clarify I want to capture an audio stream, not stream an audio file. I've amended the question to clear that up. – mattauckland Oct 24 '13 at 14:53