Order Now AdSolution Sign Up | Login » Bits on the Run Sign Up | Login »

Forums

/

Youtube API HELP!!!

57 replies [Last post]

hello, i have made a youtube qpi downloader and i cant get the link to run on the flash player this is the script:

<?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";
}
?>

so how do you make this script run with the jw flv player?????

oh and if you want to no the way to get the mp4 download link this is it:

<?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 . "&fmt=18";
           
//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";
}
?>

Player code:

  file:                  encodeURIComponent('http://my.domain.com/path/YouTube_HQ.php?url=http://www.youtube.com/watch?v=ctf1Ts2k4oA'),
  type:                 'video',

THANK YOU!!!!!!!! your my god!!!!! lol, i have been trying to fix it for ages :)

can this work on the mp4 version???

"can this work on the mp4 version???"

Hey man, it's your code, I should be asking you that question. :D

In my post above, the "HQ" in "YouTube_HQ.php" refers to your High Quality script, using "&fmt=18".

So the answer is, "Yes, it works".

ok lol, my flv player scipt is made differntly to this one cz i have changed some stuff, i will post the player code in abit, and could you help me add that little script you gave me and add it into the video script i have :)

i descoverd how to get the mp4 video by azident cz i added that &fmt=18 to make the flv high quality and i descoverd that was the mp4 download lol

Yeah, post your player code and I'll show you how to put the link into it ...or whatever?

file: encodeURIComponent('http://my.domain.com/path/YouTube_HQ.php?url=http://www.youtube.com/watch?v=ctf1Ts2k4oA'),
type: 'video',

Hi any idea to get this into a html/php code? I am using swf tools so there's a filter code for the jw flv player

For example I am able to show the player with,

<swf file="http://www.youtube.com/watch?v=VX_0wi1vXCA">

How do I implement the above script to get the hq path? thanks!

the hq path is just the same but with &fmt=18 at the end

nice script, thank you

perhaps you know how to get back the youtube logo on the video? ;-)
please mail your answer to: basi (at) japsen dot org

greetings

Hey Klink. We use the Wordtube plugin for Wordpress, and tried to implement your script, but it's not working.

http://www.mydomain.com/youtube_hq.php?url=http://www.youtube.com/watch?v=a5mwWPyuBIc&fmt=18

This is the url I enter, it pulls up the video, but the logo is still there and its not the HQ version.

Have you had any experience with this plugin on Wordpress and how to intergrate your script with it?

@Jay,

I haven't tried PS3ubo(.)co(.)nr's YouTube HQ script in Wordtube, but it does work in normal player code.

For instance, swfobject v2.1 code:

'file': encodeURIComponent('http://www.mydomain.com/youtube_hq.php?url=http://www.youtube.com/watch?v=a5mwWPyuBIc'),

You don't need the "&fmt=18" on your request URI, because the script adds that when it formulates the new URI.

