【问题标题】:How to tell if a YouTube video is live for use YouTube Data API如何判断 YouTube 视频是否在使用 YouTube Data API
【发布时间】:2023-01-09 21:00:30
【问题描述】:

我可以通过判断采集到的数据中是否有liveStreamingDetails来判断这个视频是否直播。

if 'liveStreamingDetails' in video_data:
    video_type = 'live'

但这样做会将首映视频视为直播。

如何避免这种情况?

【问题讨论】:

    标签: python youtube-data-api


    【解决方案1】:

    我设法找到了一种方法来区分视频是直播还是首播。

    我参考Use beautifulsoup to get a youtube video‘s information的回答。

    from requests_html import HTMLSession
    from bs4 import BeautifulSoup
    
    video_url = "YouTube Url"
    session = HTMLSession()
    response = session.get(video_url)
    response.html.render(sleep=3)
    soup = BeautifulSoup(response.html.html, "lxml")
    
    if soup.select_one('#info-strings').text[:8] == 'Streamed':
        video_type = 'live'
    else:
        video_type = 'video'
    

    【讨论】:

      【解决方案2】:

      据我测试,正在播放的首映视频缺少the liveStreamingDetails:concurrentViewers entry。因此,您可以使用例如 Videos: list 检查此条目是否是响应的一部分,以了解视频是正在播放的直播还是正在播放的首映式。

      【讨论】:

      • 非常感谢您的回复。但 liveStreamingDetails:concurrentViewers 也从已结束的直播流中丢失。我识别的视频是那些已经结束直播或首映的视频
      • 根据我的调查,YouTube 数据 API 似乎无法执行此操作。所以我在想能不能直接找到网页的源代码。请参阅 <yt-formatted-string class="style-scope ytd-video-primary-info-renderer"> 后跟“直播”或“首播”。
      • 似乎您可以通过使用googleapis.com/youtube/v3/…获得的status.uploadStatus来区分已结束的直播和已结束的首映对于直播,它应该是uploaded和processed对于首映。分别使用 TCBbXgBIC1I 和 2aamcJeIvEg 进行测试。
      • 根据你给的例子,确实有区别,但我用“qzRRvb8v8mE”和“-r2OqPvJDwM”来测试,都给出了“处理过”的结果
      • 你是对的qzRRvb8v8mE和-r2OqPvJDwM没有这样的区别......我认为使用源代码解析是最好的做法,就像我在open-source YouTube operational API中所做的那样,isPremiumOnly用于@987654330 @.如果您可以在我的 API 中分享您的解决方案,我们将不胜感激。
      【解决方案3】:

      你可以向链接发出get请求,检查视频的源代码中是否包含isLowLatencyLiveStream,如果包含它是实时视频,否则它只是一个视频。这个词 isLowLatencyLiveStream 只包含实时流视频。

      import requests
      def checkLink(link):
          headers = {
          "User-Agent": 'Mozilla/5.0 (iPad; U; CPU OS 3_2_1 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Mobile/7B405',
            "Accept-Language": "en-US,en;q=0.5",
            'Content-Type': 'text/html; charset=utf-8',
             'Content-Encoding': 'gzip',
          }
          try:
              response = requests.get(link, headers=headers, verify=False, cookies={'CONSENT': 'YES+42'})
              if "isLowLatencyLiveStream" in response.text:
                  return True
              else:
                  return False
          except:
              return False
      

      但我认为每个请求都需要使用代理,否则 Youtube 会阻止你

      或者使用 pafy 的另一种方式:

      import pafy
      import re
      
      # Get the video URL from the user
      url = input("Enter the YouTube video URL: ")
      
      # Check is it a link even
      if re.match('https?://(?:www.)?youtube.com/watch?v=([w-]{11})', url):
          # Get information about video using pafy
          video = pafy.new(url)
          # Looking a duration
          if video.duration == '00:00:00':
              print('This is live video')
          else:
              print('This is just vide')
      else:
          print('This is not link to the video!')
      

      【讨论】:

        猜你喜欢
        • 2015-08-10
        • 2014-04-14
        • 2012-11-25
        • 2015-10-08
        • 2017-04-21
        • 2021-01-06
        • 2021-07-09
        • 2017-06-12
        • 2020-07-13
        相关资源
        最近更新 更多