Archive for October, 2009

Youtube video in AS3 FLVPlayback Update(2)

After the update of yesterday,

Youtube changed the way on how to retreive the video from it’s html page again.
This implies, that the 2 old versions of the proxy no longer worked and that videos could no longer be retreived.

After a quick peek, the solution was rather simple.
Here’s the code for the new and improved proxy:

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
<?php
    $id = trim($_REQUEST['id']);
 
    $url = "http://www.youtube.com/watch?v=" . $id . "&fmt=18";
 
    $ch = curl_init();
 
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 
    $info = curl_exec($ch);
 
    if (!preg_match('#\'SWF_ARGS\': (\{.*?\})#is', $info, $matches))
    {
        echo "Check the YouTube URL : {$url} <br/>\n";
        die("Couldnt detect swfArgs");
    }
 
    if (function_exists(json_decode)) # >= PHP 5.2.0
    {
	$swfArgs = json_decode($matches[1]);
        $video_id = $swfArgs->video_id;
        $t = $swfArgs->t;
    }
    else
	{
        preg_match('#"video_id":.*?"(.*?)"#is', $matches[1], $submatches);
        $video_id = $submatches[1];
 
        preg_match('#"t":.*?"(.*?)"#is', $matches[1], $submatches);
        $t = $submatches[1];
    }
 
	curl_close($ch);
 
	$fullPath = "http://www.youtube.com/get_video.php?video_id=" . $video_id . "&t=" . $t; // construct the path to retreive the video from
 
	$headers = get_headers($fullPath); // get al headers from the url
 
	foreach($headers as $header){ //search the headers for the location url of youtube video
		if(preg_match("/Location:/i",$header)){
			$location = $header;			  
		}
	}
 
	header($location); // go to the location specified in the header and get the video
?>

That’s all there is to it.

This file will also be updated again in the source and files download of the original post.

Tags: , , , , ,

9 Comments



SetPageWidth