Youtube + Selenium (Python) - How do I know when a video ends?

[Edit: resolved, see middle in question text]

A quick question about understanding the contents of a Youtube page with playing videos in it:

Finally

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

I need to know when the video finished playing, as the GUI does something else (for example, submit local HTML in a 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=BHjg6cTxmrQ' driver.get(youtube_link) 

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

However, I am wondering if I can request the youtube page through the selenium driver and measure the time remaining to play in the while loop (I don't know how to extract this information from the YouTube page)

Thanks!

[Edits with solution]

Selenium solution

Thanks to Stanjer and this answer and this other answer you can get movie_player 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 noting: driver.text returns a string representing the video timer in this string format '1: 00/2: 00'. So you can check if the video has played something in this direction:

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

[edit] According to Hos's comment, this information can also be accessed:

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

Complication and the next question

I need to open the video in maximum format and with auto play.

This means that I am invoking the following URL:

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

However, this returns a very short html that contains only embed code, for 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 do not have the movie_player element.

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

Approach 2 - If I run a YouTube video on a classic Youtube page, how can I tell movie_player to maximize myself?

(note: this answer and this answer probably contains information for solving approach 2, will post if I get this to work with Selenium)

+5
source share
3 answers

You can execute the javascript API in the context of the 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 - this is when the video ended

You can add an artist to the loop by checking the status every N seconds.

+6
source

You can get the length of the video and the current playback time as text, and then convert them to seconds. In addition, as a loop, you can wait until the current time reaches the length of the video.

 length_str = driver.find_element_by_class_name("ytp-time-duration").text current_time_str = driver.find_element_by_class_name("ytp-time-current").text import re length = re.findall(r'\d+', length_str) # convert ['2:24'] to ['2', '24'] current_time = re.findall(r'\d+', current_time_str) length_sec = 60 * int(length[0]) + int(length[1]) current_time_sec = (60 * int(current_time[0]) + int(current_time[1])) remaining_time = length_sec - current_time_sec 
+1
source
 driver.find_element_by_class_name("ytp-time-current").text 

It only works when the time and name are displayed on the screen. After a couple of seconds, the names disappear and the return value is "".

+1
source

All Articles