<?php
if (!defined("BASEPATH"))
{
exit("No direct script access allowed");
}
ini_set("max_execution_time", 0);
ini_set("memory_limit", "64M");
class video
{
private $CI;
private $error;
private $video_id;
private $path_video = "uploads/videos/";
private $config;
private $vid_ext = array("mp4", "flv", "mpeg", "avi");
public function __construct()
{
$this->CI =& get_instance();
$this->CI->load->model("admin/setting_model");
$this->config = $this->CI->setting_model->get_custom_settings(array(
"dailymotion_video_download",
"youtube_video_download",
"youtube_video_quality",
"dailymotion_video_quality"
));
$this->curl_follow_location = $this->CI->config->item("curl_follow_location");
$this->video_limit_size = $this->CI->config->item("video_limit_size");
$this->providers = $this->CI->config->item("video_down_providers");
}
public function getVideo($url, $provider = NULL)
{
$this->video_id = NULL;
if ($provider != NULL)
{
return $this->$provider($url);
}
if (0 < count($this->providers))
{
foreach ($this->providers as $provider)
{
if (strposa($url, $provider["needle"]))
{
return $this->$provider["value"]($url);
}
}
}
$this->error = "The content url you entered did not match any service provider.Make sure the video Url is correct.";
return false;
}
public function dailymotion($url)
{
if ($this->config["dailymotion_video_download"] != 1)
{
$this->error = "Video downloads seem to be closed at Youtube Api.";
return false;
}
if (strpos($url, "dailymotion"))
{
$this->video_id = strtok(basename($url), "_");
}
else
{
if (strpos($url, "dai.ly"))
{
$this->video_id = end(explode("/"));
}
}
if (preg_match("#[A-Za-z0-9]#si", $this->video_id, $matches))
{
$get = curlGet("http://www.dailymotion.com/embed/video/" . $this->video_id);
$data["duration"] = "";
$data["description"] = $data["duration"];
$data["thumbnail"] = $data["description"];
$data["title"] = $data["thumbnail"];
switch ($this->config["youtube_video_quality"])
{
case "good":
$target_formats = array(
"stream_h264_hd1080_url",
"stream_h264_hd_url",
"stream_h264_hq_url",
"stream_h264_ld_url",
"stream_h264_url"
);
break;
case "medium":
$target_formats = array(
"stream_h264_hd_url",
"stream_h264_hq_url",
"stream_h264_ld_url",
"stream_h264_hd1080_url",
"stream_h264_url"
);
break;
case "low":
$target_formats = array(
"stream_h264_url",
"stream_h264_ld_url",
"stream_h264_hq_url",
"stream_h264_hd_url",
"stream_h264_hd1080_url"
);
}
$formats = array(
"stream_h264_url",
"stream_h264_ld_url",
"stream_h264_hq_url",
"stream_h264_hd_url",
"stream_h264_hd1080_url"
);
foreach ($formats as $format)
{
if (preg_match("#\"" . $format . "\"\\:\"(.*?)\"#si", $get, $videoSource))
{
$avail_formats[$format] = str_replace("\\/", "/", urldecode($videoSource[1]));
}
}
if (!isset($avail_formats))
{
$this->error = "Video source not found.";
return false;
}
$data["duration"] = "";
$data["description"] = $data["duration"];
$data["thumbnail"] = $data["description"];
$data["title"] = $data["thumbnail"];
foreach ($target_formats as $target_format)
{
foreach ($avail_formats as $avail_format_name => $avail_format)
{
if ($target_format == $avail_format_name)
{
$data["videoUrl"] = $avail_format;
}
}
}
if (!isset($data["videoUrl"]))
{
$this->error = "Video source not found.";
return false;
}
if (preg_match("#<title>(.*?)<\\/title>#si", $get, $videoTitle))
{
$data["title"] = $videoTitle[1];
}
if (preg_match("#thumbnail_url\"\\:\"(.*?)\"#si", $get, $videoThumbnail))
{
$data["thumbnail"] = str_replace("\\/", "/", $videoThumbnail[1]);
}
if (preg_match("#duration\":(.*?),#si", $get, $videoduration))
{
$data["duration"] = $videoduration[1];
}
return $data;
}
$this->error = "Please enter video Url like http://www.dailymotion.com/video/[videoID]_XXX";
return false;
}
public function metacafe($url)
{
$ex = explode("/", $url);
if (!is_numeric($ex[4]))
{
$this->error = "Please enter video Url like http://www.metacafe.com/watch/[videoID]/XXX";
return false;
}
$this->video_id = $ex[4];
$data["duration"] = "";
$data["description"] = $data["duration"];
$data["thumbnail"] = $data["description"];
$data["title"] = $data["thumbnail"];
$get = curlGet("http://www.metacafe.com/embed/" . $this->video_id . "/", true);
if (preg_match("#source src=\"(.*?)\"#si", $get, $videoUrl))
{
if (preg_match("#<title>(.*?)<\\/title>#si", $get, $videoTitle))
{
$data["title"] = $videoTitle[1];
}
if (preg_match("#property=\"og\\:image\" content=\"(.*?)\"#si", $get, $videoThumbnail))
{
$data["thumbnail"] = $videoThumbnail[1];
}
if (preg_match("#name=\"description\" content=\"(.*?)\"#si", $get, $videoDescription))
{
$data["description"] = $videoDescription[1];
}
$data["videoUrl"] = urldecode($videoUrl[1]);
return $data;
}
$this->error = "Video source not found.";
return false;
}
public function facebook($url)
{
if (preg_match("#v=[0-9]+#si", $url, $pattern))
{
$this->video_id = end(explode("=", $pattern[0]));
$get = curlGet("https://www.facebook.com/video.php?v=" . $this->video_id);
$al = facebook_encode_replace($get);
$al = stripslashes($al);
if (preg_match("#\"sd_src\":\"(.*?)\"#si", $al, $videoUrl))
{
$data["videoUrl"] = ayir("\"sd_src\":\"", "\"", $al);
if (($hdvid = @ayir("\"hd_src\":\"", "\"", $al)) != NULL)
{
$data["videoUrl"] = $hdvid;
}
$data["thumbnail"] = "";
$data["duration"] = "";
$data["title"] = @ayir("<title id=\"pageTitle\">", "</title>", $al);
return $data;
}
$this->error = "Video source not found.Make sure the video are trying to retrieve public.";
return false;
}
$this->error = "Please enter video Url like https://www.facebook.com/video.php?v=[videoID]";
return false;
}
private function url_check($video_id)
{
if (!empty($video_id))
{
$my_id = $video_id;
if (11 < strlen($my_id))
{
$url = parse_url($my_id);
$my_id = NULL;
if (is_array($url) && 0 < count($url) && isset($url["query"]) && !empty($url["query"]))
{
$parts = explode("&", $url["query"]);
if (is_array($parts) && 0 < count($parts))
{
foreach ($parts as $p)
{
$pattern = "/^v\\=/";
if (preg_match($pattern, $p))
{
$my_id = preg_replace($pattern, "", $p);
break;
}
}
}
if (!$my_id)
{
$this->$error = "No video id passed in";
return false;
}
}
else
{
$this->error = "Invalid url";
return false;
}
}
$this->video_id = $my_id;
return true;
}
$this->error = "Please enter video Url like http://www.youtube.com/watch?v=[videoID]";
return false;
}
public function youtube($url)
{
if ($this->config["youtube_video_download"] != 1)
{
$this->error = "Video downloads seem to be closed at Youtube Api.";
return false;
}
if ($this->url_check(parse_yturl($url)))
{
if (($ydata = YoutubeGen($this->video_id)) == false)
{
$this->error = "Error could not be identified given the video content.";
return false;
}
switch ($this->config["youtube_video_quality"])
{
case "high":
$target_formats = array(
"137",
"136",
"135",
"134",
"133",
"5",
"160"
);
break;
case "medium":
$target_formats = array(
"134",
"135",
"5",
"38",
"133",
"160",
"136",
"137"
);
break;
case "low":
$target_formats = array(
"133",
"5",
"160",
"136"
);
}
foreach ($target_formats as $target_format)
{
if (isset($ydata["link"][$target_format]))
{
$redirect_url = $ydata["link"][$target_format][0];
}
}
if (isset($redirect_url))
{
$data = $this->get_youtube_description($this->video_id);
$data["videoUrl"] = $redirect_url;
return $data;
}
$this->error = "Video download link not found.Entered the youtube Url can be private or not allowed access only through YouTube.";
return false;
}
return false;
}
public function vk($q, $page = 1)
{
if (empty($q))
{
$this->error = "Music name is mandotary to fill.";
return false;
}
$settings = $this->CI->setting_model->get_custom_settings(array(
"vk_video_upload",
"vk_client_id",
"vk_secret_key",
"vk_access_token",
"vk_lyrics_support"
));
if ($settings["vk_video_upload"] == 1)
{
$this->CI->load->library("vk", array(
"client_id" => $settings["vk_client_id"],
"secret_key" => $settings["vk_secret_key"],
"access_token" => $settings["vk_access_token"]
));
$result = $this->CI->vk->api("audio.search", array(
"v" => "2.0",
"q" => $q,
"count" => 20,
"offset" => ($page + 0) * 20 - 20
));
if (isset($result["error"]))
{
$this->error = $result["error"]["error_msg"];
return false;
}
if (1 < count($result))
{
foreach ($result as $value)
{
if (!is_array($value))
{
continue;
}
$data["embed"][] = "[VK_AUDIO=" . $value["owner_id"] . "_" . $value["aid"] . "]";
$data["duration"][] = floor($value["duration"] / 60) . ":" . (($value["duration"] % 60 < 10 ? "0" : "")) . $value["duration"] % 60;
$data["name"][] = $value["artist"] . " — " . $value["title"];
if (isset($value["lyrics_id"]) && $settings["vk_lyrics_support"] == 1)
{
$data["lyrics_id"][] = $value["lyrics_id"];
}
else
{
$data["lyrics_id"][] = 0;
}
}
$data["page"] = $page;
return $data;
}
$this->error = "Music not found for your search";
}
else
{
$this->error = "Vk Api seem to be closed so your operation can not be performed.";
}
return false;
}
public function vk_get_lyrics($lyrics_id)
{
$settings = $this->CI->setting_model->get_custom_settings(array(
"vk_client_id",
"vk_secret_key",
"vk_access_token",
"vk_lyrics_support"
));
$this->CI->load->library("vk", array(
"client_id" => $settings["vk_client_id"],
"secret_key" => $settings["vk_secret_key"],
"access_token" => $settings["vk_access_token"]
));
if (!empty($lyrics_id) && $settings["vk_lyrics_support"] == 1)
{
$lyric = $this->CI->vk->api("audio.getLyrics", array(
"lyrics_id" => $lyrics_id
));
$data["content"] = html_entity_decode(nl2br($lyric["text"]));
}
else
{
$data["content"] = "";
}
return $data;
}
public function download_url_video($url)
{
if ($this->get_size($url) <= $this->video_limit_size)
{
$pathinfo = pathinfo($url);
if (isset($pathinfo["extension"]))
{
$extension = explode("?", $pathinfo["extension"]);
$extension = (stristr($extension[0], "title") ? "mp4" : $extension[0]);
if (!in_array($extension, $this->vid_ext))
{
$extension = "mp4";
}
}
else
{
$extension = "mp4";
}
mt_srand();
$video_name = md5(uniqid(mt_rand())) . "." . $extension;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13");
curl_setopt($ch, CURLOPT_REFERER, "http://www.google.com");
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $this->curl_follow_location);
$video = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if (200 <= $httpcode && $httpcode < 300)
{
$file = fopen(FCPATH . $this->path_video . $video_name, "w");
fwrite($file, $video);
fclose($file);
return $video_name;
}
$this->error = "Could not access the source for downloading the video.";
}
else
{
$this->error = "Size of the video you are trying to download is above limits.You can change limits on /application/config/videnox.php ";
}
return false;
}
public function videoThumbDuration($videoPath, $gthumb = true, $gduration = true)
{
$ffmpeg = @trim(@shell_exec("type -P ffmpeg"));
if (!empty($ffmpeg))
{
$data = false;
$video = FCPATH . $this->path_video . $videoPath;
if ($gthumb)
{
$data["thumbnail"] = "";
mt_srand();
$thumb = md5(uniqid(mt_rand())) . ".jpg";
$thumbnail = FCPATH . "uploads/thumb/" . $thumb;
shell_exec("ffmpeg -i " . $video . " -deinterlace -an -ss 1 -t 00:00:01 -r 1 -y -vcodec mjpeg -f mjpeg " . $thumbnail . " 2>&1");
if (file_exists($thumbnail))
{
$data["thumbnail"] = $thumb;
}
}
if ($gduration)
{
$data["duration"] = "";
$videoTime = shell_exec("ffmpeg -i \"" . $video . "\" 2>&1");
if (preg_match("/Duration: (\\d{2}:\\d{2}:\\d{2}\\.\\d{2})/", $videoTime, $matches))
{
$time = explode(":", $matches[1]);
list($hour, $minutes) = $time;
$seconds = round($time[2]);
$data["duration"] = $hour . ":" . $minutes . ":" . $seconds;
}
}
return $data;
}
return array(
"duration" => "",
"thumbnail" => ""
);
}
public function upload_video($ffmpeg = true)
{
$config["upload_path"] = FCPATH . $this->path_video;
$config["allowed_types"] = "avi|flv|wmv|mp4";
$config["max_size"] = $this->video_limit_size;
$config["encrypt_name"] = true;
$this->CI->load->library("upload", $config);
if (!$this->CI->upload->do_upload("qqfile"))
{
$data = array(
"success" => false,
"error" => $this->CI->upload->display_errors("", "")
);
}
else
{
$video = $this->CI->upload->data();
$response = array(
"thumbnail" => "",
"duration" => ""
);
if ($ffmpeg)
{
$response = $this->videoThumbDuration($video["file_name"]);
}
$data = array(
"success" => true,
"video_url" => $video["file_name"],
"image_url" => $response["thumbnail"],
"duration" => $response["duration"]
);
}
return $data;
}
private function curlGet($URL)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
if (curl_exec($ch) === false)
{
$this->error = "Curl error: " . curl_error($ch);
return false;
}
$tmp = curl_exec($ch);
curl_close($ch);
return (empty($tmp) ? false : $tmp);
}
private function get_size($url)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
$data = curl_exec($ch);
$size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
curl_close($ch);
return $size;
}
public function get_youtube_description($video_id)
{
$json = json_decode($this->curlGet("http://gdata.youtube.com/feeds/api/videos/" . $video_id . "?v=2&alt=json"), true);
if (isset($json["entry"]))
{
$data["description"] = $json["entry"]["media\$group"]["media\$description"]["\$t"];
$data["title"] = $json["entry"]["title"]["\$t"];
$data["duration"] = $json["entry"]["media\$group"]["yt\$duration"]["seconds"];
$data["thumbnail"] = str_replace("default", "hqdefault", $json["entry"]["media\$group"]["media\$thumbnail"][0]["url"]);
return $data;
}
return false;
}
public function get_error()
{
return $this->error;
}
}
?>