From 3a8085d8981c6521cdf5dd3b5ed3412b2aac011e Mon Sep 17 00:00:00 2001 From: Horus3 Date: Sun, 14 Sep 2014 23:43:51 +0200 Subject: added support for webm videos --- dl.php | 43 ++++++++++++++++++++++++++++++++++--------- func.php | 22 +++++++++++++++++++++- stream.php | 17 +++++++++++++++++ 3 files changed, 72 insertions(+), 10 deletions(-) diff --git a/dl.php b/dl.php index 88d597e..4e6efe6 100644 --- a/dl.php +++ b/dl.php @@ -10,7 +10,7 @@ require_once("class/redis.php"); @ini_set("memory_limit",'150M'); set_time_limit(0); -$db = new database($REDIS_DBNAME, $REDIS_CONNECT); +$db = new database($REDIS_DBNAME, $REDIS_CONNECT); $db->open(); if(!$db->listExists($_GET["vid"])){ failure("No video information found.", true); @@ -119,16 +119,19 @@ switch($_GET["task"]){ case("stream"): ignore_user_abort(true); + if(file_exists($CACHEDIR."/".$info[2].".txt")) + failure('Already doing the request.', false); # send 'cached' response in case we already have the video on disk if(file_exists($CACHEDIR."/".$info[2]) && !file_exists($CACHEDIR."/".$info[2].".txt")){ echo "cached"; - } else { - # but in most cases we don't, so let's download it - $ret = video_dl($info[0], $CACHEDIR, $info[2]); - if(!$ret) - failure("Fetching the video failed.", false); - else - unlink($CACHEDIR."/".$info[2].".txt"); + exit; + } + # but in most cases we don't, so let's download it + $ret = video_dl($info[0], $CACHEDIR, $info[2]); + if(!$ret) + failure("Fetching the video failed.", false); + else + unlink($CACHEDIR."/".$info[2].".txt"); } break; @@ -140,8 +143,30 @@ switch($_GET["task"]){ pr_player(htmlentities($CACHEDIR . "/" . $info[2]), htmlentities($info[4]), htmlentities($mime), htmlentities($info[1]), htmlentities($info[2])); break; + case("convert"): + $dir = array_diff(scandir($CACHEDIR), array('.', '..')); + if(!in_array($_GET['vid'], $dir)){ + failure('Video not found.' false); + } + if(file_exists($CACHEDIR."/".$info[2].".webm.txt")) + exit; + if(file_exists($CACHEDIR."/".$info[2]."webm")){ + echo "cached"; + exit; + } + if(!convert_video($info[2])) + failure("Converting video failed.", false); + else + unlink($CACHEDIR."/".$info[2].".webm.txt"); + + $finfo = new finfo(FILEINFO_MIME_TYPE); + $mime = $finfo->file($CACHEDIR . "/" . $info[2]); + + pr_player(htmlentities($CACHEDIR . "/" . $info[2]), htmlentities($info[4]), htmlentities($mime), htmlentities($info[1]), htmlentities($info[2]), true); + + break; + default: failure("I don't understand your question.", true); break; - } diff --git a/func.php b/func.php index 3c6adb7..e1bf9bb 100644 --- a/func.php +++ b/func.php @@ -68,7 +68,26 @@ function video_xt_audio($VIDEO, $CACHEDIR, $FILENAME){ return true; } -function pr_player($file, $thumb, $mime, $title, $vid){ +function convert_video($FILENAME){ + $fp=fopen($CACHEDIR."/".$FILENAME.".webm.txt", "w"); + $content="extracting audio: ".$FILENAME."\n"; + fputs($fp, $content); + fclose($fp); + if(file_exists($FILENAME.".mp4") || file_exists($FILENAME.".webm")) + return; + rename($CACHEDIR . "/" . $FILENAME, $CACHEDIR . "/" . $FILENAME. ".mp4"); + exec("ffmpeg -i " . $CACHEDIR . "/" . $VID . ".mp4 " . $CACHEDIR . "/" . $VID . ".webm", $pid, $ret); + rename($CACHEDIR . "/" . $FILENAME . ".mp4", $CACHEDIR . "/" . $FILENAME); + if($ret != 0){ + return false; + } + + return true; +} + +function pr_player($file, $thumb, $mime, $title, $vid, $html5 = false){ +if($html5) + $WEBMVIDEO = ""; echo "

".htmlentities($title)."


@@ -78,6 +97,7 @@ echo " poster=\"".htmlentities($thumb)."\" data-setup='{\"techOrder\":[\"html5\",\"flash\"]}'> + ".$WEBMVIDEO."

To view this video please enable JavaScript, and consider upgrading to a web browser that supports HTML5 video