Update: 27/03/2016
Add->Method: ConvertYoutubeToMp3
' ***********************************************************************
' Assembly : Youtube Downloader by fudmario
' Author : fudmario
' Created : 15-06-2015
'
' Last Modified By : fudmario
' Last Modified On : 03-27-2016
' ***********************************************************************
' <copyright file="YoutubeHelper.vb" company="DeveloperTeam">
' Copyright © 2016
' </copyright>
' <summary></summary>
' *************************************************************************
Option Explicit On
Option Strict On
Option Infer On
Imports System.Collections.Specialized
Imports System.Net
Imports System.Text
Imports System.Text.RegularExpressions
Imports System.Web.Script.Serialization
Imports System.Web.HttpUtility
Imports System.Threading
''' <summary>
''' Class YoutubeHelper. This class cannot be inherited.
''' </summary>
''' <autogeneratedoc />
Public NotInheritable Class YoutubeHelper
''' <summary>
''' Address to Get Video Information
''' </summary>
Private Const InfoUrl As String = "[youtube]http://www.youtube.com/get_video_in[/youtube]fo?video_id="
''' <summary>
''' Regular expression to get Image Url.
''' </summary>
''' <value>Image Url.</value>
Private Shared ReadOnly Property Imagen As Regex
Get
Return New Regex("<meta\sproperty=""og:image""\scontent=""(.*?default\.jpg)"">")
End Get
End Property
''' <summary>
''' Regular expression for the title of the video.
''' </summary>
''' <value>title of the video.</value>
Private Shared ReadOnly Property Title As Regex
Get
Return New Regex("<meta\sproperty=""og:title""\scontent=""(.*?)"">")
End Get
End Property
''' <summary>
''' Regular expression for video description.
''' </summary>
''' <value>video description.</value>
Private Shared ReadOnly Property Description As Regex
Get
Return New Regex("<meta\sproperty=""og:description""\scontent=""(.*?)"">")
End Get
End Property
''' <summary>
''' Regular expression for video length(in seconds).
''' </summary>
''' <value>video length(in seconds).</value>
Private Shared ReadOnly Property Duration As Regex
Get
Return New Regex("length_seconds"":""(.*?)"",")
End Get
End Property
''' <summary>
''' Regular expression to validate an address Youtube.
''' </summary>
''' <value>validate an address Youtube.</value>
''' <autogeneratedoc />
Private Shared ReadOnly Property YouTubeUrl() As Regex
Get
Return New Regex("(http|https):..(www\.)?youtube.com.watch\?v=")
End Get
End Property
''' <summary>
''' Gets the Id of YouTube Video.
''' </summary>
''' <param name="url">YouTube Video Address.</param>
''' <returns>System.String.</returns>
''' <autogeneratedoc />
Private Shared Function GetId(url As String) As String
Return ParseQueryString(New Uri(url).Query).Get("v")
End Function
''' <summary>
''' Get YouTube Video Information.
''' </summary>
''' <param name="url">YouTube Video Address.</param>
''' <param name="byPass">If set to <c>true</c> apply the bypass.</param>
''' <returns>YouTubeVideoInformation.</returns>
''' <exception cref="ArgumentNullException">The <paramref name="url" /> can not be empty</exception>
''' <exception cref="FormatException">Invalid <paramref name="url" /> format</exception>
Public Shared Function GetYouTubeVideoInfo(url As String, Optional ByVal byPass As Boolean = False) As YouTubeVideoInformation
If String.IsNullOrEmpty(url) Then Throw New ArgumentNullException(message:="The URL can not be empty", paramName:="URL")
If Not YouTubeUrl.IsMatch(url) Then Throw New FormatException(message:="Invalid URL format")
Dim videoInfoFull As New YouTubeVideoInformation
Dim infoVideo As New List(Of YoutubeVideoInfo)
Dim adInfoVideo As YoutubeVideoInfo
Dim mSource As String
Dim arrayParams As NameValueCollection
Using c As New WebClient
c.Encoding = Encoding.GetEncoding(1252)
mSource = c.DownloadString(url)
Select Case byPass
Case True
arrayParams = ParseQueryString(HtmlDecode(c.DownloadString(String.Format("{0}{1}&el=vevo", InfoUrl, GetId(url)))))
Case Else
arrayParams = ParseQueryString(HtmlDecode(c.DownloadString(String.Format("{0}{1}", InfoUrl, GetId(url)))))
End Select
End Using
Dim ytTitle As String = HtmlDecode(Title.Match(mSource).Groups(1).Value)
Dim ytDescription As String = HtmlDecode(Description.Match(mSource).Groups(1).Value)
Dim ytDuration As Double = CDbl(Duration.Match(mSource).Groups(1).Value)
Dim ytImg As String = Imagen.Match(mSource).Groups(1).Value
If Not arrayParams("status").Equals("ok") Then
videoInfoFull.ErrorCode = 1
videoInfoFull.ErrorMsg = HtmlDecode(arrayParams("reason"))
Else
Dim urLs As String() = arrayParams("url_encoded_fmt_stream_map").Split(","c)
Dim sb As New StringBuilder()
For i = 0 To urLs.Length - 1
adInfoVideo = New YoutubeVideoInfo()
sb.Clear()
Dim mUriDec As String = HtmlDecode(urLs(i))
Dim urlParams As NameValueCollection = ParseQueryString(mUriDec)
Dim vidFormat As String = HtmlDecode(urlParams("type"))
sb.Append(HtmlDecode(urlParams("Url")))
sb.AppendFormat("&signature={0}", HtmlDecode(urlParams("sig")))
sb.AppendFormat("&type={0}", vidFormat)
sb.AppendFormat("&title={0}", HtmlDecode(arrayParams("title")))
Dim format As String = vidFormat.Split(";"c)(0).Split("/"c)(1)
If format.Contains("x-flv") Then
format = "flv"
End If
adInfoVideo.Url = sb.ToString
adInfoVideo.Quality = urlParams("quality")
adInfoVideo.Format = format
infoVideo.Add(adInfoVideo)
Next
videoInfoFull.ErrorCode = 0
videoInfoFull.LinksDetails = infoVideo
videoInfoFull.Title = ytTitle
videoInfoFull.Duration = ytDuration
videoInfoFull.Description = ytDescription
videoInfoFull.ImgLink = ytImg
End If
Return videoInfoFull
End Function
''' <summary>
''' Converts the youtube to MP3.
''' </summary>
''' <param name="url">YouTube Video Address.</param>
''' <param name="interval">Request interval in seconds</param>
''' <returns>ResponseAudio.</returns>
''' <exception cref="FormatException"></exception>
''' <autogeneratedoc />
Public Shared Function ConvertYoutubeToMp3(ByVal url As String, Optional interval As Integer = 2) As ResponseAudio
If Not YouTubeUrl.IsMatch(url) Then Throw New FormatException(message:="Invalid URL format")
Dim result As String
Dim serializer As New JavaScriptSerializer()
Dim response As ResponseAudio
Dim query As String = String.Format("http://embed.yt-mp3.com/fetch?v={0}&referrer=", GetId(url))
Using wc As New WebClient()
result = wc.DownloadString(query)
response = serializer.Deserialize(Of ResponseAudio)(result)
While Not (response.status.Equals("ok") AndAlso response.ready = True)
Thread.Sleep(interval * 1000)
result = wc.DownloadString(query)
response = serializer.Deserialize(Of ResponseAudio)(result)
End While
Return response
End Using
End Function
''' <summary>
''' Basic Video Information.
''' </summary>
Public Class YoutubeVideoInfo
''' <summary>
''' Gets or sets the URL Direct Video.
''' </summary>
''' <value> URL Direct Video.</value>
Public Property Url() As String
''' <summary>
''' Gets or sets the Video Format.
''' </summary>
''' <value>Video Format.</value>
Public Property Format() As String
''' <summary>
''' Gets or sets the Video Quality.
''' </summary>
''' <value>Video Quality.</value>
Public Property Quality() As String
End Class
''' <summary>
''' Complete YouTube Video Information.
''' </summary>
Public Class YouTubeVideoInformation
''' <summary>
''' Gets or sets the list of Link's available and additional information.
''' </summary>
''' <value>list of Link's available and additional information.</value>
Public Property LinksDetails() As List(Of YoutubeVideoInfo)
''' <summary>
''' Gets or sets the Title Video.
''' </summary>
''' <value>Title Video.</value>
Public Property Title() As String
''' <summary>
''' Gets or sets the Image Url.
''' </summary>
''' <value> Image Url.</value>
Public Property ImgLink() As String
''' <summary>
''' Gets or sets the description of Video.
''' </summary>
''' <value>description of Video.</value>
Public Property Description() As String
''' <summary>
''' Gets or sets the duration of Video.
''' </summary>
''' <value>duration of Video.</value>
Public Property Duration() As Double
''' <summary>
''' Gets or sets the error code: 0 = No Error, 1 = Error.
''' </summary>
''' <value>error code.</value>
Public Property ErrorCode() As Integer
''' <summary>
''' Gets or sets the error message.
''' </summary>
''' <value>error message.</value>
Public Property ErrorMsg() As String
End Class
''' <summary>
''' Class ResponseAudio.
''' </summary>
Public Class ResponseAudio
''' <summary>
''' Gets or sets the status.
''' </summary>
''' <value>The status.</value>
''' <autogeneratedoc />
Public Property status As String
''' <summary>
''' Gets or sets the identifier.
''' </summary>
''' <value>The identifier.</value>
''' <autogeneratedoc />
Public Property id As String
''' <summary>
''' Gets or sets the title.
''' </summary>
''' <value>The title.</value>
''' <autogeneratedoc />
Public Property title As String
''' <summary>
''' Gets or sets the thumbnail.
''' </summary>
''' <value>The thumbnail.</value>
''' <autogeneratedoc />
Public Property thumbnail As String
''' <summary>
''' Gets or sets the length of time in seconds.
''' </summary>
''' <value>The length.</value>
Public Property length As Integer
''' <summary>
''' Gets or sets the name artist.
''' </summary>
''' <value>The artist.</value>
Public Property artist As String
''' <summary>
''' Gets or sets the categories.
''' </summary>
''' <value>The categories.</value>
''' <autogeneratedoc />
Public Property categories As String
''' <summary>
''' Gets or sets the bitrate(kbps).
''' </summary>
''' <value>The bitrate (kbps).</value>
''' <autogeneratedoc />
Public Property bitrate As String
''' <summary>
''' Gets or sets the average rating.
''' </summary>
''' <value>The average_rating.</value>
Public Property average_rating As String
''' <summary>
''' Gets or sets the view count.
''' </summary>
''' <value>The view_count.</value>
Public Property view_count As String
''' <summary>
''' Gets or sets the progress.
''' </summary>
''' <value>The progress.</value>
''' <autogeneratedoc />
Public Property progress As Integer
''' <summary>
''' Gets or sets a value indicating whether this <see cref="ResponseAudio" /> is ready.
''' </summary>
''' <value><c>true</c> if ready; otherwise, <c>false</c>.</value>
''' <autogeneratedoc />
Public Property ready As Boolean
''' <summary>
''' Gets or sets the expires.
''' </summary>
''' <value>The expires.</value>
''' <autogeneratedoc />
Public Property expires As Integer
''' <summary>
''' Gets or sets the URL of MP3.
''' </summary>
''' <value>The URL.</value>
Public Property url As String
''' <summary>
''' Gets or sets the video Information.
''' </summary>
''' <value>The video Information.</value>
Public Property video_url As String
End Class
End Class
How to Use:
Dim res As YoutubeHelper.ResponseAudio = YoutubeHelper.ConvertYoutubeToMp3("[youtube]https://www.youtube.com/watch?v=mWRsgZuwf_8[/youtube]")
Dim sb As New System.Text.StringBuilder()
sb.AppendLine(String.Format("ID..............: {0}", res.id))
sb.AppendLine(String.Format("Title...........: {0}", res.title))
sb.AppendLine(String.Format("Artist..........: {0}", res.artist))
sb.AppendLine(String.Format("Length(sec).....: {0}", res.length))
sb.AppendLine(String.Format("Bitrate(Kbps)...: {0}", res.bitrate))
sb.AppendLine(String.Format("Categories......: {0}", res.categories))
sb.AppendLine(String.Format("Average Rating..: {0}", res.average_rating))
sb.AppendLine(String.Format("Ready?..........: {0}", res.ready))
sb.AppendLine(String.Format("MP3 Url.........: {0}", res.url))
MessageBox.Show(sb.ToString())
Return:
ID..............: mWRsgZuwf_8
Title...........: Imagine Dragons - Demons (Official)
Artist..........: ImagineDragonsVEVO
Length(sec).....: 237
Bitrate(Kbps)...: 211
Categories......: Music
Average Rating..: 4.913
Ready?..........: True
MP3 Url.........: http://dl2.yt-mp3.com/download/imagine-dragons-demons-official.mp3?id=mWRsgZuwf_8&title=Imagine+Dragons+-+Demons+%28Official%29&t=1459086715&extra=a&h=7c6ea7b8be176f673a3b46d797b3a9a89d18c5ca