我正在进行一个视频分析项目,该项目需要从youtube下载视频并将其上传到Google云存储中。我无法找到直接将它们上传到gcs的方法,因此,我尝试在本地计算机上下载它们,然后将其上传到gcs。

我浏览了多篇有关stackoverflow的文章,关于这些内容,并借助那些我能够提供以下脚本的文章。

我浏览了关于stackoverflow的多篇文章,涉及相同的内容,例如
python: get all youtube video urls of a channel

Download YouTube video using Python to a certain directory

在那些人的帮助下,我提出了以下脚本。

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')

它仅适用于前两个视频网址,然后由于以下错误而失败
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

我希望有人可以帮助我了解这里出了什么问题,是否可以帮助我解决此问题。我非常需要此功能,并且一段时间以来一直无法解决该问题。

提前非常感谢!

附言如果可能,是否有办法直接将它们上传到gcs。

最佳答案

似乎您可能会与YouTube的服务条款发生冲突,因此建议您查看此文档,并注意第5部分B字母。[1]

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

07-27 20:00