3 * Transcode audio file URLs to .mp3 and stream it while
4 * transcoding is in progress.
6 //require_once __DIR__ . '/../src/header.php';
8 if (!isset($_GET['url'])) {
9 errorOut('url parameter missing');
11 $parts = parse_url($_GET['url']);
12 if ($parts === false || !isset($parts['scheme'])) {
13 errorOut('Invalid URL');
15 if ($parts['scheme'] !== 'http' && $parts['scheme'] !== 'https') {
16 errorOut('URL is neither http nor https');
22 . ' -i ' . escapeshellarg($url)
26 $descriptorspec = array(
27 1 => array('pipe', 'w'),// stdout is a pipe that the child will write to
28 2 => array('pipe', 'w')//stderr
31 register_shutdown_function('shutdown');
33 $process = proc_open($cmd, $descriptorspec, $pipes);
34 if (is_resource($process)) {
35 header('Content-type: audio/mpeg');
36 while ($data = fread($pipes[1], 10000)) {
39 //TODO: maybe flush() and ob_flush();
42 $errors = stream_get_contents($pipes[2]);
45 $retval = proc_close($process);
48 header('HTTP/1.0 500 Internal Server Error');
49 header('Content-type: text/plain');
50 echo "Error transcoding\n";
57 global $process, $pipes;
59 if (connection_aborted()) {
60 //end ffmpeg and clean temp file
63 proc_terminate($process);
67 function errorOut($msg)
69 header('HTTP/1.0 400 Bad request');
70 header('Content-type: text/plain');