PHP - Help Needed With Streaming A Video
Hi, i am trying to stream a video from Rackspace clould files account. I could simply get the url of the video file and stream the video using the php api they have provided. But they have provided a stream function which they suggest to be used for video streaming or for large images.
Here is the code of the api : /** * Streaming read of Object's data * * Given an open PHP resource (see PHP's fopen() method), fetch the Object's * data and write it to the open resource handle. This is useful for * streaming an Object's content to the browser (videos, images) or for * fetching content to a local file. * * Pass in $hdrs array to set specific custom HTTP headers such as * If-Match, If-None-Match, If-Modified-Since, Range, etc. * * Example: * <code> * # ... authentication/connection/container code excluded * # ... see previous examples * * # Assuming this is a web script to display the README to the * # user's browser: * # * <?php * // grab README from storage system * // * $my_docs = $conn->get_container("documents"); * $doc = $my_docs->get_object("README"); * * // Hand it back to user's browser with appropriate content-type * // * header("Content-Type: " . $doc->content_type); * $output = fopen("php://output", "w"); * $doc->stream($output); # stream object content to PHP's output buffer * fclose($output); * ?> * * # See read() above for a more simple example. * # * </code> * * @param resource $fp open resource for writing data to * @param array $hdrs user-defined headers (Range, If-Match, etc.) * @return string Object's data * @throws InvalidResponseException unexpected response */ function stream(&$fp, $hdrs=array()) { list($status, $reason) = $this->container->cfs_http->get_object_to_stream($this,$fp,$hdrs); #if ($status == 401 && $this->_re_auth()) { # return $this->stream($fp, $hdrs); #} if (($status < 200) || ($status > 299 && $status != 412 && $status != 304)) { throw new InvalidResponseException("Invalid response (".$status."): " .$reason); } return True; } Here is the sample code they shared : $output = fopen("test.jpg", "w"); $pic->stream($output); fclose($output); If i echo the $pic variable, it just contains the name of the file. How do i display the image and how do i use this for streaming a video? Iam very new to php, so kindly help me. Thanks. Similar TutorialsSo i have a large video files, 1.5 gigs even. I made an html5 video player where the source is a php file. The php file is supposed to go below the web root and serve the video file to the html5 video tag. This all works just fine if the video using a script that uses HTTP_RANGE to serve the file in parts to the client. The problem is, once one video is playing - i can't do anything else. Its like the server locks up. I mean i can scrub that video, i can do anything to that page. I just can't navigate away. But i can open a new browser and play a new video. But again, once that first one is playing, or even paused, i can't do anything else. That is of course until i restart apache. Any ideas on how to stream these large files better? I have a feeling they are set to stream and never stop. This is the code for that Code: [Select] function rangeDownload($file) { $fp = @fopen($file, 'rb'); $size = filesize($file); // File size $length = $size; // Content length $start = 0; // Start byte $end = $size - 1; // End byte // Now that we've gotten so far without errors we send the accept range header /* At the moment we only support single ranges. * Multiple ranges requires some more work to ensure it works correctly * and comply with the spesifications: http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html#sec19.2 * * Multirange support annouces itself with: * header('Accept-Ranges: bytes'); * * Multirange content must be sent with multipart/byteranges mediatype, * (mediatype = mimetype) * as well as a boundry header to indicate the various chunks of data. */ header("Accept-Ranges: 0-$length"); // header('Accept-Ranges: bytes'); // multipart/byteranges // http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html#sec19.2 if (isset($_SERVER['HTTP_RANGE'])) { $c_start = $start; $c_end = $end; // Extract the range string list(, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2); // Make sure the client hasn't sent us a multibyte range if (strpos($range, ',') !== false) { // (?) Shoud this be issued here, or should the first // range be used? Or should the header be ignored and // we output the whole content? header('HTTP/1.1 416 Requested Range Not Satisfiable'); header("Content-Range: bytes $start-$end/$size"); // (?) Echo some info to the client? exit; } // If the range starts with an '-' we start from the beginning // If not, we forward the file pointer // And make sure to get the end byte if spesified if ($range == '-') { // The n-number of the last bytes is requested $c_start = $size - substr($range, 1); } else { $range = explode('-', $range); $c_start = $range[0]; $c_end = (isset($range[1]) && is_numeric($range[1])) ? $range[1] : $size; } /* Check the range and make sure it's treated according to the specs. * http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html */ // End bytes can not be larger than $end. $c_end = ($c_end > $end) ? $end : $c_end; // Validate the requested range and return an error if it's not correct. if ($c_start > $c_end || $c_start > $size - 1 || $c_end >= $size) { header('HTTP/1.1 416 Requested Range Not Satisfiable'); header("Content-Range: bytes $start-$end/$size"); // (?) Echo some info to the client? exit; } $start = $c_start; $end = $c_end; $length = $end - $start + 1; // Calculate new content length fseek($fp, $start); header('HTTP/1.1 206 Partial Content'); } // Notify the client the byte range we'll be outputting header("Content-Range: bytes $start-$end/$size"); header("Content-Length: $length"); // Start buffered download $buffer = 1024 * 8; while(!feof($fp) && ($p = ftell($fp)) <= $end) { if ($p + $buffer > $end) { // In case we're only outputtin a chunk, make sure we don't // read past the length $buffer = $end - $p + 1; } set_time_limit(0); // Reset time limit for big files echo fread($fp, $buffer); flush(); // Free up memory. Otherwise large files will trigger PHP's memory limit. } fclose($fp); } Hi, I've been trying,for a while now, to stream mp3 using MING extension. I have the later XAMPP 1.7.4 installed. So the code goes something like : Code: [Select] <?php $m = new SWFMovie(); $m->setRate(12.0); $m->streamMp3(file_get_contents('a.mp3',"r")); $m->setFrames(1420); header('Content-type: application/x-shockwave-flash'); $m->output(); ?> 'a.mp3' is in the same directory as of the php file. It opens the file, reads the file - but i've no idea what is it doing after that. There are no errors or warnings. A white ' FLASH ' screens comes up. Thats all. I've tried googling this thing up. I didn't find anything. What am I doing wrong? Hi all, I've been running into some issues trying to stream .mp3 files, specifically Safari/Flash... Firefox works great. I'm about to throw in the towel on this one and try another streaming solution, so I thought I'd reach out and connect with the PHP pros here before trying something else. Basically, the PHP streamer takes a track guid variable and token variable(MD5), looks up the field in the DB, gets the url and then proceeds to stream the file with no caching, and keeping the URL hidden from the user so they can't download it. The token variable is there to assure that the url is only used once. Once it is, the script will not allow the stream if the token/track guid combo already exists in the DB. The problem with the looping is that once that token has been used it won't stream the track, which is how it's suppose to work, sans looping. I've put an MYSQL INSERT into the code to verify that it does loop.. usually I see 3- :wtf:5 consecutive entries in the DB for that token. So Firefox does not do this.. and I gather it's something to do with how the client streams with the "readfile" function. Pasted below is some code to show the headers and the readfile function I am using. I can post the rest of the script if need be. //$path and $tokenApproved are variables i set earlier in the script date_default_timezone_set('GMT'); $date = date(DATE_RFC822); $filesize = ffilesize($path); $shortlen = $filesize - 1; //ffilesize() is a custom filesize function if($tokenApproved) { header("Content-type: audio/mpeg"); header("Content-Length: $filesize"); header('Content-Range: bytes 0-'.$shortlen.'/'.$filesize); header("Expires: $date"); header('Content-Disposition: filename="eztunesaudio.mp3"'); header("Cache-Control: no-store, no-cache, must-revalidate"); header("Cache-Control: post-check=0, pre-check=0", false); readfile($path); } //eztunesaudio.mp3 is just a default name I give the stream I am currently streaming my mp4 files perfectly fine using php. My issue is, how do I detect when the video is either no longer being watched, or disconnected? I need to run a function after this so that I know the video is no longer being watched for analytics. Is there a way maybe with sockets or something? I'm currently using this.
if (file_exists($request)) { $fp = @fopen($request, 'rb'); $size = filesize($request); $length = $size; $start = 0; $end = $size - 1; header("Content-Type: video/mp4"); header('Accept-Ranges: 0-' . $length); if (isset($_SERVER['HTTP_RANGE'])) { $c_start = $start; $c_end = $end; list(, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2); if (strpos($range, ',') !== false) { header('HTTP/1.1 416 Requested Range Not Satisfiable'); header('Content-Range: bytes ' . $start . '-' . $end . '/' . $size); exit(); } if ($range == '-') { $c_start = $size - substr($range, 1); } else { $range = explode('-', $range); $c_start = $range[0]; $c_end = (isset($range[1]) && is_numeric($range[1]) ? $range[1] : $size); } $c_end = ($end < $c_end ? $end : $c_end); if (($c_end < $c_start) || (($size - 1) < $c_start) || ($size <= $c_end)) { header('HTTP/1.1 416 Requested Range Not Satisfiable'); header('Content-Range: bytes ' . $start . '-' . $end . '/' . $size); exit(); } $start = $c_start; $end = $c_end; $length = ($end - $start) + 1; fseek($fp, $start); header('HTTP/1.1 206 Partial Content'); } header('Content-Range: bytes ' . $start . '-' . $end . '/' . $size); header('Content-Length: ' . $length); ob_end_flush(); $buffer = 8192; $time_start = time(); $bytes_read = 0; while (!feof($fp) && (($p = ftell($fp)) <= $end)) { $response = stream_get_line($fp, $buffer); echo $response; $bytes_read += strlen($response); if (30 <= time() - $time_start) { file_put_contents('/var/www/html/movieCon/'.$user.'_'.$token.'_'.$movie_id.'.con', time()); $time_start = time(); $bytes_read = 0; } } fclose($fp); exit(); }
I have a website http://www.pbm-biz.com/cricket/ where I am streaming Live Cricket world cup matches. My question is that during the streaming, I would like to keep the code in PHP so that others don't copy it. I can't find any solution. Any suggestions. Thanks, Faisal Hello, i just joined the forums, nice forum . Well im looking for a solution. I want download the videos from a site, but i cant find a solution how to get the location of the video, cause its dynamic. Here is a sample video: http://www.collegehumor.com/video:1946215 And here is the link to the video: http://8.media.collegehumor.cvcdn.com/21/12/a8cc907f5e150fe77ce7c0c47b1b9d31.mp4 That link i got via a FIREFOX Addon called "DownloadHelper" this addon finds the location with help of http headers. So is it possible way to find the videos with php? Thanks for help. hello, i have read topic on the internet that says i have to buy an adobe media interactive server and moyea so that you can have a video on the internet (without using you tube). how can i install a software like this to the webhost? thanks everyone. hi all How we can read the id of an element tag on the html page to use it in a sql query for selecting a particular record. or else how we can simply connect a link to jwplayer to play in the specific area defined for it. That we just click that link and it will play a video in that area. how it is implement in php In my php web site the video player appears and plays the video. In Chrome you can right click on the player screen and choose 'inspect element' etc, but another choice is 'Copy video URL'. How can I block that, or hide (or disguise/rename) video URLs?
Hi I am having an issue searching videos from youtube when ever I do a search i keep getting an page not found error. here is my code Code: [Select] <? class Youtube { var $dom, $idvideo, $title, $video; //function for video title function videoTitle() { return $this->title; } function pagination() { return true; } function tag($tag, $page = 1) { if ($page <= 0) $page = 1; $start = ($page-1)*10 + 1; $tag = str_replace(' ', '+', $tag); $feed = ('http://gdata.youtube.com/feeds/api/videos?vq='.$tag.'&start-index='.$start.'&max-results=10&orderby=updated&alt=rss'); $this->dom = getFeed($feed); } function video($id) { $this->idvideo = $id; $feed = ("http://gdata.youtube.com/feeds/api/videos/".$id); $this->dom = getFeed($feed); $this->video = $this->dom->getElementsByTagName('entry')->item(0); $this->title = $this->video->getElementsByTagName('title')->item(0)->textContent; } function player() { ?> <object width="425" height="355"> <param name="movie" value="http://www.youtube.com/v/<?=$this->idvideo?>" /> <param name="wmode" value="transparent" /> <embed src="http://www.youtube.com/v/<?=$this->idvideo?>" type="application/x-shockwave-flash" wmode="transparent" width="425" height="355"></embed> </object> <? } function view($tag) { $videos = $this->dom->getElementsByTagName('item'); foreach ($videos as $video) { $id = $video->getElementsByTagName('guid')->item(0)->textContent; $id = explode('/', $id); $id = $id[6]; echo '<div class="video">'; $title = $video->getElementsByTagName("title")->item(0)->textContent; echo '<p><a href="'.BASE_URL.'/0/video/'.$tag.'/'.$id.'.html">'.$title.'</a></p>'; $fotos = $video->getElementsByTagNameNS("http://search.yahoo.com/mrss/", "thumbnail"); $tiempo = $video->getElementsByTagNameNS("http://gdata.youtube.com/schemas/2007", "duration"); $tiempo = $tiempo->item(0)->getAttribute("seconds"); $i = 0; echo '<div class="img">'; foreach ($fotos as $foto) { $url = $foto->getAttribute("url"); echo '<a href="'.BASE_URL.'/0/video/'.$tag.'/'.$id.'.html"><img src="'.$url.'" alt="'.$title.'"/></a>'; $i++; if ($i > 2) break; } echo '</div>'; echo '<p class="links"><a href="'.BASE_URL.'/0/video/'.$tag.'/'.$id.'.html">'._VIEW.'</a> '._DURATION.': '.minutes($tiempo).'</p>'; echo '<p>'.cut($video->getElementsByTagName("description")->item(0)->textContent).'</p>'; $tags = $video->getElementsByTagName("category"); echo '<div class="tags">'; echo 'tags: '; foreach ($tags as $palabra) { if (strpos($palabra->textContent, "http") === FALSE) echo '<a href="'.BASE_URL.'/0/tag/'.$palabra->textContent.'.html">'.$palabra->textContent.'</a> '; } echo '</div>'; echo '</div>'; } } function videoInfo() { $id = $this->video->getElementsByTagName("id")->item(0)->textContent; $id = explode("/", $id); $id = $id[6]; echo '<div class="video">'; $title = $this->video->getElementsByTagName("title")->item(0)->textContent; $tiempo = $this->video->getElementsByTagNameNS("http://gdata.youtube.com/schemas/2007", "duration"); $tiempo = $tiempo->item(0)->getAttribute("seconds"); echo '<p class="links">'._DURATION.': '.minutes($tiempo).'</p>'; echo '<p>'.$this->video->getElementsByTagName("description")->item(0)->textContent.'</p>'; $tags = $this->video->getElementsByTagNameNS("http://www.w3.org/2005/Atom", "category"); echo '<div class="tags">'; echo 'tags: '; foreach ($tags as $palabra) { if (strpos($palabra->getAttribute("term"), "http") === FALSE) echo '<a href="'.BASE_URL.'/0/tag/'.$palabra->getAttribute("term").'.html">'.$palabra->getAttribute("term").'</a> '; } echo '</div>'; echo '</div>'; } } ?> Ok, this is killing me, So I'm finally asking for help. I'm working on a website, which streams videofiles from external links. The site plays & allows you to download the videos from the same external link. I need to mask the url of these videos. At the moment, I have successfully masked the url when downloading. If you click the download image, rather than the path being http://externalsite.com/videofile.avi its http://mysitename.com/external.php?id=xx&mid=xx So that is nicely hidden. However, I am having a problem when it comes to the embedded player on the page. At the moment i am using flowplayer for flv & mp4 videos, and just the usual embed for avi videos. Flow players embed code is like this --------------------------------------------------- <a href="<?=$videoFile?>" style="display:block;width:740px;height:400px;" id="player"> </a> <script language="JavaScript"> flowplayer("player", "<?=url::base()?>flowplayer-3.2.5.swf", { clip: { autoPlay: false, autoBuffering: true } }); </script> ----------------------------------------- At the moment, the player only works if i put the url in this format: http://externalsite.com/videofile.avi and wont work in this format http://mysitename.com/external.php?id=xx&mid=xx all download.php does is check the file exists, sets the headers and prompts users to open or save. Why isnt this working with the player? Please see the attached external.php file for my heade info etc to hopefully shed some light on this Hi guys, My first post here. I would like to seek you guys help about playing video from mysql. I wanted to upload video to database and also play it inside my webpage using the media player format. Thank in advance Hi,i m confused php developer (beginner bt i wnt to do advance things with php and mysql) i want to make video website for myself and i also have 10000s of video in my pc and can any one help me to complete this project 1. when user clik on some video thumbnail the video player(jw or flow player) pop-up and start the video without go to different page(if possible) 2.do i need to upload all my video first to my server or i upload it with php mysql then how is it possible to link and make thumbnail of all the 10000s videos with video player so all the video will play without error and i knw i cant make these 10000s videos thumbnails and video link and pages for each videos so how can i start my thing please help me in this a BIG THANKS in advance Hi, I want to restrict my registered user to see video only once per day... Can anyone guide me how can i do this with php??? Thanks Hiya! Is it possible to play an avi or mpg video file with php? If not is there any other recommeded scripts out there be it jquery, ajax, js ect that will do this? Many thanks, James. Hi everyone,
I'm hoping the someone can guide me in the right direction for what I need to do. Does anyone know of a really good tutorial and/or information about a plugin product that is available to do what I need? For example, what I need is to create a website that has the ability to allow video conferencing between a teacher and students. This what I envision - a college professor can log on to his website and display a live video feed of him. Also, as each of his students log on, their live video feed is then also supplied. Would prefer a already made plugin product but would also like to look at raw code in PHP to see how something like this could be accomplished. Thank you in advance.
Charles
Hi, I'm looking to create a PHP script that converts video files to MP4, WebM and OGG but I don't know where to start. I've been searching on google and on these forums for that type of information but I didn't find anything. Is there any website or PHP library/framework that I should look at in order to find that kind of information? Any help is appreciated! i need to load advertisemtn and after that video. how it is possible to do example link is belove. i need to load random commercials on random vidoes. please guide me thanks http://sports.yahoo.com/nfl/blog/shutdown_corner/post/Enjoy-special-teams-failures-Merry-Christmas-fr?urn=nfl-295573 I had a similar posting regarding positioning of a 2nd watermark on an uploaded video. I have resoled that successfully - thanks again for the help. Now, the watermarks look clear/sharp on a small screen, but when the screen is enlarged the images don't seem as sharp/clear. What is the trick? Solution? Is it to have a large image to begin with? The watermark.png currently is 98px w by 16px h. Here is a portion of the file code:
$watermark_image_full_path = "watermark.png"; // demo Video $video_time = ''; $demo_video = ''; if ($pt->config->demo_video == 'on' && !empty($data_insert['sell_video'])) { $have_demo = false; if (!empty($duration_file['playtime_seconds']) && $duration_file['playtime_seconds'] > 0) { $video_time = round((10 * round($duration_file['playtime_seconds'],0)) / 100,0); $video_time = '-t '.$video_time.' -async 1'; $have_demo = true; } } // demo Video $ffmpegCommand =''.$ffmpeg_b.' -y -i '.$video_file_full_path.' -i '.$watermark_image_full_path.' -i '.$watermark_image_full_path.' -filter_complex "scale=640:-2, scale=640:-2, overlay=10:10, overlay=170:170:enable=between(t\,5\,5+2)" -vcodec libx264 -preset '.$pt->config->convert_speed.' -crf 26 '.$video_output_full_path_240.' 2>&1'; $shell = shell_exec($ffmpegCommand); $upload_s3 = PT_UploadToS3($filepath . "_240p_converted.mp4"); $db->where('id', $insert); $db->update(T_VIDEOS, array( 'converted' => 1, '240p' => 1, 'video_location' => $filepath . "_240p_converted.mp4" )); if ($pt->config->queue_count > 0) { $db->where('video_id',$insert)->delete(T_QUEUE); } if ($video_res >= 3840) { $ffmpegCommand =''.$ffmpeg_b.' -y -i '.$video_file_full_path.' -i '.$watermark_image_full_path.' -i '.$watermark_image_full_path.' -filter_complex "scale=640:-2, scale=640:-2, overlay=10:10, overlay=170:170:enable=between(t\,5\,5+2)" -vcodec libx264 -preset '.$pt->config->convert_speed.' -crf 26 '.$video_output_full_path_4096.' 2>&1'; $shell = shell_exec($ffmpegCommand); $upload_s3 = PT_UploadToS3($filepath . "_4096p_converted.mp4"); $db->where('id', $insert); $db->update(T_VIDEOS, array( '4096p' => 1 )); // demo Video if ($pt->config->demo_video == 'on' && empty($demo_video) && $have_demo == true) { $demo_video = substr($filepath, 0,strpos($filepath, '_video') - 10).sha1(time()). "_video_4096p_demo.mp4"; $shell = shell_exec("$ffmpeg_b $video_time -y -i $video_file_full_path -vcodec libx264 -preset {$pt->config->convert_speed} -filter:v scale=3840:-2 -crf 26 ".$full_dir . $demo_video." 2>&1"); $upload_s3 = PT_UploadToS3($demo_video); $db->where('id', $insert); $db->update(T_VIDEOS, array( 'demo' => $demo_video )); } // demo Video } if ($video_res >= 2048) { $ffmpegCommand =''.$ffmpeg_b.' -y -i '.$video_file_full_path.' -i '.$watermark_image_full_path.' -i '.$watermark_image_full_path.' -filter_complex "scale=640:-2, scale=640:-2, overlay=10:10, overlay=170:170:enable=between(t\,5\,5+2)" -vcodec libx264 -preset '.$pt->config->convert_speed.' -crf 26 '.$video_output_full_path_2048.' 2>&1'; $shell = shell_exec($ffmpegCommand); $upload_s3 = PT_UploadToS3($filepath . "_2048p_converted.mp4"); $db->where('id', $insert); $db->update(T_VIDEOS, array( '2048p' => 1 )); // demo Video if ($pt->config->demo_video == 'on' && empty($demo_video) && $have_demo == true) { $demo_video = substr($filepath, 0,strpos($filepath, '_video') - 10).sha1(time()) . "_video_2048p_demo.mp4"; $shell = shell_exec("$ffmpeg_b $video_time -y -i $video_file_full_path -vcodec libx264 -preset {$pt->config->convert_speed} -filter:v scale=2048:-2 -crf 26 ".$full_dir . $demo_video." 2>&1"); $upload_s3 = PT_UploadToS3($demo_video); $db->where('id', $insert); $db->update(T_VIDEOS, array( 'demo' => $demo_video )); } // demo Video } if ($video_res >= 1920 || $video_res == 0) { $ffmpegCommand =''.$ffmpeg_b.' -y -i '.$video_file_full_path.' -i '.$watermark_image_full_path.' -i '.$watermark_image_full_path.' -filter_complex "scale=640:-2, scale=640:-2, overlay=10:10, overlay=170:170:enable=between(t\,5\,5+2)" -vcodec libx264 -preset '.$pt->config->convert_speed.' -crf 26 '.$video_output_full_path_1080.' 2>&1'; $shell = shell_exec($ffmpegCommand); any assistance is appreciated Hello there, I am going around and thinking of this is possible. Let's say I have a flash video player or something, and the thing I want to do is, that when the flash video is DONE playing, a PHP script/function will be made. How can that be done? I guess it should be a little bit like Lockerz. You know, when the video is done, you get credited. Any suggestions? Regards. |