Rich Rodecker’s blog on flash, flex, actionscript, javascript, and php, with a dash of randomness
FLVFetcher – Directly download youtube and google flv’s into flash
Here’s my take on downloading the flv’s directly from youtube and google to stream into flash. It’s made up of two parts – an AS 2 class called FLVFetcher and a php script.
As of right now, to get the path to the flv file you simply add and event listener for the ON_URL event, then all you need to do is pass the video id of the file you wish to load.
For youtube videos, its that is the value of the ‘v’ variable in a url like:
and for google video, its the value of the docid variable in a url like:
FLVFetcher.fetchByID(id:String, videoType:String);
The class has two constants you can use for the video type, either FLVFetcher.YOU_TUBE or FLVFetcher.GOOGLE.
On the php side, it uses a custom HTTPRequest class to get around server configurations where remote file access is disabled.
You can check it out here and download a zip of the source here.
Some example code:
import com.f1fd.flv.FLVFetcher;
import com.f1fd.flv.FLVFetchEvent;
import mx.utils.Delegate;
// for google, you jsut need the 'docid' variable from the url
var googleID = "-4329042477720699378";
// for youtube vids, all you need is the 'v' variable
var youTubeID="J0TiFPynBgI";
var fetcher = FLVFetcher.getInstance();
this.onEnterFrame = function(){
init();
delete this.onEnterFrame;
}
function init(){
FLVFetcher.PHP_PATH = 'get_video.php';
fetcher.addEventListener(FLVFetchEvent.ON_URL, Delegate.create(this, onURL));
fetcher.addEventListener(FLVFetchEvent.ON_ERROR, Delegate.create(this, onError));
google_button.addEventListener ('click', Delegate.create(this, onGetGoogleClick));
youtube_button.addEventListener ('click', Delegate.create(this, onGetYouTubeClick));
}
function onGetGoogleClick(){
fetcher.fetchByID(googleID, FLVFetcher.GOOGLE)
}
// change second param to FLVFetcher.GOOGLE for google videos
function onGetYouTubeClick(){
fetcher.fetchByID(youTubeID, FLVFetcher.YOU_TUBE)
}
function onURL(evt){
message_txt.text = 'The URL is : '+newline+evt.data;
my_vid.contentPath = evt.data;
}
function onError(evt){
}
| Print article | This entry was posted by rich on August 16, 2007 at 5:54 pm, and is filed under flash, php. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
about 5 years ago
nice work.
i was wondering though :
Is this “legal” / “fair” ?
Can you use youtube /google video to host / stream videos for your own website this way ????
Feels like stealing somebody else’s bandwidth and not giving them the benefit of making a buck through advertising.
just wondering, you know.
I would definitely use this a lot if it would turn out to be ok.
about 5 years ago
striclty speaking…no, so use at your own risk. A quick perusal of youtube’s TOS clearly states that you can’t access the contents directly:
However, this is obviously not the first time someone has done this…take neave.tv for example. So, i’m not breaking any new ground here, but it’s up to you if you want to use this or not.
about 5 years ago
Cool beans.
This might be a dumb question (I’m not AS wizard) but how would I load a YouTube video directly into the player automatically (without the user having to click anything)?
For instance: I want to have this video http://youtube.com/watch?v=A4uyN5rQbbU appear when someone goes to the page.
Cheers.
about 5 years ago
you can use most of the code above, except you wouldn’t need the lines for the buttons and video ID’s. Then in the init() method, add this line:
fetcher.fetchByURL("http://youtube.com/watch?v=A4uyN5rQbbU")about 5 years ago
Awesome!! Thanks rich. Much appreciated.
about 5 years ago
how someone wondered if it’s fair or not?
IT’S FANTASTIC!! THANKS FOR THE CODE!
about 5 years ago
I have a problem to deploy it on my website. I downloaded the source and uploaded to my site without alterations, yet it didn’t work.
Could it have anything to do with php version or firewall settings?
about 5 years ago
While awaiting the moderation I came up with another question:
will this work if I load FLVFetcher.swf into another swf?
about 5 years ago
Here is a nice websitbe where you can search, view and download YouTube videos :
ilikeyouyube.com
Enjoy.
about 5 years ago
igr – you may possible have a problem with php version, although I can’t really say for sure without knowing exactly where the error is occuring. Also, it’s not the swf that makes it work, its the actionscript classes, so if you are using the classes you should be able to use them anywhere.
pat – yeah, there are tons of sites and apps that will grab the video for you, but this is more for flash developers who are trying to play the flv’s in their apps.
about 5 years ago
the url for the source don’t work!
about 5 years ago
I tried to download the source files but the link is not working…
about 5 years ago
the link to the source has been fixed.
about 5 years ago
Hi,
your code work inseide ide (standalone) but when i publish on the web the flv isn’t loaded. Why???
bye SK
about 5 years ago
Siciliankiòòa – that sounds like a cross-domain issue.
about 5 years ago
Rich – I suppose! If you take a look the crossodomain policies , you can see youtube and google domain only (http://www.youtube.com/crossdomain.xml). Do you know a possible solution?
about 5 years ago
I downloaded the code and I can not get it working.
Looks like the get_video.php can not communicate back to the flash.
Is there special any server settings involved here?
I use Apache, PHP 5+.
Thanks
about 5 years ago
I used php 4 for this. You may need to tweak for php 5.
about 5 years ago
After the flash calls the get_video.php I got a popup window with the return value. How can I disable this window?
Thanks
about 5 years ago
Can you tell me how to change the string in the text input (by default is http://youtube.com/v/zk5w9QPf6ek)???
I’m a little confused… thanks…
about 5 years ago
thanks for the GREAT post! Very useful…
about 5 years ago
Great tool. However, the google flv fetcher does not seem to work. I would be happy if you post a fix for that.
Thank you.
about 5 years ago
hi,
sorry i can’t seem to find the source code for flash 8. can someone prvide me the link please
cheers!
about 5 years ago
Hi there,
the .fla appears to be corrupted and can’t open it
Can someone help me to find out a working source?
about 4 years ago
I keep getting the following compiler error:
FLVFetcher.as, Line 34 1153: A constructor can only be declared public.
about 4 years ago
Hi,
I would like to now if is it possible to load my own .flv files. thanks guys…
about 4 years ago
Hey guys,
Why when I upload the files to my website….doesn’t work?!! At my local machine works great, but when is online…doesn’t work NOTHING!!!!
about 4 years ago
Hi!
Have this player the seekTo function?
I make an example!
I want to create a no flash botton that link directly to 30th second of the video?
Is it possible?
Thank’s
about 4 years ago
I think youtube has that ability in their API, but this isn’t maintained anymore. Feel free to grab the code and try and get that working.