Hello PHP people 🙂

I have a tough one here, I wrote a content script that plays media files,
but I can't seem to get things to actually buffer and stream as they should.
Here's the conundrum:

http://server/media/file.asf - Media player will buffer and start playing
before download completes
http://server/content.php?f=media/file.asf - Media player has to download
entire file before it starts trying to play

Here's the source of content.php, anyone see anything obvious that I'm
missing? I've been all over the HTTP/1.1 spec and media RFCs trying to
figure out what the heck I'm missing. Windows Media Player interprets the
media name as "content" if that's any help. Alse, the mime types are all
correct so that's not the issue, I'm just trying to figure out how to make
the players buffer+play instead of download+play.

<?php
$mime_type = strtolower(strrchr($f,'.'));
$mime_type_array = array(
'.asf' => 'application/vnd.ms-asf',
'.avi' => 'video/x-msvideo',
'.gif' => 'image/gif',
'.jpg' => 'image/jpeg',
'.mov' => 'video/quicktime',
'.mpe' => 'video/mpeg',
'.mpeg' => 'video/mpeg',
'.mpg' => 'video/mpeg',
'.ra' => 'audio/x-pn-realaudio',
'.ram' => 'audio/x-pn-realaudio',
'.rm' => 'audio/x-pn-realaudio',
'.wmv' => 'audio/x-ms-wmv'
);

// this is our security, bleh
if(!in_array($mime_type,array_keys($mime_type_array)))
{
header("Location: /error.php");
}
$filename = '/path/to/'.$f;
$dlname = substr(strrchr($filename,'/'),1);
$offset = (isset($nocache)?0🙁86400 * 3));
header("Accept-Ranges: bytes");
header("Expires: ".gmdate("D, d M Y H:i:s \G\M\T", time() + $offset));
header("Cache-Control: max-age=".$offset);
header("Last-modified : ".gmdate("D, d M Y H:i:s \G\M\T",
filemtime($filename)));
header("Content-Length: ".filesize($filename));
header("Content-Disposition: filename=$dlname");
if($debugx==1)
{
phpinfo();
}
else
{
header("Content-Type: ".$mime_type_array[$mime_type]);
@readfile($filename);
}
?>

Thanks in advance for any help,
Please cc me on any replies since I am not on this mailing list.

Stephen VanDyke

    Hi!

    Doesn't one have to get a streaming server (or server library) to be able to stream files over the internet. At least this is how Real Player works.

    Or am I wrong?

    Best,
    Stas

      5 months later

      I just got this to work a few days ago.

      The trick is to send a play list that includes a link or links to the actual media files.

      Here's a sample php script called stream.php:

      <?
      header("Content-type: audio/x-mpegurl");
      header("Content-Disposition: filename=$playlist");
      include $playlist;
      ?>

      I call it as follows:

      http://mydomain.com/stream.php?playlist=lesson01.m3u

      And my playlist (lesson01.m3u) looks like this:

      http://mydomain.com/ogg_files/file01.ogg
      http://mydomain.com/ogg_files/file02.ogg
      http://mydomain.com/ogg_files/file03.ogg

      This will cause file01.ogg, file02.ogg, and file03.ogg to be streamed to the client's computer in turn.

      Also, neither winamp nor xmms seem to mind that I'm sending them ogg files, instead of mpeg files as advertised in the mime headers!

      The bottom line is that you don't need shoutcast, icecast, or anything else beyond Apache and php to do streaming media. I don't know how efficient this method is under load, but it seems to be working.

      I hope this helps!

      Rick

        Write a Reply...