can someone explain exactly how to use this for noobs like me please? what does it do exactly? how to impliment it on a website?
would be greatful! The low res videos are horrible:(

I've having trouble getting this to work. I am using JW Player 4.2, embedding with SWFObject 1.5

It looks like the 'file':
code posted above is for SWFObject 2.1, and it seems like a completely different syntax. How would I go about calling the HQ quality video using SWFObject 1.5?

Please help...!

guys could someone please explain how this works step by step? how do you implement it, sorry i am not a wizz here:(
Would it work on youtube playlist?

You should upgrade your player code to use swfobject v2.1. It does a better job of generating the object element for embedding the Flash Player and you can load it from Google, possibly making your page load faster (it may already be in your client's cache). See this thread: http://www.jeroenwijering.com/?thread=14032

However, if you choose to stay with SWFObject v1.5, then the equivalent code would be:

so.addVariable('file', encodeURIComponent('http://www.mydomain.com/youtube_hq.php?url=http://www.youtube.com/watch?v=a5mwWPyuBIc'));

If you need more help, post your player code or a link to your test page.

Thanks for the help, kLink. I am on Wordpress, and was not able to get the JW Player working with SWFObject 2.1. All of the instructions online seem to be for 1.5, and even the LongTail advertising solution only spits out code for 1.5, it seems. I will switch up once I'm more confident with the code and know exactly how to play with it.

For anyone else that needs it, here is my complete working SWFObject 1.5 High Quality YouTube embed code:

<div id='play-youtubeHQ'>TO BE REPLACED</div>
<script type='text/javascript'>
var s1 = new SWFObject('http://www.example.com/video/player.swf','player','640','360','9');
s1.addParam('allowfullscreen','true');
s1.addParam('allowscriptaccess','always');
s1.addVariable('file', encodeURIComponent('http://www.example.com/video/youtubehq.php?url=http://www.youtube.com/watch?v=XXXXXXXXXXXXXXX'));
s1.addVariable('type', 'video');
s1.write('play-youtubeHQ');

note the second-last line there -- without it, you just get the bad quality vid.

I have made a video testing page on my website where I am testing the various methods of embedding with JW Player.

Please check it out:

http://www.engvid.com/ielts-video/
the password is: videotest

- The first video there is the High Quality video pulled with the hack above from YouTube. Works perfectly, I think.

- After that is the same thing, but me resizing the video viewport for standard size, thought it is a widescreen video, to check how it behaves. Seems good.

- Next is the OFFICIAL API method of pulling the low quality vid from YouTube. Here there are a couple problems: First, the YouTube logo is too big. I've heard this is a mistake on their part that will be fixed, though, so fine. Second, the quality is shite. Compare it to the standard YouTube player embedded at the bottom of the page. The YouTube player does a much better job of smoothing the low-quality video. Any way to fix this? It's my main concern, really, because I need users to have the low-quality option, as my analytics indicate that many of them are still on low-bandwidth connections.

- Next I've tried to use the hack at the top of this page to embed the low quality video from YouTube (thinking perhaps it would appear more like the embedded low quality YouTube video)... but it appears that the code that script spits out (the flv script, not the mp4 one) is invalid. The URL it is trying to hit is :
http://www.youtube.com/get_video.php?video_id=dJg138YlwvA&t=OEgsToPDskKdjdDqsUoS0Nk1hVZzo21z.flv
but the actual working URL of the LQ vid as revealed by keepvid.com is:
http://www.youtube.com/get_video?video_id=dJg138YlwvA&t=OEgsToPDskJ-amRLRZo2GXxjOrkc3YAv
Note the different string towards the end. Is this just a case of YouTube having switched things up to break compatability with apps that leech the file off their servers?

- Finally is the standard embedded LQ video from YouTube in their standard player. Compare the quality to the JW player low quality... YouTube's is unfortunately much better.

And just one small question, exposing my complete ignorance of the original PHP script posted here: is this using my server as a proxy? By this I mean... I am going to be serving up all that High Quality video bandwidth with this method? I'm pretty sure I can tell from the code that it is pulling directly from YouTube, but I want to be certain.

@Josh,

The script does a 302 redirect:header ("Location: $youtubeVideoPath");
The video comes directly from YouTube to the client, so the traffic is YouTube's.

Also, I've noticed that even when i don't specifically request the HQ, most of the time I'm getting an MP4 file that is about 2.5X the size of the old FLV, even for files that have been on YouTube for 2+ years.

If you want to embed high quality video (for thuse videos that have their HQ counterparts) use httpvh:// instead (Video High).

If you want to embed HD Quality (DVD quality 720p) video use httpvhd:// instead (Video High Defintion).

THis is increadble! thanks! if anyone here is good with the youtube api and php please email meh i need some help with a diffrent code. thanks!

assault14@gmail.com

Is there any way of making this working in an XML playlist? I just did a side-by-side test of this video with the API and with just a normal link to the video: http://www.youtube.com/watch?v=FENsjBYCiZE

Here is how the playlist is:

<track>
<title>HQ Test</title>
<annotation>api</annotation>
<location>ythq.php?url=http://www.youtube.com/watch?v=FENsjBYCiZE</location>
<info></info>
</track>

<track>
<title>HQ Test</title>
<annotation>normal</annotation>
<location>ythq.php?url=http://www.youtube.com/watch?v=FENsjBYCiZE</location>
<info></info>
</track>

Does the "encodeURIComponent" have to be in there somewhere? I'm using swfobject v2.1+

You don't need to use the encodeURIComponent() method in a playlist.

Both of your playlist tracks are the same. How are you doing a comparison?

I can't get it to work and I can't seem to find the error.
From a media page I click on an image, which opens the html containing the video in a modal window.

This is the code for the html page containing the video:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

<title>modal</title>
<style type="text/css">
html,body
{
margin:0;
padding:0;
}
div
{
width:480px;
height:405px;
background:#212121;
color:#fff;
}
div h1
{
top:50%;
text-align:center;
position:absolute;
margin:0 auto;
width:100%;
}
</style>
</head>

<body>
<div>

<script type='text/javascript' src='/embed/swfobject.js'></script>

<div id='play-youtubeHQ'>TO BE REPLACED</div>
<script type='text/javascript'>
var s1 = new SWFObject('/embed/player.swf','player','640','360','9');
s1.addParam('allowfullscreen','true');
s1.addParam('allowscriptaccess','always');
s1.addVariable('file', encodeURIComponent('/YouTube_HQ.php?url=http://www.youtube.com/watch?v=BpAO9a4z_aU'));
s1.addVariable('type', 'video');
s1.write('play-youtubeHQ');
</script>
</body>
</html>

the php I'm using is the top one the first one in PS3ubo(.)co(.)nr original post.

When the html opens all I get is the message "to be replaced".
Can someone help me with this one ?

@Nick,

Check:

http://my.domain.com/embed/swfobject.js
http://my.domain.com/embed/player.swf

by copying & pasting the URI in your browser. Adjust for your domain.

I did, they are the standard files. And I included the full url for these files.
Swfobject is version 2.1 and the player is the newest I could download here.
Do I need to add something in one of these files ?
The problem refuses to go away... Sorry if this sounds a noob question, I know quite a bit about programming but this one I can't figure out on my own.

OK, this is weird.
I tried using Josh's youtubehq.php and my video appeared, when I use mine I get the player but it won't connect me with youtube...

Arg...sorry I did a copy + paste + paste >.<

It's like this:

<track>
<title>HQ Test</title>
<annotation>api</annotation>
<location>ythq.php?url=http://www.youtube.com/watch?v=FENsjBYCiZE</location>
<info></info>
</track>

<track>
<title>HQ Test</title>
<annotation>normal</annotation>
<location>http://www.youtube.com/watch?v=FENsjBYCiZE</location>
<info></info>
</track>

bumpage

Test your YouTube video URI grabber script:http://my.domain.com/path/ythq.php?url=http://www.youtube.com/watch?v=FENsjBYCiZE
You should get a video file returned. If there are errors, fix them and re-test.

You also need:<meta rel='type'>video</meta> in EVERY track of your playlist.

I am in wordpress using wordtube.

Everything seems fine, I upload the youtube video, the preview-picture loads, also all the tags for the video. But as soon as I try to play the video the youtube logo shows, and in the player there is a sign with an "!" in and this text below: "YouTube error (video not found?):encodeURIComponent(\'http://my.domain.com/YouTube_HQ.ph"

It is the same which video I´m trying to add in the wordtube.

I have tried this one:
http://www.youtubemp4.com/video/futseY25trI.mp4
and it works fine except that the video load very slowly.

Any idea what´s wrong?

Fs

bumpage

@Fs,

Did you upload the correct version of the YouTube proxy, YT.SWF, to your server in the same directory as PLAYER.SWF???

Yes, I think I did. It´s the YT.SWF-file that followed in the Wordtube package - and I put it in the same directory as PLAYER.SWF.

But I can not find out the version of my YT.SWF.

Compare your filesize to the YT.SWF here: http://developer.longtailvideo.com/trac/browser/trunk/as3

Right-click on your YT.SWF, then select properties to see the exact filesize.

Okei, the info of my YT.SWF says 4kb (1 187 byte). Does that tell you anything?

Fs

The new YT.SWF seems to be 1,210 bytes.

From here: http://developer.longtailvideo.com/trac/browser/trunk/as3

Thanx alot.

Now I have replaced the old YT.SWF with the one you linked to. I got all the tags and everything seems fine, but when trying to play the video it does not start. I see the spinning wheel, and nothing happens. With the older version of the YT-file the video started, and stopped after a second or two.

I have saved the "youtube qpi downloader" from PS3ubo(.)co(.)nr on my server, but I have not installed the script for mp4. So I might have done something wrong.

I am not sure what to do? Any idea?

Anyone?

Ok, Ive been working on getting this to work for me FOREVER, and it still doesn't, when I go to the direct API link and put in the video URL, it comes up and fives me a "403 Forbidden" error and below says I don't have permission to view it (obviously) but when I take the link out and link directly to the API itself it says that there isn't a URL to process. I've checked the File & Directory Attributes/Permissions and its all set to what it should be. Any help?

Fatal error: Call to undefined function curl_init() in /www/localhost/flv/htdocs/youtube.php on line 19

<?php
$videoid=$_GET["videoid"];
$url="http://www.youtube.com/api2_rest?method=youtube.videos.get_video_token&video_id=$videoid";
$t = trim(strip_tags(@file_get_contents($url)));
$url = "http://www.youtube.com/get_video.php?video_id=" . $videoid . "&t=" . $t;
header("Location: $url");
?>

goatlord999, could you please explain how to get your code working?

@Steven,

First, you need a hosting service that allows external file access and that allows you to execute PHP scripts.

Next, place the PHP script on your server and call it from the player code like this:

so.addVariable('file', encodeURIComponent('http://www.mydomain.com/path/YouTube_Get_URI.php?videoid=FENsjBYCiZE'));

Adjust the domain, path, script name, and YouTube ID to yours.

From JW Player Quickstart info:
"If you want to play YouTube content in the player, you also have to copy the yt.swf file along with the player-viral.swf."

My question: How do you call the yt.swf file in the html code?

I am using mp3player v 3.5, swfobject v 1.5 (swfobject v 2.1 not compatible with OE when sending JW Player webpage in mail), and a 5 track playlist. No .php server access.

My html code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>Melody Festival 2009</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta content="mshtml 6.00.2900.5626" name=generator>
<meta content="Anders" name=author>
<script type="text/javascript" src="swfobject.js"></script>
<style type="text/css">
body {
margin: 0;
overflow: hidden;
background-color: #004d85;
}
#bkg {
left: 0px;
width: 100%;
position: absolute;
top: 0px;
height: 100%;
}
#swfcontent {
left: 50%;
margin-left: -200px;
margin-top: -122px;
width: 400px;
border: 1px #004d85 solid;
position: absolute;
top: 50%;
height: 244px;
}
p {
font: 14pt Verdana, sans-serif;
top: 40px;
position: relative;
height: auto;
text-align: center;
}
</style>
</head>
<body scroll=no>
<img id=bkg src="bg6212029.jpg">
<div id="swfcontent">
<p>You need to upgrade your Adobe Flash player<br><br><a href="http://www.adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p>
</div>

<script type="text/javascript">
// <![CDATA[

var so = new SWFObject("mp3player.swf", "player", "400", "244", "8", "expressInstall.swf");
so.addParam("scale", "exactfit");
so.addParam("allowscriptaccess", "always");
so.addParam("allownetworking", "all");
so.addParam("allowfullscreen", "true");
so.addParam("bgcolor", "#004d85");
so.addVariable("file", "playlist.xml");
so.addVariable("lightcolor", "0xff4040");
so.addVariable("backcolor", "0x004d85");
so.addVariable("frontcolor", "0xffffff");
so.addVariable("autostart", "true");
so.addVariable("shuffle", "false");
so.addVariable("playlist", "bottom");
so.addVariable("playlistsize", "23");
so.addVariable("repeat", "always");
so.addVariable("volume", "100");
so.addVariable("shoxeq", "false");
so.addVariable("autoscroll", "false");
so.addVariable("displayheight", "202");
so.write("swfcontent");

// ]]>
</script>

</body>
</html>

Please advice on how to call the yt.swf file for YouTube streaming video.

Cheers, Anders

 
The built-in access to the YouTube API is only available in the v4.x players.

The player accesses the YT.SWF proxy automatically; no code is needed, just put it with PLAYER.SWF.

If you load swfobject v2.1 from Google's CDN, it may solve your compatibility issue:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/swfobject/2.1/swfobject.js"></script>

Thanks a bunch leffTy! I do have swfobject v 2.1 available for other projects. Will have to switch over to v 4.x player.

Kind regards,
Anders

YouTube_HQ.php - is not down again... can you tell me what needs to be fixed?

@cody
youtube have blocked there get_video so you cant downlaod videos at the moment unless you use a get_file_content script for it
<?php$videoid=$_GET["videoid"];$url="http://www.youtube.com/api2_rest?method=youtube.videos.get_video_token&video_id=$videoid";$t = trim(strip_tags(@file_get_contents($url)));$url = "http://www.youtube.com/get_video.php?video_id=" . $videoid . "&t=" . $t . "fmt=18";header("Location: $url");?>
That should work

Hi Dear,

The youtube video download code is working in my local pc...The Url I am getting from my code is fine and working ok.But when I am uploading it to the server...I am getting the correct url value but it is not working or downloading the video...My server is fine....Curl is working fine....The url of the website is

www.dailyvideotv.com/sam2.php

If i run same code from my local pc I am getting same url structure but not working in my server....

has youtube blocked or did something my IP or what...

Can some one tell me why it is not working

Thanks in advance

I also tried with the code given above(Top of this forum) which is working fine in my local pc but not working in my server....

which is http://www.dailyvideotv.com/sam3.php

Please help me..

Thanks

Anyone having trouble getting the api to work after the outage today. I only just started working with the demo code yesterday. Everything was working great, but today after the Google outage the demo code appears not to work. I even went back to the a fresh downloaded code on a new website and even then still I get no errors but no results. source code I am using is at http://framework.zend.com/svn/framework/standard/trunk/demos/Zend/Gdata/YouTubeVideoBrowser/
I have also download the zend files as well. Just curious if anyone else is having a problem.

my site is located at
http://hot4videos.com/test/
if you want to take a look.

thanks,
Jeff

I'm getting the same problem as Sam. The script works great on the local machine but not on the server. The following is true:

YOUTUBE
1.) The video to be played is verified as working fine.

LOCAL MACHINE
1.) The php script works fine and causes the download to happen when the script is executed.

THE REMOTE SERVER
1.) The php script does not cause the download but a redirect if I keep the header() function enabled. It redirects to the location where the flv should be.

Please let me know if there's anything further that could help explain/correct this problem.

Thanks in advance,

Gene

The player code:

echo "<p id='mainvideo'>The player will show in this paragraph</p>
<script type='text/javascript' src='swfobject.js'></script>
<script type='text/javascript'>
var s1 = new SWFObject('player-viral.swf','player','".$mainvideo_width."','".$mainvideo_height."','".$mainvideo_flashversion."');
s1.addParam('allowfullscreen','true');
s1.addParam('allowscriptaccess','always');
s1.addVariable('file', encodeURIComponent('".$youtubeVideoPath."'));
s1.addVariable('type', 'video');
s1.write('mainvideo');
</script>";

Hi there I was just wondering if there was any code or anything i can use to download youtube videos with annotations?

I was using the first script to get the "t" params, but the source code of youtube has changed.

Now I'm using this one :

[...]
$response = curl_exec($session);
if (preg_match_all('/"t": "[^"]*/', $response , $matches))
{
$t = $matches[0][0];
$t = preg_split('/"t": "/', $t);
$t = $t[1];

[...]

(but this solution still working :
ask to :
http://www.youtube.com/api2_rest?method=youtube.videos.get_video_token&video_id=IDVIDEO

and get token in the XML reply:

<ut_response status="ok">
<t>vjVQa1PpcFM5zjFeYWDF41JPNCzZ0VhMk4aMDuW_hLk=</t>
</ut_response>

)

that's all.

I have 2 fixed source of youtube downloader....

check demo

demo 1----http://pcrole.chemicalservers.com/1/

demo2---- http://pcrole.chemicalservers.com/ i

If you want then contact me..
Email- mbt1370 @ gmail . com