-
How to get youtube direct flv url from giving youtube url
Hi all. Could any one show me how i can get youtube direct flv url using youtube url ? I know there programs that do that but i need to do it for a project that i work with. I found a php example but don't know how to convert it to vb6 so i be happy if some one help me convert it to vb6.Thanks
PHP Code:
function get_link($url){
if ($html = file_get_contents($url)){
if(preg_match_all("/\b(?:video_id)\b:.*/", $html, $matches)){
$ref = $matches[0][0];
//echo $ref.'<br>';
preg_match_all("/\'[^\'\\\\\r\n]*(?:\\\\.[^\'\\\\\r\n]*)*\'/", $ref, $match);
//video_id:'WAvj0iY1Zig',l:'142',t:'OEgsToPDskLspgdtd49MOElkAv_WEJmW',sk:'2ZgBZFfneV2DzW3Zb8g2AAC1
//print_r ($match);
$id1 = str_replace("'", "", $match[0][0]);
$id2 = str_replace("'", "", $match[0][2]);
$id3 = str_replace("'", "", $match[0][3]);
$id = $id1.'&t='.$id2.'&sk='.$id3;
//echo $id;
$url = 'http://youtube.com/get_video.php?video_id='.$id;
$url = get_headers($url);
//$url = print_r($url);
$url = $url[9];
//echo $id;
$url = substr($url,10);
$url = $url.'.flv';
//echo $url;
return $url;
}
return false;
}
}
-
Re: How to get youtube direct flv url from giving youtube url
I don't know how to do it in VB, but this is how it works.
For example, you have this url to a Youtube video: http://www.youtube.com/watch?v=TJhP6pyVu20
Change the url to this: http://www.youtube.com/v/TJhP6pyVu20
When you paste the above (second) url into your browser's address bar and press enter you'll get this url in your browser's address bar: http://www.youtube.com/swf/cps.swf?v...rel=1&border=0
Extract the "video_id=" parameter and the "t=" parameter from that url.
Then add them to the following url: http://www.youtube.com/get_video.php?
And you'll get the the url to the file:
http://www.youtube.com/get_video.php...-yUTlV1RHWt8ua
Save the file with a ".flv" extension.
-
Re: How to get youtube direct flv url from giving youtube url
chris thanks for you reply. But my intention is not to download the flv file . I want to get exact url of flv file using vb6 so i be able to use it remotely!!
For example for the youtube url that you provided the direct link will be :
not:
The php code is doing exactly what i want . But since i want to convert like 60 urls automatically php is not that fast and gives me maximum execution time reached error!!
If i be able to get header for one before last url of yours then that would be the direct url that i want.
Hope i get some help in making this converting process automatic.
-
Re: How to get youtube direct flv url from giving youtube url
I can't help you with that, sorry.
A few months ago I tried to figure that out as well, but I could only find out how to do it the way I explained above.
If somebody knows how to do it, I'd like to know that as well.
-
Re: How to get youtube direct flv url from giving youtube url
tony 007, i find cris001 post is usufull. i did not get whats the problem with cris001's code. i advice you to put that php code in to VBF php forum, there you probably get translated :)
-
Re: How to get youtube direct flv url from giving youtube url
The disadvantage of my way is that the link only works for a short period of time. The "video_id=" parameter is always the same, but the "t=" parameter changes frequently.
If you click on the last link in my first post you'll see that it gives you a 410 message (page gone) now.
My solution works fine if you want to download the video right away, but you can't post the final link on an other webpage, because the link expires.
-
Re: How to get youtube direct flv url from giving youtube url
Got it, but the code is very messy. Somebody needs to clean it up and perhaps change it a bit. I had to connect to the Youtube website twice or else I got an error that Winsock wasn't in the correct state to send data a second time. I used a socket class, because Winsock froze VB every time I wanted to close the connection.
Also, this doesn't work for every clip. Some clips are apparently taken from google video and the returned url is something like this:
Code:
http://cache.googlevideo.com/get_video?video_id=vWuwXDSy5M8&origin=sjl-v145.sjl.youtube.com
Of course you can check if the url is from google video and then use different extraction code, but you'll have to do that yourself. It's late and I'm off to bed.
-
Re: How to get youtube direct flv url from giving youtube url
Thanks chris . I tried your code and i keep getting this error:
Quote:
compile error:
User-defined type not defined
pointing at :
Quote:
Private WithEvents Winsock As CSocketPlus
-
Re: How to get youtube direct flv url from giving youtube url
I have no idea, it works fine for me.
Try it without "WithEvents"
-
2 Attachment(s)
Re: How to get youtube direct flv url from giving youtube url
Quote:
Originally Posted by Chris001
I have no idea, it works fine for me.
Try it without "WithEvents"
When i loaded it for the first time i got the error shown in the pic. i wonder why you had to connect to youtube first . couldn't your connect to giving url directly using winsock?
I even tried it without "withEvents" but got same error!!
-
Re: How to get youtube direct flv url from giving youtube url
I'm so sorry, it's my fault... I added a class and module from another project, but I forgot to include them with the upload :blush:
The reason you have to connect twice to the youtube website, is because after you've sent the first link to get the header, the server closes the connection. Then you'll have to connect to the youtube website again to send the second link in order to get the direct video link from the header.
Here's the project with the class and module..
-
Re: How to get youtube direct flv url from giving youtube url
chris thanks now it is working. since i try to process a list of youtube urls . I made listbox and populated it will all my youtube urls and i added a button so it takes each url and place it in textbox and process it.
But unfortunately when i click that button the listbox urls gets placed in text box so fast without allowing them to get processed!!
Could you look at my code and let me how i can solve this problem?Thanks
2 Code:
Private Sub Command2_Click()
Dim i As Long
For i = 0 To List2.ListCount - 1
List2.Selected(i) = True
txtOriginalURL = List2.List(i)
DoEvents
'cmdSend_Click
Next
End Sub
Private Sub txtOriginalURL_Change()
GetYoutubeUrl txtOriginalURL.Text
End Sub
-
Re: How to get youtube direct flv url from giving youtube url
You can do something like this, but I'm not sure if it works.
Code:
Private iCount As Integer '// Top of the form
Code:
Private Sub Command1_Click()
List2.Selected(iCount) = True
GetYoutubeUrl List2.List(iCount)
End Sub
Put this at the end of the GetYoutubeUrl sub and make txtFinalURL mutiline in the properties.
Code:
'// Put the direct link in the textbox
txtFinalURL.Text = txtFinalURL.Text & ReturnURL & vbCrLf
If iCount = (List 2.ListCount - 1) Then
MsgBox "Done !", vbInformation
Exit Sub
End If
iCount = iCount + 1
List2.Selected(iCount) = True
GetYoutubeUrl List2.List(iCount)
-
Re: How to get youtube direct flv url from giving youtube url
Thanks . i tried it but i get this error:
Quote:
compile error:
Exit sub not allowd in function or property
pointing at Exit sub of this part of code:
2 Code:
If iCount = (List2.ListCount - 1) Then
MsgBox "Done !", vbInformation
Exit Sub
End If
I commented out the Exit Sub and i saw highlight in listbox is moving downward and i keep getting the following in txtFinalURL textbox! :
Quote:
wait...404 Not found
404 Not found
404 Not found
...
I think there should be some kind of wait untill first url get executed before jumping the others !!
-
Re: How to get youtube direct flv url from giving youtube url
The "Exit Sub" is correct.
Change
Code:
Private Function GetYoutubeUrl(strURL As String)
to
Code:
Private Sub GetYoutubeUrl(strURL As String)
because it's not returning anything.
Also, keep in mind I wrote that project last night at 4am only to show you how to get the direct video link. It wasn't written to be used in an automated process with a Listbox. The code is messy, it barely has any error handling and it can probably be improved quite a lot.
-
Re: How to get youtube direct flv url from giving youtube url
chris thanks . This time no error but the highlight in listbox is moving so fast not giving enough time for the program to process the urls!!
In some of my projects i used this line of code and it was working well in slowing down listbox but in this project it keeps giving me eror:
vb Code:
txtOriginalURL = List2.List(i)
Text1 = Inet1.OpenURL("List2.List(i)", icByteData)
complaining about icbyteData and giving me this error:
Quote:
compile error
icbyteData not defined
-
Re: How to get youtube direct flv url from giving youtube url
I have no idea what you're trying to do, but Inet is not going to work.
The reason why Inet slows it down is because the code is halted until "Inet1.OpenURL" is finished retrieving the HTML source code of a webpage. You're getting a compile error because "icByteData" doesn't exist. It should be "icByteArray" and "icByteArray" should normally be used with binary files. The data should be stored in a byte array, not in a Textbox (assuming Text1 is a Textbox). To show HTML source code in a Textbox use "icString".
But like I said Inet is not going to work. You need the header and not the HTML source code of a webpage and as far as I know that can't be done with Inet. Inet automatically handles redirections and "Inet1.URL" never gets updated to the page you get redirected to.
-
Re: How to get youtube direct flv url from giving youtube url
Here is what i want to achive. I pull youtube urls from mysql and populate listbox or listview. Then convert these urls to direct flv files autmatically and then create an xml playlist for so i be able to use it with my flash video player.
-
Re: How to get youtube direct flv url from giving youtube url
I found that if you use a web browser control, then you can merely get an url.
http://www.youtube.com/watch?v=nvSNbmxdjDA
And then just put
www.youtube.com/v/watch?v=nvSNbmxdjDA
It still plays if the video has embed disabled.
-
Re: How to get youtube direct flv url from giving youtube url
Quote:
Originally Posted by Vanasha
well i want to create playlist not just play one video. That is why i look for direct flv urls!!
-
Re: How to get youtube direct flv url from giving youtube url
I've made a YouTube video playlist. It saves the video id's of the wanted videos, then puts it into a file. Reads it again, and put's them into a listbox, so whenever you click the name of the video you want to watch, it plays it using the ID and the method I showed you.
-
Re: How to get youtube direct flv url from giving youtube url
Quote:
Originally Posted by Vanasha
I've made a YouTube video playlist. It saves the video id's of the wanted videos, then puts it into a file. Reads it again, and put's them into a listbox, so whenever you click the name of the video you want to watch, it plays it using the ID and the method I showed you.
It would be nice if you share it. Does it play more the one video(playlist) one after another without clicking ?
-
2 Attachment(s)
Re: How to get youtube direct flv url from giving youtube url
I've attached the main form of the project, please keep in mind that I coded it, do not distribruite or claim as your own. :D
I'm sure you could code the playing after one and other.
-
Re: How to get youtube direct flv url from giving youtube url
Quote:
Originally Posted by Vanasha
I've attached the main form of the project, please keep in mind that I coded it, do not distribruite or claim as your own. :D
I'm sure you could code the playing after one and other.
Thanks for sharing it. I had to comment out your help form since it didn't exist. I added 2 videos but it never playes the second one when the first one finishes!! I wish it could play the added songs one after anotehr with me having to clicking them. Also the first video doesn't start automatically!!
-
Re: How to get youtube direct flv url from giving youtube url
Then code it in, Tony. Study the code and learn how it works. You can't always expect everbody to give you code that works 100% the way you want it. Sometimes it needs to be modified a bit to suit your needs. Vanasha gave you most of the code, playing it automatically and after one and other is not a real big step.
I spent 1 hour in the middle of the night trying to figure out how to get the direct youtube video url, created a sample project to show you how it can be done and gave you code to loop through a Listbox to automatically extract the urls, which works fine for me, but you simply dismiss it because it does't work 100% out of the box for you.