Youtube downloader

To follow online courses is somewhat a pain in the ass when it comes to platforms as Youtube to get the video lessons. Too many annoying adverts in some videos, and you have odds that the video is no longer there next time you want to watch it. The point is that I was on the need to download a bunch of videos from lists. From the youtube list panel you can select pieces of the list… this was another shortcoming that I had to solve, because you can not select the whole list. This post on StackOverflow was very informative. The most interesting parts are:
  • In YouTube, click on the subscriptions on the left hand pane. This will open up all your subscriptions in the center of the page. Scroll down and you’ll find a Export to RSS reader button which produces an xml file of all your subscriptions
  • You can get the feeds like this:
    https://www.youtube.com/feeds/videos.xml?channel_id=CHANNELID
    https://www.youtube.com/feeds/videos.xml?user=USERNAME
    https://www.youtube.com/feeds/videos.xml?playlist_id=YOUR_YOUTUBE_PLAYLIST_NUMBER
  • Video images are in the format http://i.ytimg.com/vi/[videoid]/default.jpg (square) andhttp://i.ytimg.com/vi/[videoid]/hqdefault.jpg (high quality)
  • Additionally you can request for API key for public access to your YouTube videos from your developer console and get YouTube Videos, Playlists in JSON format like this (more information from YouTube v3 docs):
    - Get Channels: 
      https://www.googleapis.com/youtube/v3/channels?part=snippet%2CcontentDetails&forUsername={YOUR_USER_NAME}&key={YOUR_API_KEY}
    - Get Playlists: 
      https://www.googleapis.com/youtube/v3/playlists?part=snippet%2CcontentDetails&channelId={YOUR_CHANNEL_ID}&key={YOUR_API_KEY}
    - Get Playlist Videos: 
      https://www.googleapis.com/youtube/v3/playlistItems?part=snippet%2CcontentDetails%2Cstatus&playlistId={YOUR_PLAYLIST_ID}&key={YOUR_API_KEY}
And after that I also wondered if I could download the subtitles/captions. To directly get the subtitles as SRT file, you can go here. But we can also carry a VBA download, but we first have to check if video has closed caption (not yet for the automatic generated captions), because if only the automatic ones are present, then it will not download anything:
Private Sub fYoutube_ClosedCaptions()
' Via Nitrxgen.net
    Dim strVideoId As String
    Dim strReturn As String
    Dim aTrack() As String
    Dim oTrack As Variant
    
    strVideoId = ...
    
    'If oIE Is Nothing Then fIE_Initialize
    'If Not fIE_OpenURL("http://www.nitrxgen.net/youtube_cc/" & strVideoId & "/") Then Exit Sub
    'If not fYoutube_ClosedCaption(strVideoId, "error") then
    '    MsgBox "Video [" & strVideoId & "] has no Closed Captions"
    'End If
    
    strReturn = fXMLHTTP_GetURL("http://video.google.com/timedtext?type=list&v=" & strVideoId)
    If strReturn Like "* name=""Automatic""*" Then
        MsgBox "Video [" & strVideoId & "] has no Closed Captions"
    Else
        aTrack() = VBA.Split(strReturn, "<track") 
        For Each oTrack In aTrack() 
If oTrack Like "* lang_code=""en""*" Then 
Stop 
' Get id 
' Download file... 
End If 
        Next oTrack 
    End If 
    Stop 
End Sub 
Private Function fYoutube_ClosedCaption(Optional ByVal strVideoId As String = vbNullString, _ 
                                        Optional ByVal strClass As String = "error") As Boolean 
' Check if video has Closed caption 
' class="error" --> Captions Not Found
' There are no closed captions associated with the YouTube URL you submitted. Please try another video.

    Dim lngLoop As Long
    Dim bComplete As Boolean
    Dim dtTic As Date
    Dim dtToc As Date

    ' get button item
    dtTic = VBA.Now()
    bComplete = True
    Do
        If strClass <> vbNullString Then
            If strVideoId = vbNullString Then Stop
            If VBA.Len(strVideoId) <> 11 Then Stop
            
            Set hCollection = hDoc.getElementsByClassName(strClass)
            If hCollection.Length > 0 Then
                bComplete = False
                Exit Do
            End If
        Else
            Exit Do
        End If
        
        dtToc = VBA.Now()
        If dtToc > dtTic + VBA.TimeSerial(0, 0, 5) Then
            'MsgBox "Too much to load... seems there is no such item"
            Exit Do
        End If
    Loop Until bComplete
    
    Set hElement = Nothing
    Set hCollection = Nothing
    
    fYoutube_ClosedCaption = bComplete
End Function

Private Function fXMLHTTP_GetURL(strURL As String) As String
    Dim oHTTP As Object
    
    Set oHTTP = CreateObject("MSXML2.ServerXMLHTTP")
    
    With oHTTP
        '.Open "POST", googleApiUrl, False ' use POST method with an API Key
        .Open "GET", strURL, False
        '.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
        .setRequestHeader "Content-Type", "text/xml" ' only to retrieve text
        '.setRequestHeader "Content-type", "application/text" ' this may also work for text
        
        .send ("")
        
        'wait
        'Do While .Status = 200 And .readyState = 4
        'Loop
        
        fXMLHTTP_GetURL = .responseText 
    End With 
End Function
To download the captions, use this script:
Public Function fYouTube_Subtitles_Download(ByVal strVideoId As String) As Boolean
    Dim oHTTP As Object
    Dim oStream As Object
    
    Set oHTTP = CreateObject("MSXML2.XMLHTTP")
    With oHTTP
        .Open "GET", "http://video.google.com/timedtext?lang=en&v=" & strVideoId, False
        .send
        
        Set oStream = CreateObject("ADODB.Stream")
        With oStream
            .Open
            .Type = 1
            .write oHTTP.responseBody
            .SaveToFile VBA.Environ$("UserProfile") & "\Downloads\" & strVideoId & ".xml", 2
            .Close
        End With
    End With
End Function
This is a JS library on GitHub that can handle also the automatic generated captions. If you have a youtube video and want to download the automatic generated subtitles, follow this step-by-step guide. If you want to do it yourself, follow this other one. Go and edit the captions.

Leave a Reply

Your email address will not be published. Required fields are marked *