Youtube + Selenium ChromeDriver(Python)-如何知道视频何时结束? [英] Youtube + Selenium ChromeDriver (Python) - How to know when a video ends?

查看:554
本文介绍了Youtube + Selenium ChromeDriver(Python)-如何知道视频何时结束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于了解其中正在播放视频的Youtube页面内容的简短问题:

总结

作为基于浏览器的GUI的一部分,我使用Selenium在Youtube上播放视频.

我需要知道视频何时结束播放,以便GUI进行其他操作(例如,将本地HTML馈入浏览器).

代码段和问题

import os, time
from selenium import webdriver

# [...]
chromedriver = 'path_to_chromedriver_binary'  # substitute as appropriate
driver = webdriver.Chrome(chromedriver)
youtube_link = 'https://www.youtube.com/watch?v=somevideo'
driver.get(youtube_link)

这时,我可以time.wait()表示视频的长度.

但是,我想知道是否可以通过Selenium驱动程序查询YouTube页面并在while循环中估算播放的剩余时间(我不熟悉如何从youtube页面提取此信息)

谢谢!

[使用解决方案进行编辑]

硒解决方案

感谢Stanjer,并感谢此答案

(不要忘记在脚本的开头添加"return")

硒替代解决方案

不太优雅,但值得指出: driver.text以此字符串格式"1:00/2:00"返回代表视频计时器的字符串.因此,您可以按照以下方式检查视频是否已通过播放:

video_is_playing = True
while video_is_playing:
    time.sleep(1)
    video_is_playing = not(driver.text[:4] == driver.text[-4:])

[edit]根据Jose的评论,也可以通过以下方式访问此信息:

driver.find_element_by_class_name("ytp-time-current").text

并发症和下一个问题

我需要以自动播放的最大化格式打开视频.

这意味着我调用了以下网址:

youtube.com/v/<video_code>?rel=0&autoplay=1

但是,这将返回一个非常短的html,其中仅包含嵌入代码,如此处的示例所示:

<HTML><HEAD></HEAD>
    <BODY leftMargin=0 scroll=no topMargin=0>
        <EMBED height="100%" 
        type=application/x-shockwave-flash 
        width="100%" 
        src=https://www.youtube.com/v/Fsc-oT9PsSQ?rel=0&amp;autoplay=1
        fullscreen="yes">
    </BODY>
</HTML> 

所以我在这里没有movie_player元素.

方法1-我可以从application/x-shockwave-flash中提取计时器吗?

方法2-如果我在经典的YouTube页面上启动youtube视频,该如何告诉movie_player使其自身最大化?

(注意:此答案此答案可能包含解决方法2的信息,如果我知道与Selenium一起使用)

解决方案

您可以在youtube视频页面的上下文中执行javascript API:

youtubePlayer = document.getElementById("movie_player");
youtubePlayer.getPlayerState();

因此,根据 https://developers.google.com/youtube/js_api_reference?csw = 1

state == 0是视频结束时

您可以在循环中添加一个执行程序,每N秒检查一次状态.

[Edit: solved, see mid-way in text of question]

Quick question on understanding the contents of a Youtube page with a video playing in it:

In summary

I use Selenium to play videos on Youtube, as part of a browser-based GUI.

I need to know when the video has finished playing, for the GUI to do something else (e.g. feed local HTML into the browser).

Code snippet and question

import os, time
from selenium import webdriver

# [...]
chromedriver = 'path_to_chromedriver_binary'  # substitute as appropriate
driver = webdriver.Chrome(chromedriver)
youtube_link = 'https://www.youtube.com/watch?v=somevideo'
driver.get(youtube_link)

At this point, I could time.wait() for the length of the video.

However, I wonder if I could query the YouTube page via the Selenium driver and gauge the time remaining for the play-through in a while loop (I am not familiar with how to extract this info from a youtube page)

Thanks!

[Edits with solution]

Selenium Solution

Thanks to Stanjer, and to this answer and this other answer, you can get the movie_player status through this method:

player_status = driver.execute_script("return document.getElementById('movie_player').getPlayerState()")

(don't forget to add "return" at the beginning of the script)

Selenium Alternative Solution

Less elegant but worth pointing out: driver.text returns a string representing the video timer in this string format '1:00 / 2:00'. Therefore you can check if the video has played through by something along these lines:

video_is_playing = True
while video_is_playing:
    time.sleep(1)
    video_is_playing = not(driver.text[:4] == driver.text[-4:])

[edit] As per Jose's comment, this info can also be accessed by:

driver.find_element_by_class_name("ytp-time-current").text

Complication and next question

I need to open up the video in maximised format, and with autoplay.

This means I call the following url:

youtube.com/v/<video_code>?rel=0&autoplay=1

However this returns a very short html which only contains an embed code, as per example here:

<HTML><HEAD></HEAD>
    <BODY leftMargin=0 scroll=no topMargin=0>
        <EMBED height="100%" 
        type=application/x-shockwave-flash 
        width="100%" 
        src=https://www.youtube.com/v/Fsc-oT9PsSQ?rel=0&amp;autoplay=1
        fullscreen="yes">
    </BODY>
</HTML> 

So I have no movie_player element here.

Approach 1 - can I extract the timer from the application/x-shockwave-flash?

Approach 2 - If I launch the youtube video in a classic Youtube page, how can I tell the movie_player to maximise itself?

(note: this answer and this answer probably contain info to solve approach 2, will post if I get this to work with Selenium)

解决方案

You can execute javascript API in context of youtube video page:

youtubePlayer = document.getElementById("movie_player");
youtubePlayer.getPlayerState();

So according to https://developers.google.com/youtube/js_api_reference?csw=1

state == 0 is when a video has ended

You can add an executor in a loop checking the state every N seconds.

这篇关于Youtube + Selenium ChromeDriver(Python)-如何知道视频何时结束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