Hello friends, How to play videos using php, could anyone suggest me code to play the videos. For example, I have a video "abc.mpg", i want to play it in the browser using php code. I need help asap. Thanks.
videos don't play with php. you html to play a video, windows media player code, or quicktime, real player, or what would be easier would be flash. try uploading the video to youtube and just embedding their player into your page
No, I want to play the videos dynamically, for example video.php?abc.mpg how to retrieve the abc.mpg from the file name and play the video.
if you want something simple videos would be accessed from video.php?v=abc.mpg or whatever <? $videos = array(); $videos["abc.mpg"] = "http://example.com/vids/abc.mpg"; $videos["sample.avi"] = "http://example.com/sample.avi"; $chosen = $_GET['v']; echo "<embed src=".$videos[$chosen]."></embed>"; ?> PHP: otherwise you can use a mysql database to retrieve values dynamically as you add them to the database but you need to embed the html into pages to watch videos. using youtube would be ideal, they have a very simple universal way of playing videos and they give you the embed code
I'm not very skilled when it comes to mysql databases. I can't remember the code off hand to access these. You would also have to write the code to your specifications. Sorry
You can make a database with following fields (for example: videos): - id - name - file Then you have an url: www.domain.com/video.php?id=1 You can use a script like this: <?php if(empty($_GET[id]) OR !is_numeric($_GET[id])){ echo 'Error'; }else{ $query=msyql_query("SELECT id,name,file FROM videos WHERE id='" . $_GET[id] . "' LIMIT 1"); if(mysql_num_rows($query)==0){ echo 'Error.'; }else{ $vid=mysql_fetch_object($query); $file=$vid->file; // put here your html } } ?> PHP:
Video Playing php Code :- <figure class="style-img-2 fleft"> <a class="lightbox-image" href="video/video_AS3.swf?width=495&height=275&fileVideo=aa.flv" data-gal="prettyVideo[prettyVideo]"> <img src="images/page1-img2.jpg" alt=""></a></figure>
@Parth01 You do realise that the code you posted is... wow. Incredibly bad, wrong, and completely useless in a real-life environment that does NOT apply lightbox, right? Also, with no instructions, how is the OP supposed to know that the image doesn't magically appear? And... just. No. Go away. Come back when you have something useful to contribute.
Well you could have at least told the OP that the first guy's code was horrible also. @Chamaro Zwinkles
you can use html5 video tag to play video and change the src dynamically. <video id="myVideo" width="500" height="280" controls autoplay poster="image/poster.png"> <source src="" type="video/mp4" /> <em>Sorry, your browser doesn't support HTML5 video.</em> </video> <?php $videos[0]="http://somedomain1.com/video1.mp4"; $videos[1]="http://somedomain2.com/video2.mp4"; if(isset($_REQUEST['vid'])){ $vid=$_REQUEST['vid']; //put your code here to convert $vid to $url of the video, e.g. $url=$videos[$vid]; } ?> <script> var vid=document.getElementById('myVideo'); vid.src='<?php echo $url?>'; </script> HTML:
Why are we assuming he wants to build some sort of infrastructure with PHP to allow a visitor to search and play thousands of videos? He didn't ask that. His question was simple... the answer is you can't directly play a video through PHP. You would use HTML5 or JavaScript or embed a player.
This is a simple way to play files in the way you asked. This player I use and is perfect. http://codecanyon.net/item/ultimate-video-player/7694071
Maybe Moin is asking for a php way to render or stream video, it is not that hard, here is the code <?php $browser = "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36"; $url=$_REQUEST['url']; play($url, "video/mp4"); function play($file, $content_type = 'application/octet-stream') { global $start, $end, $length; @error_reporting(0); $opts = array( 'http'=>array( 'method'=> "GET", 'seekable'=> true ) ); /*** Sends an http request to $file ***/ $ctx = stream_context_create($opts); if (!($fp = fopen($file, 'r', false, $ctx))) { header("HTTP/1.1 404 Not Found"); exit; } // Get file size $meta = stream_get_meta_data($fp); $jsonStr=json_encode($meta); $jsonStr=substr($jsonStr,strrpos($jsonStr, "Content-Length:")); preg_match("#Content\-Length:\s(\d+)#",$jsonStr,$matches); $filesize=$matches[1]; $range=false; // Handle 'Range' header if(isset($_SERVER['HTTP_RANGE'])){ $range = $_SERVER['HTTP_RANGE']; }else if($apache = apache_request_headers()){ $headers = array(); foreach ($apache as $header => $val){ $headers[strtolower($header)] = $val; } if(isset($headers['range'])) $range = $headers['range']; else $range = FALSE; //Is range $partial = false; if(strlen($range)>1){ $partial = true; list($param, $range) = explode('=',$range); // Bad request - range unit is not 'bytes' if(strtolower(trim($param)) != 'bytes'){ header("HTTP/1.1 400 Invalid Request"); exit; } // Get range values $range = explode(',',$range); $range = explode('-',$range[0]); // Deal with range values if ($range[0] === ''){ $end = $filesize - 1; $start = $end - intval($range[0]); } else if ($range[1] === '') { $start = intval($range[0]); $end = $filesize - 1; }else{ // Both numbers present, return specific range $start = intval($range[0]); $end = intval($range[1]); // Invalid range/whole file specified, return whole file if ($end >= $filesize || (!$start && (!$end || $end == ($filesize - 1)))) $partial = false; } $length = $end - $start + 1; } else $partial = false; // No range requested // send extra headers for range handling... if ($partial) { // Send standard headers header("Content-Type: $content_type"); header("User-Agent: $browser"); header('HTTP/1.1 206 Partial Content'); header('Accept-Ranges: bytes'); header("Content-Length: $filesize"); header("Content-Range: bytes $start-$end/$filesize"); header("Cache-Control: max-age=2592000, public"); header("Content-Transfer-Encoding: binary"); header("Expires: ".gmdate('D, d M Y H:i:s', time()+2592000) . ' GMT'); header('Cache-Control: private'); header('Access-Control-Allow-Origin: *'); header("Content-Disposition: inline;"); header('Connection: close'); ob_clean(); flush(); $seek_range=$start; if($seek_range>0){ if($seek_range<=PHP_INT_MAX) stream_get_contents($fp,1,$seek_range); else { while($seek_range>PHP_INT_MAX){ stream_get_contents($fp,PHP_INT_MAX); $seek_range -= PHP_INT_MAX; } stream_get_contents($fp,1,$seek_range); } } while($length){ set_time_limit(0); $read = ($length > 2048) ? 2048 : $length; $length -= $read; print stream_get_contents($fp, $read); } exit; } readstream($fp, $content_type, $filesize); //just send the whole file fclose($fp); } function readstream($fp, $content_type = 'application/octet-stream', $filesize) { global $start, $end, $length; if($start>0||$end<$filesize) header('HTTP/1.1 206 Partial Content'); else header('HTTP/1.0 200 OK'); header("Content-Type: $content_type"); header("User-Agent: $browser"); header('Accept-Ranges: bytes'); header("Content-Length: $filesize"); header("Content-Range: bytes $start-$end/$filesize"); header("Cache-Control: max-age=2592000, public"); header("Expires: ".gmdate('D, d M Y H:i:s', time()+2592000) . ' GMT'); header('Access-Control-Allow-Origin: *'); header("Content-Transfer-Encoding: binary"); header('Content-Description: File Transfer'); header('Cache-Control: private'); header('Connection: close'); header("Content-Disposition: inline;"); header('Cache-Control: must-revalidate'); header('Pragma: public'); ob_clean(); flush(); $cur=$start; while(!feof($fp)&& $cur<$end && (connection_status()==0)){ $read=min(1024*16,$end-$cur); print stream_get_contents($fp, $read); $cur+=$read; usleep(1000); } } ?> PHP: