Can I pause YouTube in Chrome from the command line?
I can pause Spotify by pausing all system programs playing (well, toggling). How can I do something like that to pause/play YouTube videos in Google Chrome?
Solution 1:
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.
Solution 2:
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.
Solution 3:
Chromium now uses D-Bus to implement MPRIS for media content currently active in the browser, and browsers built on top of Chromium such as Google Chrome and Brave inherit this feature. That means media content currently active in such browsers can be controlled using a utility such as playerctl which can send and receive MPRIS commands.
You can download and install playerctl from their releases section on GitHub and then run this command to pause currently running media in Chrome:
$ playerctl pause
Or you can control a specific instance of Chrome:
$ playerctl --list-all
chrome.instance24818
$ playerctl --player=chrome.instance24818 pause