How do I code a Python script for running a browser video in loop at playback speed more than 2x?

Question:

Learning python scripts but I thought of experimenting with something for a browser video. I’m trying to make the python script that will run that particular browser video in a loop for as many times I want and in 4x playback speed.

As I searched many tools appeared like selenium with python.
But still, I’m looking for some help through this.

Asked By: Ankit Kashyap

||

Answers:

You can use any browser automation tool to do this.
Here is the solution for your various use case using selenium :

YouTube

Imports and page load

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By

chrome_path = r"C:UsershpoddarDesktopToolschromedriver_win32chromedriver.exe"

s = Service(chrome_path)
driver = webdriver.Chrome(service=s)
driver.get('https://www.youtube.com/watch?v=c-YO1MRGl3M')

Utility to wait for Ad

The below code waits until the Ad completes. Obviously you can do this using selenium’s wait.until(EC.invisibility_of_element. Let me know if you need that code. Here we are doing this using javascript. Its better you do this every x period(say).

waitForAd  = '''if (document.querySelector("div.ad-showing"))
                    return true
                return false
             '''
while(driver.execute_script(waitForAd)): pass

Play on Loop

To play the video in loop, you can set the video object property.

playInLoop = '''const video = document.getElementsByClassName("video-stream html5-main-video")[0]
                    video.loop = true
             '''
driver.execute_script(playInLoop)

To play on loop on some condition you can check you can write a javascript function that plays the video on loop until the variable play is set, when you want the video to stop playing again, you can set the variable play as false.

Change playback speed

Here we have passed 4, you can pass anything here

driver.execute_script('''document.querySelector('video').playbackRate = 4;''')

Obviously all the above script can be formatted on user input by using python’s format string.

Vimeo

The above example was for a youtube video. Here is how you can do the same for a vimeo embedded video.

Get Element

getVideo = '''vid = document.getElementsByTagName('video')[0]'''
driver.execute_script(getVideo)

Play on loop

Again, the loop attribute can be set to false on user input by making an execute_script request again.

playOnLoop = '''vid.loop = true'''
driver.execute_script(playOnLoop)

Change playback speed

Here we have set it to 3, you can set anywhere between the range

playbackRate= '''vid.playbackRate = 3'''
driver.execute_script(playbackRate)

Pausing the video

pause = '''vid.pause()'''
driver.execute_script(pause)

Vimeo provides multiple API’s for a video element that can satisfy all your project requirement. Try exploring the documentation for it.

Answered By: Himanshu Poddar