aboutsummaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorJesús Eduardo <heckyel@hyperbola.info>2017-12-25 16:58:41 -0500
committerJesús Eduardo <heckyel@hyperbola.info>2017-12-25 16:58:41 -0500
commit49d54c0c3199fb8e380ce68f8fb08a308ddf56a7 (patch)
tree87e9be319452d75241476853a3b240aabdcbd7e2 /tools
parent6a844225b3b63b89926e136cf65c140df01a4677 (diff)
downloadytlibre-49d54c0c3199fb8e380ce68f8fb08a308ddf56a7.tar.lz
ytlibre-49d54c0c3199fb8e380ce68f8fb08a308ddf56a7.tar.xz
ytlibre-49d54c0c3199fb8e380ce68f8fb08a308ddf56a7.zip
migración mayor a smarty PHP
Diffstat (limited to 'tools')
-rw-r--r--tools/init.php89
-rw-r--r--tools/processor.php54
2 files changed, 143 insertions, 0 deletions
diff --git a/tools/init.php b/tools/init.php
new file mode 100644
index 0000000..65de424
--- /dev/null
+++ b/tools/init.php
@@ -0,0 +1,89 @@
+<?php
+
+function get($url) {
+ $ch = curl_init();
+ curl_setopt($ch, CURLOPT_URL, $url);
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+ $data = curl_exec($ch);
+ curl_close($ch);
+ return $data;
+}
+
+function secToDuration($seconds){
+ $minutes = intval($seconds/60);
+ $seconds = ($seconds - ($minutes * 60));
+ return $minutes . ' minutes ' . $seconds . ' seconds';
+}
+
+function parseStream($stream){
+ $available_formats = [];
+ foreach ($stream as $format) {
+ parse_str($format, $format_info);
+ if (isset($format_info['bitrate'])) $quality = isset($format_info['quality_label'])?$format_info['quality_label']:round($format_info['bitrate']/1000). 'k';
+ else $quality = isset($format_info['quality'])?$format_info['quality']:'';
+
+ switch ($quality){
+ case 'hd720':
+ $quality = "720p";
+ break;
+ case 'medium':
+ $quality = "360p";
+ break;
+ case 'small':
+ $quality = "240p"; // May Less
+ break;
+ }
+ $type = explode(";", $format_info['type']);
+ $type= $type[0];
+ switch ($type) {
+ case 'video/3gpp':
+ $type = "3GP";
+ break;
+ case 'video/mp4':
+ $type = "MP4";
+ break;
+ case 'video/webm':
+ $type = "WEBM";
+ break;
+ }
+
+ $libretype = explode(";", $format_info['type']);
+ $libretype= $libretype[0];
+ switch ($libretype) {
+ case 'video/webm':
+ $libretype = "video/webm";
+ break;
+ }
+
+ $available_formats[] = [
+ 'itag' => $format_info['itag'],
+ 'quality' => $quality,
+ 'type' => $type,
+ 'libretype' => $libretype,
+ 'url' => $format_info['url'],
+ 'size' => getSize($format_info['url']),
+
+ ];
+ }
+
+ return $available_formats;
+}
+
+function getSize($url)
+{
+
+ $ch = curl_init($url);
+ curl_setopt($ch, CURLOPT_HEADER, true);
+ curl_setopt($ch, CURLOPT_NOBODY, true);
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+ curl_setopt($ch, CURLOPT_TIMEOUT, 10);
+ curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
+ curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
+ $r = curl_exec($ch);
+
+ foreach (explode("\n", $r) as $header) {
+ if (strpos($header, 'Content-Length:') === 0) {
+ return intval(intval(trim(substr($header, 16)))/ (1024*1024)) . " MB";
+ }
+ }
+}
diff --git a/tools/processor.php b/tools/processor.php
new file mode 100644
index 0000000..ff67722
--- /dev/null
+++ b/tools/processor.php
@@ -0,0 +1,54 @@
+<?php
+require_once "init.php";
+
+if (empty($_GET['link'])){
+ header('Location: index.php');
+}
+
+$baselink = htmlspecialchars($_GET['link']);
+
+$yturl = 'youtu.be/';
+
+if (strpos($baselink,$yturl) !== false){
+ $link = preg_replace('~^https?://youtu\.be/([a-z\d]+)$~i', 'https://www.youtube.com/watch?v=$1', $baselink);
+} else {
+ $link = $baselink;
+}
+
+parse_str($link, $urlData);
+$my_id = array_values($urlData)[0];
+
+$videoFetchURL = "https://www.youtube.com/get_video_info?&video_id=" . $my_id . "&asv=3&el=detailpage&hl=en_US";
+$videoData = get($videoFetchURL);
+
+parse_str($videoData, $video_info);
+
+$video_info = json_decode(json_encode($video_info));
+if (!$video_info->status === "ok") {
+ die("error in fetching youtube video data");
+}
+$videoTitle = $video_info->title;
+$videoAuthor = $video_info->author;
+$videoDurationSecs = $video_info->length_seconds;
+$videoDuration = secToDuration($videoDurationSecs);
+$videoViews = $video_info->view_count;
+
+//change hqdefault.jpg to default.jpg for downgrading the thumbnail quality
+$videoThumbURL = "https://i1.ytimg.com/vi/{$my_id}/hqdefault.jpg";
+$librethumb = "https://i1.ytimg.com/vi/{$my_id}/maxresdefault.jpg";
+
+if (!isset($video_info->url_encoded_fmt_stream_map)) {
+ die('No data found');
+}
+
+$streamFormats = explode(",", $video_info->url_encoded_fmt_stream_map);
+
+if (isset($video_info->adaptive_fmts)) {
+ $streamSFormats = explode(",", $video_info->adaptive_fmts);
+ $pStreams = parseStream($streamSFormats);
+}
+
+$cStreams = parseStream($streamFormats);
+
+// Aquí selecciona solo 2 vídeos principales.
+$videosStream = array_slice($cStreams, 0, 2);