...

View Full Version : AVI Streaming using PHP



Derek_M
08-09-2007, 11:57 AM
Hello,

I've been trying to have a PHP file give the location/open/read (whichever, as this is the issue) with fopen(), fread(), etc... but with the DiVX web player, it is hard.

Here's my situation. I'm trying to keep the location of the video(s) secret by having the source read by a .php file (source="file.php"). I've tried using the fopen() and fread() solutions, but the Divx player doesnt want to "connect".

Now, I had to define the file via file.php?video=file.avi (since the player won't bother if it doesnt have a .avi or .divx extension), tried having the script run in the object/embed code... but it just stays on "Connecting..." in the player.

All sources are correct, it just doesn't want to work. Anyone have any solution for what should be in the php file or another way to "hide" the source location?

Thank you.

BatCountry
08-09-2007, 01:22 PM
Hello,
I've been trying to have a PHP file give the location/open/read (whichever, as Thank you.
It's possible, but you would need to be running Apache (server) and the server would have to have a special mod installed. You could probably do it with php but the problem is the cpu usage would be through the roof.

Here's how it would work with php:

1) - PHP File opens avi file,
2) - reads first 500kb-stores last line it read and spits out data read.
3) - reads next 500kb starting from previous last line, stores new last line, spits out data.
4) - repeat step 3 till file is finished, close file.

The hardest part would be getting the flash file to accept that small chunk of data your sending it and for it to properly decipher it, you'd have to use wmv or flv, you can't use an avi I don't think.

Also notice that steps 2 and 3 are continuous meaning your server would be thrashed with requests, since every few seconds it's getting a new request for more chunks of the file. Put that together with say 3 users doing this at once and you can seriously blow your server - especially if the files are big.

usik
08-09-2007, 02:47 PM
why don't you hash the file path and save them to a database with the original path name



$path = "/videos/car.avi";

$filename = hash(sha1, $path);

mysql_query("INSERT INTO videos (file, path) VALUES ('$file', '$path')");


than have another script that becomes the video
e.g. /file.php?vid=7d5c2a2d6136fbf166211d5183bf66214a247f31



<?php
if(!isset($_GET["vid"]))
{
die("must select a video");
}

$video = $_GET["vid"];

$connect = mysql_connect("my_server", "my_user", "my_pass");
mysql_select_db("my_db");

$result = mysql_query("SELECT path FROM videos WHERE file = '$video'");
$videos = mysql_fetch_array($result);

$translated_video = $videos["path"];
mysql_close($connect);

header("Content-Type: video/x-msvideo"); //that is an avi video
readfile($translated_video);
?>


i have tested this and it works and the video is kept as whatever the script is called, e.g. video.php, and if they save it

so all you need to do then is


<embed src="file.php?vid=7d5c2a2d6136fbf166211d5183bf66214a247f31"></embed>

then add some security coding in the file.php to avoid people from downloading the script

Derek_M
08-10-2007, 12:00 AM
Hello,

Thank you BatCountry and usik! I do like the suggestion you gave, usik, but am wondering if there could be a way to do this without the use of a database?

I, of course, have all the MySQL access I'll need, I just wanted to know if there is another way that won't involve using MySQL? I say this for clutter issues and for the ease of the database.

