我可以从命令行暂停Chrome中的YouTube吗?

我可以暂停所有播放的系统程序(好吧,切换)来暂停Spotify。 如何在Google Chrome中暂停/播放YouTubevideo?

好吧,我猜你总是可以使用像xdotool这样的工具向你的YouTube窗口发送一个按键。 此方法的缺点是您必须在发送按键之前激活窗口(Chrome在未聚焦时忽略键盘输入)。

以下脚本可能适合您

 #!/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 

如果您使用媒体密钥控制YouTube就是您所追求的,那么似乎有一些声称可以向Chrome添加此function的扩展程序:

我自己还没试过。

您可以使用Chrome WebDriver启动Chrome会话(使用Youtube播放列表):

WebDriver是一个开源工具,用于跨多个浏览器自动测试webapps。 它提供了导航到网页,用户输入,JavaScript执行等function。 ChromeDriver是一个独立的服务器,它为Chromium实现了WebDriver的有线协议。 ChromeDriver适用于Android上的Chrome和桌面版Chrome (Mac,Linux,Windows和ChromeOS)。

安装以下依赖项:

 sudo apt-get install python-selenium 

从这里下载Chromedriver,选择与您的架构相对应的一个,例如:

http://chromedriver.storage.googleapis.com/2.14/chromedriver_linux64.zip或http://chromedriver.storage.googleapis.com/2.14/chromedriver_linux32.zip

例如,在$HOME文件夹中提取chromedriver文件。

然后从python启动chromedriver,打开一个终端并输入:

 $ 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 
>>> video = driver.find_element_by_id("player-api") >>> # And just click to play/pause your video: >>> video.click() >>>

注意 :您仍然可以使用Chrome WebDriver启动的Chrome实例在其他标签中进行浏览。 即使Youtube选项卡不是活动选项卡(没有焦点)。 video.click()事件将继续有效。

在此处输入图像描述

简单:在一个chrome会话中运行spotify / youtube,在另一个chrome会话中运行所有其他内容。 然后就是

 kill -SIGSTOP [pid] 

暂停,并:

 kill -SIGCONT [pid] 

恢复。

如果您制作一个小脚本来打开两个chrome会话,请执行以下操作:

 google-chrome http://spotify.com/myplaylist http://youtube.com/myplaylist & pgrep google-chrome > /tmp/TimChromepid.RUN google-chrome & 

并为您的切换脚本准备好pid:

 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 

它会暂停spotify和Youtube以及你在第一个Chrome会话中放置的内容。