secondsToHMS($seconds_to_add); list ($add_hours, $add_minutes, $add_seconds) = split(':', $add_time); $seconds = $cur_seconds + $add_seconds; if ($seconds >= 60) { $add_minutes++; $seconds = $seconds - 60; } $minutes = $cur_minutes + $add_minutes; if ($minutes >= 60) { $add_hours++; $minutes = $minutes - 60; } $hours = $cur_hours + $add_hours; $end_pos = "$hours:$minutes:$seconds"; echo "Adding $seconds_to_add seconds to $current_pos = $end_pos\n\n"; return $end_pos; } // This function may (and probably should) be overwritten by a customized class function getStreamURL($url) { return $url; } /* This function actually runs mplayer twice. The first time it saves the stream to /dev/null and just records to endpos 00:00:01, and grabs the audio position from the output. It then calculates the desired ending position, then runs mplayer a second time to save to the real file in wave format */ function save_stream($output_file = '') { $this->wavfile = ($output_file) ? $output_file : $this->tempfile('wav'); echo "Saving {$this->stream_url} to {$this->wavfile} for {$this->seconds} seconds\n\n"; // TODO: Better sanity check for $stream_url $safe_stream_url = addslashes($this->stream_url); $initial_mplayer_command = "{$this->mplayer_command} \"{$safe_stream_url}\" -nolirc -ao pcm:file=/dev/null -vc null -vo null -endpos 00:01:01"; echo "====================================================================================\n"; echo "About to exec initial mplayer_command: $initial_mplayer_command\n\n"; $output = array(); $last_line = exec($initial_mplayer_command, $output, $rc); $current_pos = ''; foreach ($output as $line) { //A:4269.7 ( 1:11:09.6) of 0.0 (unknown) ??,?% 12% if (preg_match("#A:[0-9\.]+ \(([0-9:\. ]+)\)#", $line, $matches)) { $current_pos = $matches[1]; echo "Found the Current Position at $current_pos\n\n"; } } if (!$current_pos) { echo "COULDN'T FIND CURRENT POSITION\n\n";print_r($output); return false; } $end_pos = $this->addTimes($current_pos, $this->seconds); echo "Calculated desired end_pos at $end_pos\n"; $mplayer_command = "{$this->mplayer_command} \"{$safe_stream_url}\" -nolirc -ao pcm:file={$this->wavfile} -vc null -vo null -endpos $end_pos"; echo "About to exec REAL mplayer_command:\n$mplayer_command\n\n"; $rc = system($mplayer_command, $output); echo "mplayer command returned $rc\n"; // TODO: check here if {$this->seconds} has elapsed, and restart mplayer and append // to current wave file if it isn't close return $rc; } function encode_to_mp3($output_file = '') { $this->mp3file = ($output_file) ? $output_file : $this->tempfile('mp3'); $lame_command = "{$this->lame_command} -m s {$this->wavfile} -o \"{$this->mp3file}\""; echo "About to exec $lame_command\n\n"; $rc = system($lame_command); echo "lame_command returned $rc\n"; return $rc; } function cleanup() { unlink($this->wavfile); unlink($this->mp3file); } } ?>