-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstreamconvert.php
117 lines (95 loc) · 3.56 KB
/
streamconvert.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
/*
This script, when installed on a webserver provides a means to convert
ogg internet radio streams to mp3.
One reason for doing a thing like this is hardware without ogg support,
like my Sonos solution.
This script depends on sox (http://sox.sourceforge.net) which must be
compiled with ogg and mp3 write support (LAME).
When installed, you can launch it either as
http://yourserver/oggconvert.php?url=http://ormgas.rainwave.cc/ormgas.m3u
to make the script output a m3u playlist pointing to itself or as
http://yourserver/oggconvert.php?url=http://ormgas.rainwave.cc/ormgas.m3u&play=1
to make the script start do the conversion.
The script looks at the content-type returned from the server after fetching
the document the url= parameter is pointing to. If it was a playlist, it
will try to convert any stream pointed to by the playlist. If it was
the ogg stream itself, it'll just convert that.
In any other case, it bails out with an error message.
In laymans terms this means that you can just put any URL remotely related
to an ogg stream after the url= and the script will try to do the right thing.
The script is (c) 2008 by Philip Hofstetter and licensed under the MIT license.
Any questions can be directed to [email protected]
*/
// the path to the MP3-Write-Enabled SOX
define('SOX_PATH', '/opt/sox/bin/sox');
if (empty($_GET['url'])){
die("no url to play provided. use ?url=url_of_stream to select the stream");
}
if ($_GET['play'] != '1'){
header('Content-Type: audio/x-mpegurl');
echo 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'].'?url='.rawurlencode($_GET['url'])."&play=1\n";
exit;
}
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"User-Agent: ogg2mp3 (gnegg.ch/ogg2mp3)\r\n"
)
);
$context = stream_context_create($opts);
$fh = fopen($_GET['url'], 'r', false, $context);
$header = array();
foreach($http_response_header as $line){
list($name, $content) = explode(': ', $line, 2);
$header[strtolower($name)] = $content;
}
switch($header['content-type']){
case 'audio/x-mpegurl':
$streams = fetchStreams($fh);
fclose($fh);
foreach($streams as $url) convertStreamUrl($url, $context);
break;
case 'application/ogg':
convertStream($fh);
break;
default:
header('Content-Type: text/plain');
die("Server returned unknown Content-Type: ".$header['content-type']);
}
function fetchStreams($fh){
$streams = array();
while (!feof($fh)){
$line = trim(fgets($fh));
if (empty($line)) continue; // skip empty lines
if (preg_match('%^\s*#%', $line)) continue; // skip comments
$streams[] = $line;
}
return $streams;
}
function convertStreamUrl($url, $context){
if ( ($fh = fopen(trim($url), 'r', false, $context)) === false) return false;
convertStream($fh); // never returns
return true;
}
function convertStream($fh){
$descspec = array( 0 => array('pipe', 'r'),
1 => array('pipe', 'w'),
2 => array('file', '/tmp/oggerr.log', 'a')
);
$pipes = array();
$cmd = SOX_PATH.' -t ogg - -t mp3 -';
$p = proc_open($cmd, $descspec, $pipes, '/tmp', $_ENV);
if (!$p) die("cannot open sox\n");
header('Content-Type: audio/mpeg');
stream_set_blocking($pipes[1], 0);
while (!feof($fh)){
fwrite($pipes[0], fread($fh, 8192));
echo fread($pipes[1], 8192);
}
fclose($pipes[0]);
fclose($pipes[1]);
proc_close($p);
return true;
}
?>