SwissCenter is an open source project powered by Assembla

Assembla offers free public and private SVN/Git repositories and project hosting with bug/issue tracking and collaboration tools.

Commit 1684

User picture
  • Author: Pernod
  • 2012-02-11 19:02 (about 2 years ago)

Updated YouTube streaming due to site changes at YouTube.

Files Affected

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