1

I am working on a video analysis project which requires to download videos from youtube and upload them on google cloud storage. I could not figure out a way to directly upload them to gcs thus, I tried to download them on local machine and then upload them to gcs.

I went through multiple articles on stackoverflow regarding the same and with the help of those I was able to come up with the following script.

I went through multiple articles on stackoverflow regarding the same such as python: get all youtube video urls of a channel and

Download YouTube video using Python to a certain directory

and with the help of those I was able to come up with the following script.

import urllib.request
import json
from pytube import YouTube
import pickle

def get_all_video_in_channel(channel_id):
    api_key = 'AIzaSyCK9eQlD1ptx0SKMsmL0srmL2ua9_EuwSs'

    base_video_url = 'https://www.youtube.com/watch?v='
    base_search_url = 'https://www.googleapis.com/youtube/v3/search?'

    first_url = base_search_url+'key={}&channelId={}&part=snippet,id&order=date&maxResults=25'.format(api_key, channel_id)

    video_links = []
    url = first_url
    while True:
        inp = urllib.request.urlopen(url)
        resp = json.load(inp)

        for i in resp['items']:
            if i['id']['kind'] == "youtube#video":
                video_links.append(base_video_url + i['id']['videoId'])

        try:
            next_page_token = resp['nextPageToken']
            url = first_url + '&pageToken={}'.format(next_page_token)
        except:
            break
    return video_links


#Load the file containing all the youtube video url
load_url = get_all_video_in_channel(channel_id)

#Access all the youtube url in the list and store them on local machine. Need to figure out if there is a way to directly upload them to gcs
for i in range(0,len(load_url)):
    YouTube('load_url[i]').streams.first().download('C:/Users/Tushar/Documents/Serato_Video_Intelligence/youtube_videos')

It works only for the first two video urls and then fails with the below error

Traceback (most recent call last):
 File "<stdin>", line 2, in <module>
 File "C:\Python37\lib\site-packages\pytube\streams.py", line 217, in download
   bytes_remaining = self.filesize
 File "C:\Python37\lib\site-packages\pytube\streams.py", line 164, in filesize
   headers = request.get(self.url, headers=True)
 File "C:\Python37\lib\site-packages\pytube\request.py", line 21, in get
   response = urlopen(url)
 File "C:\Python37\lib\urllib\request.py", line 222, in urlopen
   return opener.open(url, data, timeout)
 File "C:\Python37\lib\urllib\request.py", line 531, in open
   response = meth(req, response)
 File "C:\Python37\lib\urllib\request.py", line 641, in http_response
   'http', request, response, code, msg, hdrs)
 File "C:\Python37\lib\urllib\request.py", line 569, in error
   return self._call_chain(*args)
 File "C:\Python37\lib\urllib\request.py", line 503, in _call_chain
   result = func(*args)
 File "C:\Python37\lib\urllib\request.py", line 649, in http_error_default
   raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden

I was hoping if someone can please help me understand what is going wrong here and if could help me to resolve this issue. I desperately need this and have been unable to resolve the issue for some time.

Thanks a lot in advance !!

P.S. If possible, is there a way to directly upload them to gcs.

Tushar Mehta
  • 550
  • 1
  • 9
  • 21

1 Answers1

0

Seems like you could fall in a conflict with YouTube's the terms of service, I suggest to you check this document and put attention on the section number 5 letter B. [1]

[1]https://www.youtube.com/static?gl=US&template=terms

Harif Velarde
  • 733
  • 5
  • 10