summaryrefslogtreecommitdiff
path: root/protected/Video.php
diff options
context:
space:
mode:
Diffstat (limited to 'protected/Video.php')
-rwxr-xr-xprotected/Video.php95
1 files changed, 95 insertions, 0 deletions
diff --git a/protected/Video.php b/protected/Video.php
new file mode 100755
index 0000000..3149482
--- /dev/null
+++ b/protected/Video.php
@@ -0,0 +1,95 @@
+<?php
+/**
+ * Created by IntelliJ IDEA.
+ * User: root
+ * Date: 8/15/13
+ * Time: 11:17 AM
+ * To change this template use File | Settings | File Templates.
+ */
+
+class Video {
+
+ private $_cache = null;
+
+ public function __construct(){
+ $this->_cache = new Cache();
+ }
+
+ public function playVideo($url){
+ $id = $this->_cache->urlToId($url);
+
+ if($this->_cache->isCached($id)){
+ $filePath = Settings::$CachePath . DIRECTORY_SEPARATOR .$id;
+ $this->playFileVideo($filePath);
+ }else{
+
+ Async::createAsyncRequest("/async.php", array(
+ "password" => Settings::$DeveloperPassword,
+ "url" => $url,
+ "method" => "cacheVideo"
+ ));
+
+ $this->playHttpVideo($url);
+ }
+ }
+
+ public function playFileVideo($filePath){
+
+ $path = $filePath;
+
+ $size=filesize($path);
+
+ $fm=@fopen($path,'rb');
+ if(!$fm) {
+ // You can also redirect here
+ header ("HTTP/1.0 404 Not Found");
+ die();
+ }
+
+ $begin=0;
+ $end=$size;
+
+ if(isset($_SERVER['HTTP_RANGE'])) {
+ if(preg_match('/bytes=\h*(\d+)-(\d*)[\D.*]?/i', $_SERVER['HTTP_RANGE'], $matches)) {
+ $begin=intval($matches[0]);
+ if(!empty($matches[1])) {
+ $end=intval($matches[1]);
+ }
+ }
+ }
+
+ if($begin>0||$end<$size)
+ header('HTTP/1.0 206 Partial Content');
+ else
+ header('HTTP/1.0 200 OK');
+
+ header("Content-Type: video/mp4");
+ header('Accept-Ranges: bytes');
+ header('Content-Length:'.($end-$begin));
+ header("Content-Disposition: inline;");
+ header("Content-Range: bytes $begin-$end/$size");
+ header("Content-Transfer-Encoding: binary\n");
+ header('Connection: close');
+
+ $cur=$begin;
+ fseek($fm,$begin,0);
+
+ while(!feof($fm)&&$cur<$end&&(connection_status()==0))
+ { print fread($fm,min(1024*16,$end-$cur));
+ $cur+=1024*16;
+ usleep(1000);
+ }
+ die();
+
+ }
+
+ public function playHttpVideo($url){
+
+ $command = Settings::$PythonCommand . " " . Settings::$BasePath."protected" ." play_online_video.cgi";
+
+ header("Location: /play_video_online.cgi?url=$url");
+
+ }
+
+
+} \ No newline at end of file