This is how we can extract the Youtube Title:
First we need to import the following namespaces:
Code:
Imports System.Net
Imports System.IO
Then add this function to the application. This function gets the source code of any webpage.
Code:
Function GetPage(ByVal pageUrl As String) As String
Dim s As String = ""
Try
Dim request As HttpWebRequest = WebRequest.Create(pageUrl)
Dim response As HttpWebResponse = request.GetResponse()
Using reader As StreamReader = New StreamReader(response.GetResponseStream())
s = reader.ReadToEnd()
End Using
Catch ex As Exception
Throw New Exception("Failed to get source code !!!")
End Try
Return s
Now we need to parse the source code to extract the data we need:
First we extract the Video Title using the following function:
'This function is optional and is not needed if you want to just download the video.
Code:
Function GetTitle(ByVal URL As String)
Dim result As String = "Couldn't extract title !!"
Try
If Not URL = vbNullString Then
Dim c As String = GetPage(URL)
Dim a1 As Integer = c.IndexOf("<title>") + 8
Dim a2 As Integer = c.IndexOf("</title>", a1)
Dim title As String = Mid(c, a1, a2 - a1).Replace("YouTube", "")
title = title.Trim
title = title.Remove(0, 2)
result = title
End If
Catch ex As Exception
End Try
Return result
End Function
Then we use the following function to extract the video's download link:
Code:
Function getDownloadLink(ByVal URL As String)
Dim result As String = "Couldn't extract link !!"
Try
If Not URL = vbNullString Then
Dim c As String = GetPage(URL)
Dim a1 As Integer = c.IndexOf("'VIDEO_ID': '") + 14
Dim a2 As Integer = c.IndexOf("'", a1) + 1
Dim id As String = Mid(c, a1, a2 - a1)
Dim a3 As Integer = c.IndexOf("""t"": """) + 7
Dim a4 As Integer = c.IndexOf("""", a3) + 1
Dim t As String = Mid(c, a3, a4 - a3)
result = id & "&t=" & t
End If
Catch ex As Exception
End Try
Return result
End Function
This is all the code.
Example:
Code:
System.Diagnostics.Process.Start("https://www.youtube.com/get_video?video_id=" & getDownloadLink("https://www.youtube.com/watch?v=u_0q0kL03Lg"))
If you want to download the video in high quality, insert "&fmt=18" or in HD then insert "&fmt=22" at the end of the Video URL.
Hope this helps !!
Regards