Basically, I just want to give a link (it'll input the video name is a .php?vid=file.avi) and it to then play it. I have no issue specifying the directories in the .php file that is to be played, just would like to keep the load down.

But still, this is do able and will go with this if there isn't another way.

Thank you very much guys!

anarchy3200
08-10-2007, 12:07 AM
An alternative that i can see to using the database is to change the hash for a reversable encryption, you could send the encrypted version in the URL then the page decrypts it and embeds the appropriate video. As the database is only being used as a 1 to 1 lookup table this may be a usable alternative.
Hope an idea is enough help at the moment as i'm not in a position to write anything at the moment, just to offer an alternative!

Derek_M
08-10-2007, 12:14 AM
An alternative that i can see to using the database is to change the hash for a reversable encryption, you could send the encrypted version in the URL then the page decrypts it and embeds the appropriate video. As the database is only being used as a 1 to 1 lookup table this may be a usable alternative.
Hope an idea is enough help at the moment as i'm not in a position to write anything at the moment, just to offer an alternative!

Thank you. I'm still searching and testing, trying to find the right thing.

Derek_M
08-10-2007, 12:52 AM
Here is what I've been trying to use...

stream.php - where the video player is:

<html>
<html>
<head>
<title>Viewing <?=$_GET['vid']?></title>
</head>
<body>
<object classid="clsid:67DABFBF-D0AB-41fa-9C46-CC0F21721616" width="425" height="350" codebase="http://go.divx.com/plugin/DivXBrowserPlugin.cab">
<param name="custommode" value="Stage6" />

<param name="bufferingMode" value="null" />
<param name="allowContextMenu" value="false" />
<param name="src" value="stream_file.php?vid=<?=$_GET['vid']?>" />

<embed src="stream_file.php?vid=<?=$_GET['vid']?>" custommode="Stage6" width="425" height="350" bufferingMode="null" allowContextMenu="false" pluginspage="http://go.divx.com/plugin/download/">
</embed>
</object>
<br>No video? Get the DivX Web Player for <a style="text-decoration: underline;" href="http://download.divx.com/player/DivXWebPlayerInstaller.exe">Windows</a> or <a style="text-decoration: underline;" href="http://download.divx.com/player/DivXWebPlayer.dmg">Mac</a>
</body>
</html>


And then, stream_file.php, is coded with:

<?PHP

$video = "/videos/".$_GET["vid"];

header("Content-Type: video/x-msvideo"); //that is an avi video
readfile($video);

?>

As you see, very simple, but it doesnt seem to want to operate. The player just keeps "connecting". as seen here: http://www.gaminghybrid.com/stream.php?vid=ssbb.divx

http://www.gaminghybrid.com/videos/ as you can see, the file is in there, but the player just doesn't want to play.

Same issue with an .avi file. Just keeps "connecting"...

usik
08-10-2007, 09:49 AM
ok Iv spent a fair bit of time on this :p, I know it's sad, but here it is:

stream_video.php


<html>
<head></head>

<body>
<object classid="clsid:6BF52A52-394A-11D3-B153-00C04F79FAA6.">
<param name="src" value= <?php echo "\"video.php?vid={$_GET["vid"]}\""; ?>>
<param name="autoplay" value="true">
<param name="controller" value="true">

<embed src=<?php echo "\"video.php?vid={$_GET["vid"]}\""; ?>
autoplay="true" controller="true"
pluginspage="">
</embed>

</object>
</body>
</html>


and..

video.php


<?php
if(!isset($_GET["vid"])) {
die();
}

if(empty($_GET["vid"])) {
die();
}

$video = $_GET["vid"];
$length = filesize($video);

list($name, $type) = split("[.]", $video, 2);

switch($type) {
case "m4v":
$content = "video/x-m4v";
break;
case "3gp":
case "3gpp":
$content = "video/3gpp";
break;
case "3g2":
case "3gp2":
$content = "video/3gpp2";
break;
case "sdv":
$content = "video/sd-video";
break;
case "mp4":
$content = "video/mp4";
break;
case "mpg":
case "mpeg":
case "m1s":
case "m1v":
case "m1a":
case "m75":
case "m15":
case "mp2":
case "mpm":
case "mpv":
case "mpa":
$content = "video/x-mpeg";
break;
case "mov":
case "qt":
case "mqv":
$content = "video/quicktime";
break;
case "flc":
case "fli":
case "cel":
$content = "video/flc";
break;
case "asf":
case "asx":
$content = "video/x-ms-asf";
break;
case "wm":
$content = "video/x-ms-wm";
break;
case "wax":
$content = "video/x-ms-wax";
break;
case "wmv":
$content = "video/x-ms-wmv";
break;
case "wvx":
$content = "video/x-ms-wvx";
break;
case "divx":
case "div":
$content = "video/divx";
break;
}

header("Content-Type: $content");
header("Content-Length: $length");
readfile($video);
?>

iv tested the code plenty and it works for me, have a go and see what happens, I added a switch statement so you can play more than one type of video through the script, have fun :)
hope i'v helped



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum