« Nanocast alpha is released | Main | Torrent RFC or specfication? »
November 26, 2006
YouTube ActionScript 2.0 API
I had small requirement of YouTube API in one of my personal ActionScript 2.0 project, I couldn't find any ActionScript 2.0 version of YouTube API. I wrote it couple of hours.
You can look at source code and example (app I made to test it, quick and dirty) and you can download it with example files.
You would notice, to construct FLV URL, I am doing client side parsing of YouTube html page to find out "t" param. This doesn't work in Internet Explorer 6.0 (IE 6) because of a bug where IE 6.0 doesn't uncompress gzipped page if requested through LoadVars?
In production mode, I would prefer server-side proxy to find out "t" param because it's optimized:
- Would work in IE 6.0 and other browsers.
- Doesn't require loading YouTube html page ( > 20 KB) and parsing.
Update: I am using following PHP code to get FLV from YouTube asset servers.
<?php
// Youtube FLV Fetcher
// Responds to both HTTP GET and POST requests
// Author: Abdul Qabiz (abdul dot qabiz at gmail dot com)
//
// Description:Gets the path of FLV from YouTube URL
// Is it a POST or a GET?
if (isset($_GET['url']))
{
$url = $_GET['url'];
$v = $url;
$v = preg_split ("/\?v=/", $v);
//youtube video-id
$v = $v[1];
//hardcoding URL here, couldn't figure how to make CURL follow 303 redirection
//seems something wrong with libCurl or my knowledge...
$url = "http://www.youtube.com/watch?v=" . $v;
//Start the Curl session
$session = curl_init();
curl_setopt ($session, CURLOPT_URL, $url);
//you might want to turn it to false, in case you want
//to content to client (flash etc).
curl_setopt($session, CURLOPT_HEADER, true);
//not working for me :(
curl_setopt($session, CURLOPT_FOLLOWLOCATION, true);
//curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
// Make the call
$response = curl_exec($session);
if (preg_match_all("/&t=[^&]*/", $response, $matches))
{
$t = $matches[0][0];
$t = preg_split("/=/", $t);
//youtube t param
$t = $t[1];
//construct the flv-url
$youtubeVideoPath = "http://www.youtube.com/get_video.php?video_id=" . $v . "&t=". $t . "&.flv";
//redirect to flv - you can replace this code with echo/print
//to return the path to client (browser/flash)
header ("Location: $youtubeVideoPath");
}
else
{
echo "null";
}
curl_close($session);
}
else
{
echo "No YouTube URL to process";
}
?>
Technorati tags: youtube, flv, fetcher, download, php, curl, script
Posted by Abdul Qabiz at November 26, 2006 02:36 AM
Comments
Dude, this is the bomb, I didn't know that GooTube allowed that.. tho I've seen http://www.neave.tv but it never made me wonder what's possible.
Anyways - great work, I love it :)
Posted by: Jare at November 26, 2006 05:18 AM
I saw the demo, this is is awesome. I can already think of good uses for this.
Posted by: Brajeshwar at November 26, 2006 01:36 PM
Great work! and another actionscript site/resource for my bookmarks ;)
Ps: Sorry for my bad english
Posted by: Marks at December 6, 2006 07:30 PM
Guys,
It seems YouTube has done some changes in their crossdomain.xml. So example posted here might not work anymore. You would need a server-side proxy to make it work. I would update it asap.
-abdul
Posted by: Abdul Qabiz at December 7, 2006 10:53 PM
is it possible make it in MX2004? YouTube is able to visit with IE7 and Flash Player 7,0,68,0, and Flash MX Pro 2004 also have player 7 buit in.
I'm trying to load a direct flv's link into a movieclip, but doesn't work. i guess its wrong way to load the video, isn't it? can you give any hints. thanks.
Posted by: skyA at February 28, 2007 02:51 AM
good work buddy ...
i was looking for a way to stream their tubes into a custom flv player ...
time to test it now i guess .
thanks for sharing ,
Moo
Posted by: Mohammed Zainal at May 14, 2007 04:00 PM
Is the demo is working fine ? Does youtube decide to change is way to protect flv ?
Posted by: MG at June 7, 2007 11:24 PM
The demo seems to not working anymore, is there any changes?
Posted by: Transformer at July 1, 2007 04:59 PM
@Transformer: Yeah, demo doesn't work because YouTube changed crossdomain.xml, hence, YouTube API is unusable for Flash clients without a server-side proxy. I need to change the demo to use server-side proxy.
Thanks
-abdul
Posted by: Abdul Qabiz at July 1, 2007 06:41 PM
someone has solved the cross domain thing here
http://www.zainals.com/blog/2007/07/04/play-youtube-videos-in-flash-tutorial/
Posted by: web design brighton at August 7, 2007 06:58 PM
how can i make an as2 example using a server side proxy, ive been trying all day but cant get it working, i can make a php script that will download the flv but cantget it to stream to the flv player
Posted by: web brighton at August 8, 2007 04:47 AM
@web brighhton: Thanks for that link, he is using server-side script to invoke REST API but that would not be required if YouTube can host a crossdomain.xml that allows other domains, probably they would do it soon.
Coming back to your other comment, I think, it's quite straight-forward, you just need to pass the php path to NetStream instance to play. Can I look at your code (the simplest but complete form) to see how you doing?
-abdul
Posted by: Abdul Qabiz at August 20, 2007 11:15 PM
hello this is very nice piece of flash api here but i cant seem to figure out how to get the title to display after searching by tag or any title in the videolist data box any help please
Posted by: keagan at September 22, 2007 08:05 PM
HI
I have made a similar kind of application in flash 8
and it is working perfectly as a desktop application.But when i put it on a server a try to run it on internet explorer as html i am not gettin any output.Can anyone please help me out with this problem
Posted by: Arun at September 27, 2007 01:14 PM
Where can i get an API for youtube ?
Posted by: Zalmay at October 3, 2007 06:25 PM
Could any one show me a working way to get actual youtube flash file url using php? I want to automate the process of getting actual url of youtube flash file url using php and use it in jw flash player but so far no luck in getting the actual flash file url using php. Hope some one help me here.Thanks
Posted by: method at October 16, 2007 12:08 PM
heres another way to display a youtube clip in flash
example
[url]http://jaxxrr.110mb.com/other/fs3.html[/url]
sourcecode
[url]http://jaxxrr.110mb.com/other/flash-fullscreen-youtube.zip[/url]
the fullscreen part is nothing special really.. one line of script on a button
on (release) {
Stage.displayState = "fullScreen";
}
but you also need to edit the html file and set the allowFullScreen parameter from false to true, it is there just set it to true (don't write it twice like I did :) )
Also make sure your file does'nt get overwritten.. go to publish settings and uncheck html
so either
...
or
AC_FL_RunContent( ... "allowFullScreen", "true", ... )
this is also in flash CS3 help :)
cheers
Posted by: jaxx at December 9, 2007 03:15 PM
Hi Abdul,
The Source link isn't working anymore.
Can U please fix this?
ma3a selama,
Hassan
Posted by: Hassan at February 3, 2008 03:24 PM
@Hassan: Thanks a lot, I just checked, some how directory owner and permission have been changed, I have filed a ticket for my web-host. I hope, it would be fixed.
Thanks
-abdul
Posted by: Abdul Qabiz at February 3, 2008 03:55 PM
does anyone a script that works for youtube anymore?
the script i have will no longer play the movie, it displays the player but it just wont play. Email me if you have any info.
Thanks in advance,
Ben
Posted by: Ben at February 4, 2008 08:31 PM
+1
If someone fixed this issue, could you please send me a mail or post the solution..
Thx in advance,
Charlie
Posted by: charlie at February 13, 2008 06:20 AM
What problem are you guys facing? @Ben which script?
-abdul
Posted by: Abdul Qabiz at February 13, 2008 06:24 AM
I use rayzz script.
And cannot view the video embedded from youtube.
I cannot "upload" the youtube url as well.. so someone gave me this link and told me that I have to put my own dev id on this line:
$youtube_xml = $this->_GetPage('http://youtube.com/api2_rest?method=youtube.videos.get_details&dev_id=MYDEV-ID&video_id='.$youtube_video_id);
i did it, but it still not working? And agriya tech support never reply.. so what to do next??
Posted by: charlie at February 13, 2008 07:11 AM
Hi abdul,
You script is working when I run it from Flash 8.
When I try to open it within the IE7.0 or firefox, the player can't connect the youtube.
As I understood, there is a cross domain issue (correct me if I'm wrong).
Please explain to me how do I use your script now ? How can I make it work on Flash 8 action script 2.0 ?
Please advice,
Sallaboy
Posted by: sallaboy at March 6, 2008 04:23 AM
hi abdul,
this link of you app :
http://www.abdulqabiz.com/files/YouTube_API_ActionScript2/YouTubeAPITest.html
doesn't work anymore ...
Posted by: sallaboy at March 6, 2008 04:28 AM
yes... doesn't work anymore...
maybe Youtube find a way to make impossibile to sniff the FLV's url! :(
Posted by: flashzen at June 13, 2008 08:47 PM