【发布时间】:2023-01-09 21:00:30
【问题描述】:
我可以通过判断采集到的数据中是否有liveStreamingDetails来判断这个视频是否直播。
if 'liveStreamingDetails' in video_data:
video_type = 'live'
但这样做会将首映视频视为直播。
如何避免这种情况?
【问题讨论】:
我可以通过判断采集到的数据中是否有liveStreamingDetails来判断这个视频是否直播。
if 'liveStreamingDetails' in video_data:
video_type = 'live'
但这样做会将首映视频视为直播。
如何避免这种情况?
【问题讨论】:
我设法找到了一种方法来区分视频是直播还是首播。
我参考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'
【讨论】:
据我测试,正在播放的首映视频缺少the liveStreamingDetails:concurrentViewers entry。因此,您可以使用例如 Videos: list 检查此条目是否是响应的一部分,以了解视频是正在播放的直播还是正在播放的首映式。
【讨论】:
status.uploadStatus来区分已结束的直播和已结束的首映对于直播,它应该是uploaded和processed对于首映。分别使用 TCBbXgBIC1I 和 2aamcJeIvEg 进行测试。
qzRRvb8v8mE和-r2OqPvJDwM没有这样的区别......我认为使用源代码解析是最好的做法,就像我在open-source YouTube operational API中所做的那样,isPremiumOnly用于@987654330 @.如果您可以在我的 API 中分享您的解决方案,我们将不胜感激。
你可以向链接发出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!')
【讨论】: