Author: Pernod
(2012/02/12 02:02) 3 months ago
Updated YouTube streaming due to site changes at YouTube.
152
// A YouTube id has been provided so we need to determine the actual location of the video file.
153
if (!isset($_SESSION["stream_id"]) || ($_SESSION["stream_id"] !== $_REQUEST["youtube_id"]))
154
{
155
$video_id = $_REQUEST["youtube_id"];
$videoId = $_REQUEST["youtube_id"];
156
157
// Get the YouTube video page to parse
158
$youtube_url = 'http://www.youtube.com/watch?v='.$video_id;
$youtube_url = 'http://www.youtube.com/watch?v='.$videoId;
159
$html = file_get_contents($youtube_url);
160
161
//
162
// This code is based upon a script found at http://userscripts.org/scripts/show/109103
163
// Version 1.2.5 : Date 2011-08-04
// Version 1.3.8 : Date 2012-02-09
164
165
// Obtain video ID, temporary ticket, formats map
166
$video_player = preg_get('/<div id="watch-player".*>(.*)<\/div>/Usm', $html);
if (!empty($html))
167
if ( !empty($video_player) )
168
169
$video_ticket = preg_get('/\&t=([^(\&|$)]*)/', $video_player);
$videoTicket = preg_get('/(?:"|\&t=([^(\&|$)]+)/', $html);
170
$video_formats = preg_get('/\&url_encoded_fmt_stream_map=([^(\&|$)]*)/', $video_player);
$videoFormats = preg_get('/(?:"|\&url_encoded_fmt_stream_map=([^(\&|$|\\)]+)/', $html);
}
171
172
if ( empty($video_formats) )
if (empty($videoTicket))
173
174
$video_formats = preg_get('/\&fmt_url_map=([^(\&|$)]*)/', $video_player);
$videoTicket = preg_get('/\"t\":\s*\"([^\"]+)\"/', $html);
175
$videoFormats = preg_get('/\"url_encoded_fmt_stream_map\":\s*\"([^\"]+)\"/', $html);
176
177
178
if (!empty($videoTicket) && !empty($videoFormats))
179
// Video title
180
$video_title = preg_get('/<title>(.*)<\/title>/Usm', $html);
181
$videoTitle = preg_get('/<title>(.*)<\/title>/Usm', $html);
$video_title = 'video';
182
$videoTitle = str_replace(' - YouTube', '', $videoTitle);
183
$videoTitle = 'video';
184
// Available formats:
185
// 5 - FLV 240p
186
...
// 34 - FLV 360p
189
// 35 - FLV 480p
190
187
// 37 - MP4 1080p (HD)
191
188
// 38 - MP4 Original (HD)
192
// 38 - MP4 4K (HD)
// 43 - WebM 360p
193
// 44 - WebM 480p
194
// 45 - WebM 720p (HD)
195
196
// Parse fmt_url_map
197
// Parse the formats map
$video_url = array();
$sep1 = '%2C';
198
$sep2 = '%26';
199
$sep3 = '%3D';
200
if ( strpos($video_formats, ',') !== false ) // new UI
201
if ( strpos($videoFormats, ',') !== false )
202
$sep1 = ',';
203
$sep2 = '\\u0026';
204
$sep2 = (strpos($videoFormats, '&') !== false ? '&' : '\u0026');
$sep3 = '=';
205
if ( strpos($video_formats, '&') !== false )
$sep2 = '&';
206
$video_formats_group = explode($sep1, $video_formats);
207
for ($i=0; $i<count($video_formats_group); $i++)
208
$videoUrl = array();
209
$videoFormatsGroup = explode($sep1, $videoFormats);
210
for ($i=0; $i<count($videoFormatsGroup); $i++)
211
$video_formats_elem = explode($sep2, $video_formats_group[$i]);
212
$videoFormatsElem = explode($sep2, $videoFormatsGroup[$i]);
$url = explode($sep3, $video_formats_elem[0]);
213
if (count($videoFormatsElem) < 5) continue;
$itag = explode($sep3, $video_formats_elem[4]);
214
$partialResult1 = explode($sep3, $videoFormatsElem[0]);
$video_url[$itag[1]] = rawurldecode(rawurldecode($url[1]));
215
if (count($partialResult1) < 2) continue;
$video_url[$itag[1]] = str_replace('\/','/',$video_url[$itag[1]]);
216
$url = rawurldecode(rawurldecode($partialResult1[1]));
$video_url[$itag[1]] = str_replace('\u0026','&',$video_url[$itag[1]]);
217
$url = str_replace('\/','/',$url);
218
$url = str_replace('\u0026','&',$url);
219
$partialResult2 = explode($sep3, $videoFormatsElem[4]);
220
if (count($partialResult2) < 2) continue;
221
$itag = $partialResult2[1];
222
if (strpos($url, 'http') == 0) // validate URL
223
$videoUrl[$itag] = $url;
224
if (!isset($video_url[18]))
225
send_to_log(6,'Available YouTube formats for : '.$youtube_url, $videoUrl);
// Add standard MP4 format (fmt18), even if it's not included
$video_url[18] = 'http://www.youtube.com/get_video?fmt=18&video_id='.$video_id.'&t='.$video_ticket.'&asv=3';
send_to_log(6,'Available YouTube formats for : '.$youtube_url, $video_url);
226
// Determine whether to request the HD format
227
$fmt = 18;
228
if ( get_sys_pref('YOUTUBE_HD','YES') == 'YES' && isset($video_url[22]) )
229
if ( get_sys_pref('YOUTUBE_HD','YES') == 'YES' && isset($videoUrl[22]) )
$fmt = 22;
230
231
// Store parsed details in $_SESSION to avoid reparsing.
232
if ( !empty($video_ticket) )
233
if ( !empty($videoTicket) )
234
$_SESSION["stream_url"] = $video_url[$fmt];
235
$_SESSION["stream_url"] = $videoUrl[$fmt];
$_SESSION["stream_filename"] = $video_title.'.mp4';
236
$_SESSION["stream_filename"] = $videoTitle.'.mp4';
$_SESSION["stream_id"] = $video_id;
237
$_SESSION["stream_id"] = $videoId;
238
else
239
240
{{$video_id = $_REQUEST["youtube_id"];$videoId = $_REQUEST["youtube_id"];$youtube_url = 'http://www.youtube.com/watch?v='.$video_id;$youtube_url = 'http://www.youtube.com/watch?v='.$videoId;// Version 1.2.5 : Date 2011-08-04// Version 1.3.8 : Date 2012-02-09$video_player = preg_get('/<div id="watch-player".*>(.*)<\/div>/Usm', $html);if (!empty($html))if ( !empty($video_player) ){{$video_ticket = preg_get('/\&t=([^(\&|$)]*)/', $video_player);$videoTicket = preg_get('/(?:"|\&t=([^(\&|$)]+)/', $html);$video_formats = preg_get('/\&url_encoded_fmt_stream_map=([^(\&|$)]*)/', $video_player);$videoFormats = preg_get('/(?:"|\&url_encoded_fmt_stream_map=([^(\&|$|\\)]+)/', $html);}if ( empty($video_formats) )if (empty($videoTicket)){{$video_formats = preg_get('/\&fmt_url_map=([^(\&|$)]*)/', $video_player);$videoTicket = preg_get('/\"t\":\s*\"([^\"]+)\"/', $html);}$videoFormats = preg_get('/\"url_encoded_fmt_stream_map\":\s*\"([^\"]+)\"/', $html);}{$video_title = preg_get('/<title>(.*)<\/title>/Usm', $html);$videoTitle = preg_get('/<title>(.*)<\/title>/Usm', $html);$video_title = 'video';$videoTitle = str_replace(' - YouTube', '', $videoTitle);$videoTitle = 'video';// 38 - MP4 Original (HD)// 38 - MP4 4K (HD)// Parse fmt_url_map// Parse the formats map$video_url = array();if ( strpos($video_formats, ',') !== false ) // new UIif ( strpos($videoFormats, ',') !== false ){{$sep2 = '\\u0026';$sep2 = (strpos($videoFormats, '&') !== false ? '&' : '\u0026');if ( strpos($video_formats, '&') !== false )$sep2 = '&';$video_formats_group = explode($sep1, $video_formats);for ($i=0; $i<count($video_formats_group); $i++)$videoUrl = array();$videoFormatsGroup = explode($sep1, $videoFormats);for ($i=0; $i<count($videoFormatsGroup); $i++){{$video_formats_elem = explode($sep2, $video_formats_group[$i]);$videoFormatsElem = explode($sep2, $videoFormatsGroup[$i]);$url = explode($sep3, $video_formats_elem[0]);if (count($videoFormatsElem) < 5) continue;$itag = explode($sep3, $video_formats_elem[4]);$partialResult1 = explode($sep3, $videoFormatsElem[0]);$video_url[$itag[1]] = rawurldecode(rawurldecode($url[1]));if (count($partialResult1) < 2) continue;$video_url[$itag[1]] = str_replace('\/','/',$video_url[$itag[1]]);$url = rawurldecode(rawurldecode($partialResult1[1]));$video_url[$itag[1]] = str_replace('\u0026','&',$video_url[$itag[1]]);$url = str_replace('\/','/',$url);$url = str_replace('\u0026','&',$url);$partialResult2 = explode($sep3, $videoFormatsElem[4]);if (count($partialResult2) < 2) continue;$itag = $partialResult2[1];if (strpos($url, 'http') == 0) // validate URL$videoUrl[$itag] = $url;if (!isset($video_url[18]))send_to_log(6,'Available YouTube formats for : '.$youtube_url, $videoUrl);{// Add standard MP4 format (fmt18), even if it's not included$video_url[18] = 'http://www.youtube.com/get_video?fmt=18&video_id='.$video_id.'&t='.$video_ticket.'&asv=3';}send_to_log(6,'Available YouTube formats for : '.$youtube_url, $video_url);if ( get_sys_pref('YOUTUBE_HD','YES') == 'YES' && isset($video_url[22]) )if ( get_sys_pref('YOUTUBE_HD','YES') == 'YES' && isset($videoUrl[22]) )if ( !empty($video_ticket) )if ( !empty($videoTicket) ){{$_SESSION["stream_url"] = $video_url[$fmt];$_SESSION["stream_url"] = $videoUrl[$fmt];$_SESSION["stream_filename"] = $video_title.'.mp4';$_SESSION["stream_filename"] = $videoTitle.'.mp4';$_SESSION["stream_id"] = $video_id;$_SESSION["stream_id"] = $videoId;{{