Can I pause YouTube in Chrome from the command line?
You could start the Chrome session (with your Youtube playlist) using the Chrome WebDriver:
WebDriver is an open source tool for automated testing of webapps across many browsers. It provides capabilities for navigating to web pages, user input, JavaScript execution, and more. ChromeDriver is a standalone server which implements WebDriver's wire protocol for Chromium. ChromeDriver is available for Chrome on Android and Chrome on Desktop (Mac, Linux, Windows and ChromeOS).
Install the following dependency:
sudo apt-get install python-selenium
And download the Chromedriver from here, select the one corresponding to your architecture e.g:
http://chromedriver.storage.googleapis.com/2.14/chromedriver_linux64.zip or http://chromedriver.storage.googleapis.com/2.14/chromedriver_linux32.zip
Extract the chromedriver
file for example in your $HOME
folder.
Then start the chromedriver from python, open a terminal and type:
$ python
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> from selenium import webdriver
>>> from selenium.webdriver.chrome.options import Options
>>> chrome_options = Options()
>>> chrome_options.add_argument("--disable-sync")
>>> driver = webdriver.Chrome(os.path.expanduser('~/chromedriver'), chrome_options=chrome_options)
>>> # Open the desired youtube page:
>>> driver.get('https://www.youtube.com/watch?v=NxD_kWK8A5M&list=PLMquns5MbFKm_cVrB0ZjKlIlS5HCQL1dL')
>>> # Let selenium find the player container <div>
>>> video = driver.find_element_by_id("player-api")
>>> # And just click to play/pause your video:
>>> video.click()
>>>
Note: You can still use the Chrome instance started by the Chrome WebDriver for browsing in other tabs. even if the Youtube tab is not the active one (no focus). The video.click()
events will continue to work.
Well, I guess you could always use a tool like xdotool
to send a k key-press to your YouTube window. The downside to this method is that you have to activate the window before sending the key-press (Chrome ignores keyboard input when it isn't focused).
The following script might work for you
#!/bin/bash
# Dependencies: xdotool (sudo apt-get install xdotool)
# Functions
save_active () {
# get current workspace
ActiveDesktop="$(xdotool get_desktop)"
# get current active window ID
ActiveWindowID="$(xdotool getactivewindow)"
# get current active window name
ActiveWindowName="$(xdotool getwindowname "$ActiveWindowID")"
}
restore_active(){
xdotool set_desktop "$ActiveDesktop"
# Activating the root window (Desktop) results in an error message, so we
# try to avoid it
[[ "$ActiveWindowName" != "Desktop" ]] && xdotool windowactivate "$ActiveWindowID"
}
youtube_playpause(){
xdotool search --name YouTube windowactivate
sleep 0.1
xdotool key --clearmodifiers k
}
# Main
## save active window and desktop
save_active
## activate Chrome YouTube window and send keyboard event
youtube_playpause
## restore previously active window/desktop
restore_active
If controlling YouTube with your media keys is what you're after, there seem to be some extensions out there that claim to add this functionality to Chrome:
https://chrome.google.com/webstore/detail/key-socket-media-keys/fphfgdknbpakeedbaenojjdcdoajihik/reviews
https://github.com/VivekPanyam/YoutubeMediaKeysChrome
I haven't given them a try myself, yet.
Easy: run the spotify/youtube in one session of chrome and all your other stuff in another chrome session. Then just
kill -SIGSTOP [pid]
to pause, and:
kill -SIGCONT [pid]
to resume.
If you make a little script to open two sessions of chrome, by:
google-chrome http://spotify.com/myplaylist http://youtube.com/myplaylist &
pgrep google-chrome > /tmp/TimChromepid.RUN
google-chrome &
and have the pid ready for your toggle script:
if [ -f /tmp/TimChromepid.RUN ]; then
mv /tmp/TimChromepid.RUN /tmp/TimChromepid.PSD
kill -SIGSTOP < /tmp/TimChromepid.PSD
else
mv /tmp/TimChromepid.PSD /tmp/TimChromepid.RUN
kill -SIGCONT < /tmp/TimChromepid.RUN
fi
It would pause spotify and Youtube and whatever you put in the first Chrome session.