7

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?

9
  • 3
    These should probably be separate questions, as it may require 2 different answers from two different people to answer this. Good questions, though.
    – MGodby
    Oct 17, 2014 at 10:20
  • This would require some kind of API call to the HTML5 or Flash video player through Chrome - I don't think there's any kind of setup that exists that can do that.
    – Thomas Ward
    Jan 28, 2015 at 17:57
  • @Thom I wondered if there was an API that could... Like an offline version of ifttt.
    – Tim
    Jan 28, 2015 at 17:58
  • 1
    @Tim I do not know - that may be a different question you would post.
    – Thomas Ward
    Jan 28, 2015 at 18:31
  • 1
    Ubuntu webapp should be able to do that, im not sure if it works now. Here is video: youtube.com/watch?v=8CtKNf53HvA
    – Aiphee
    Feb 1, 2015 at 17:27

4 Answers 4

9

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
2
  • 2
    To the top please! May 28, 2021 at 9:54
  • I second this motion.
    – Jeff_V
    Mar 12, 2022 at 21:22
5
+100

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:

I haven't given them a try myself, yet.

6
  • These aren't working for me I'm afraid. However, thanks for the suggestion. The other issue I would have it spotify is paused by the button, and I don't want to be "switching" them... +1 anyway though for the idea.
    – Tim
    Feb 4, 2015 at 17:09
  • Atually, the first one does work. However, that is now monopolising the Media Keys... :/ I think I'd like an extention like it that doesn't use media keys. I will see if I can extract and reprogram that one. I'm gonna offer another +50 bounty and give you that one.
    – Tim
    Feb 4, 2015 at 17:11
  • 1
    Changed my mind, I'll put up with no spotify play / pause with media keys. +100
    – Tim
    Feb 4, 2015 at 18:22
  • @Tim Thanks for assigning me the bounty. That's very generous of you. FWIW, to still be able to control Spotify via the keyboard you could use the DBUS interface. For instance, dbus-send --print-reply --session --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlayPause will play/pause a track. I don't know if Unity supports this, but you could try assigning these DBUS commands to CTRL + <respective media key>. Feb 4, 2015 at 19:20
  • Problems with this script : it gets confused when there are several "youtube" windows. In particular, this very window (this question) has Youtube in its name so counts as one. When there are several, it seems to cycle through them as you call it again and again. That's a great deal of awesome pointers all the same, thanks much. Mar 22, 2020 at 9:07
4

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.

enter image description here

12
  • Ooh, very nice! What about being signed into chrome?
    – Tim
    Feb 3, 2015 at 17:00
  • It says that sync is disabled by my admin...
    – Tim
    Feb 3, 2015 at 17:02
  • @Time I've updated my answer with a possible workaround for the sync error. I can't test it myself as it works out of the box. Could you please give it a try? Feb 3, 2015 at 18:16
  • Wow! If we can get that to work on Tim's machine, that would mean everything could be scripted from python...
    – Fabby
    Feb 3, 2015 at 18:17
  • @Tim Sylvain has edited the post but he mistyped your handle. Come and have another look!
    – Fabby
    Feb 3, 2015 at 18:21
3

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.

11
  • And knowing the Tim from the answers instead of the questions, he's now [facepalm]ing himself! ;-) It's a bit of a rough draft now, but you'll get the gist... If not, I'll edit and clean up a bit... (drop me a note @Fabby)
    – Fabby
    Jan 30, 2015 at 10:42
  • Does that stop/ prevent chrome from surfing the web too or just stops the youtube? Jan 30, 2015 at 11:07
  • I know Firefox has plugin container, so we could pause that with the SIGSTOP and Internet should still be accessible , but what about chrome ? Jan 30, 2015 at 11:09
  • @Serg: the above "pauses" one entire chrome session with everything in it, so that's why you need two: One to continue surfing while the other one is paused... (or not, depending on the use case) ;-)
    – Fabby
    Jan 30, 2015 at 11:30
  • 1
    My goodness this is a wonderful hack. I salut the "linuxiness" of it.
    – Att Righ
    Apr 29, 2022 at 9:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .