forked from ahdidou-mohamed/Youtube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathYoutube.php
97 lines (85 loc) · 2.43 KB
/
Youtube.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
namespace Zarkiel\Media;
/**
* This class allows you to get the download links from any youtube video
*
* @author Zarkiel
*/
class Youtube{
/**
* The video map for the results
*
* @var array
*/
private $videoMap = array(
"13" => array("3GP", "Low Quality - 176x144"),
"17" => array("3GP", "Medium Quality - 176x144"),
"36" => array("3GP", "High Quality - 320x240"),
"5" => array("FLV", "Low Quality - 400x226"),
"6" => array("FLV", "Medium Quality - 640x360"),
"34" => array("FLV", "Medium Quality - 640x360"),
"35" => array("FLV", "High Quality - 854x480"),
"43" => array("WEBM", "Low Quality - 640x360"),
"44" => array("WEBM", "Medium Quality - 854x480"),
"45" => array("WEBM", "High Quality - 1280x720"),
"18" => array("MP4", "Medium Quality - 480x360"),
"22" => array("MP4", "High Quality - 1280x720"),
"37" => array("MP4", "High Quality - 1920x1080"),
"38" => array("MP4", "High Quality - 4096x230")
);
/**
* The page that will be used for requests
*
* @var string
*/
private $videoPageUrl = 'http://www.youtube.com/watch?v=';
/**
* Returns the video page content
*
* @param string The video id
* @return string The video page content
*/
protected function getPageContent($id){
$page = $this->videoPageUrl.$id;
$arr = array(
"ssl"=>array(
"verify_peer"=>false,
"verify_peer_name"=>false,
),
);
$content = file_get_contents($page, false, stream_context_create($arr));
return $content;
}
/**
* Return the download links
*
* @param string The video id
* @return array The download links
*/
function getDownloadLinks($id){
$content = $this->getPageContent($id);
$videos = array('MP4' => array(), 'FLV' => array(), '3GP' => array(), 'WEBM' => array());
if(preg_match("'\"url_encoded_fmt_stream_map\":\"(.*?)\"'si", $content, $r)){
$data = $r[1];
$data = explode(',', $data);
foreach($data As $cdata){
$cdata = str_replace('\u0026', '&', $cdata);
$cdata = explode('&', $cdata);
foreach($cdata As $xdata){
if(preg_match('/^sig/', $xdata)){
$sig = substr($xdata, 4);
}
if(preg_match('/^url/', $xdata)){
$url = substr($xdata, 4);
}
if(preg_match('/^itag/', $xdata)){
$type = substr($xdata, 5);
}
}
$url = urldecode($url);
$videos[$this->videoMap[$type][0]][$this->videoMap[$type][1]] = $url;
}
}
return $videos;
}
}