Home > Flash and Actionscript, Web 2.0 > YouTube ActionScript 2.0 API

YouTube ActionScript 2.0 API

November 26th, 2006 Leave a comment Go to comments

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:

  1. Would work in IE 6.0 and other browsers.
  2. 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, curlscript



  • Sankar
    this method no longer works please check ,Youtube changed the path construction
  • yup! YouTube changed it long time back. I am yet to spend sometime to figure out new ways. However, if you have looked at Chromeless Player (API), you don't even need this stuff. You can use API to do most of things.

    And yeah, workarounds/hacks don't have long life.
  • Swami Charan
    hi abdul,

    this script was working for sometime....but now it stopped working...can u please fix this script?

    -Swami
  • Mirko
    Hi Abdul,
    I am in a need for a AS2 YouTube API implementation and came across your page.
    I wanted to check out your AS2 version of the YouTube API but unfortunately the source code, example and download links on this page are broken.

    Do you have a working download link for your sources and examples and moreover is it still working with the current YouTube API?

    Thanks
    Mirko
  • @Mirko - Thanks for letting me know, I fixed it.
  • Hi! i am using this code.But in my server it is not working.While it is working on local server .plz help me.
    Thanks & regards
    Rajrupa
  • Hi Guys
    Things have changed a lot since I wrote above code. You can probably check out YouTube's new API and Chromeless player.
    There are various code available online that even allows to embed chromless player in AS3/Flex2 apps.
    I am not sure, how many people still use ActionScript 2.0? If you use, I can try figuring out a way to update above script.
    Thanks
    -abdul
  • pokjumibutherius
    Hi Abdul, please help us. It doesnt work anymore, i dunno what to do. I need some help! =(
  • All my youtube videos stoped working too.
    I don't have a lot of them so I will delete them.
    For those who want youtube videos to populate your site you can always use grabit.to and then upload it as your own file.
    Hope I helped.
    Ps. A fix for Rayzz would be nice too...
  • Ferencz Bartha
    Hi Abdul
    Do you know how we can load and play in flash the one whole youtube playlist?
    To have a player like the youtube can creates?
    Or somehow plaste the embed player from the plalist in flash?
    :)
    Thx!
    Feri
  • yes... doesn't work anymore...
    maybe Youtube find a way to make impossibile to sniff the FLV's url! :(
  • sallaboy
    hi abdul,
    this link of you app :
    http://www.abdulqabiz.com/files/YouTube_API_ActionScript2/YouTubeAPITest.html
    doesn't work anymore ...
  • sallaboy
    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
  • 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??
  • What problem are you guys facing? @Ben which script?
    -abdul
  • +1
    If someone fixed this issue, could you please send me a mail or post the solution..
    Thx in advance,
    Charlie
  • Ben
    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
  • @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
  • Hassan
    Hi Abdul,
    The Source link isn't working anymore.
    Can U please fix this?
    ma3a selama,
    Hassan
  • jaxx
    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
  • method
    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
  • Zalmay
    Where can i get an API for youtube ?
  • Arun
    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
  • 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
  • @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
  • 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
  • @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
  • Transformer
    The demo seems to not working anymore, is there any changes?
  • MG
    Is the demo is working fine ? Does youtube decide to change is way to protect flv ?
  • 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
  • skyA
    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.
  • 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
  • Marks
    Great work! and another actionscript site/resource for my bookmarks ;)
    Ps: Sorry for my bad english
  • I saw the demo, this is is awesome. I can already think of good uses for this.
  • Jare
    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 :)
blog comments powered by Disqus