Skip to content

talwrii/yt-cli-rmt-guide

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 

Repository files navigation

Youtube command-line remote control

@readwithai - X - blog - machine-aided reading - 📖⚡️🖋️

This pages describes how to remotely control youtube running within a browser from the command-line. Previously this sort of tool required an extension like streamkeys. However, Chrome now has native support for a protocol called MPRIS which allows any application to control playing media (via DBUS).

Using the MPRIS interface in chrome, you can:

You can do things like:

  • Stop the player
  • Skip forward
  • Get the current position in the audio. Suitable for adding to notes or shared with others.
  • Get the length of the video

Obtaining the position may be particularly useful when combined yt-dlp to obtain subtitles for the video.

Usage

You can use playerctl from the command-line. You may like to explore the documentation. This script can be wrapped for ther purposes.

I use the following scripts (amongst others):

skip: Skip 10 seconds forward (or number of seconds given)

#!/bin/bash
playerctl ${1-10}+

back: Skip 10 seconds backwards (or number of seconds given)

#!/bin/bash
playerctl ${1-10}-

pos: Return the position into the video

#!/bin/bash
playerctl position

yt-position: Return the percentage through the video

#!/bin/bash
length=$(playerctl metadata  | grep length | awk '{ print $3 }')
pos=$(playerctl position)
perl -e 'my ($a,$b) = @ARGV; print(int($a * 1000000 / $b * 100) . "%\n")' "$pos" "$length"

yt-link: Return a link to the video 10 seconds ago

#!/bin/bash
mozeidon tabs  get  | jq '.[] | .[] | select(.url | test("youtube\\.com")) | .url ' -rc | { read line ; echo $line\&t=$(playerctl position | perl -pe '$_ -= 10') ; }

I use this script (and others) to refer to videos while taking notes in Obsidian. If you are interested in note taking you might like to read my review of note taking in Obsidian.

Caveats

The interface presented by MPRIS only supports a single playing source of media. If you have more than one thing playing (such as background music and a video that you are reviewing), then the most recently used tab is controlled. To change the media that is controlled by MPRIS you can select the current tab.

mozeidon can remotely fetch information about the tabs (including the currently selected tab) and switch between tabs. This could be used to find the playing tab if the media is playing.

Alternatives

You may prefer to download media from youtube using yt-dlp to download media and play it in another player.

A shortcut to jump to the youtube tab may be sufficient for your uses. I have down this before with a combination of key simulation with xdotool interacting with the tab selection feature and wmctrl to raise windows. You can aso used mozeidon to select tabs

It is to be noted that the youtube website provides a variety of social and recommendation featues that other approaches do not support.

References

  1. yt-dlp
  2. mozeidon

About me

I am @readwithai. I create tools for reading, research and agency sometimes using the markdown editor Obsidian.

I also create a stream of tools that are related to carrying out my work.

I write about lots of things - including tools like this - on X. My blog is more about reading and research and agency.

About

A guide to controlling youtube within Chromium-based browsers (Brave, Chrome, Vivaldi) from the command-line (cli)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors