如何将 youtube 字幕放入列表中?

Posted

技术标签:

【中文标题】如何将 youtube 字幕放入列表中?【英文标题】:How to put youtube captions in a list? 【发布时间】:2021-12-26 17:51:25 【问题描述】:

“文本”字符串包含 youtube 视频的字幕。如何将这些字幕放在每个视频的单独列表中。

videos = get_channel_videos(channel_id)
video_ids = []  # list of all video_id of channel
 
for video in videos:
    video_ids.append(video['snippet']['resourceId']['videoId'])
 
for video_id in video_ids:
    try:
        responses = YouTubeTranscriptApi.get_transcript(
            video_id, languages=['en'])
        print('\n'+"Video: "+"https://www.youtube.com/watch?v="+str(video_id)+'\n'+'\n'+"Captions:")
        for response in responses:
            text = response['text']
            print(text)
    except Exception as e:
       print(e)

【问题讨论】:

【参考方案1】:

您可能需要一个列表字典,而不仅仅是很多列表:

videos = get_channel_videos(channel_id)
video_ids = []  # list of all video_id of channel
 
for video in videos:
    video_ids.append(video['snippet']['resourceId']['videoId'])

# make your dictionary here
captions = 
 
for video_id in video_ids:
    if video_id not in captions:
        # initialize empty list for each id
        captions[video_id] = [] 

    try:
        responses = YouTubeTranscriptApi.get_transcript(
            video_id, languages=['en'])
        print('\n'+"Video: "+"https://www.youtube.com/watch?v="+str(video_id)+'\n'+'\n'+"Captions:")
        for response in responses:
            text = response['text']

            # append to that list here
            captions[video_id].append(text)

            print(text)
    except Exception as e:
       print(e)

对于多个字段

你需要一本字典:

for video_id in video_ids:
    if video_id not in captions:
        # initialize empty list for each id
        captions[video_id] = 'Transcript': [], 'Title': None
    
    try:
        responses = YouTubeTranscriptApi.get_transcript(
            video_id, languages=['en'])
        print('\n'+"Video: "+"https://www.youtube.com/watch?v="+str(video_id)+'\n'+'\n'+"Captions:")
        for response in responses:
            text = response['text']

            # append to that list here
            captions[video_id]['Transcript'].append(text)

            print(text)

        captions[video_id]['title'] = response['title']
    except Exception as e:
       print(e)

【讨论】:

您知道如何将 youtube 视频的标题添加到字典中吗?所以输出的例子是 'Key': ['transcript'],[ 'title'] @SlimDowning 查看最新编辑 执行代码时,值'None'在每一行的标题字典中。同样在 sn-p 'captions[video_id]['title'] = response['title']' Pycharm 给出一个错误,即名称'response'可以是未定义的 正确,如果responses 为空,则不会定义response 我希望在响应中填写当前显示为“无”的标题

以上是关于如何将 youtube 字幕放入列表中?的主要内容,如果未能解决你的问题,请参考以下文章

获取只有字幕 Youtube API 的视频列表

获取 YouTube 字幕

如何使用youtube-dl刻录/硬编码已下载的youtube视频的字幕

如何在 YouTube api v3 中获取 YouTube 视频的 cc 字幕

如何通过 Android 应用程序的链接重定向到 youtube 应用程序?

Youtube API - 如何使用自定义控件打开/关闭字幕,更改语言?