"Picture-in-Picture" mode for YouTube videos in 4 lines of bash code

Add this function to your ~/.bashrc:

youtube-stream-mpv() {
    local title=$(youtube-dl -e "$1") 
    local mpvopts="--force-seekable=yes --ontop" 
    if [[ "$OSTYPE" == "darwin"* ]]; then
        mpvopts="$mpvopts --ontop-level=window --on-all-workspaces --macos-app-activation-policy=accessory" 
    fi
    youtube-dl -o - "$1" | $mpv --title="mpv - $title - \${width}x\${height}" $mpvopts -
}
Update from 2021

It turned out that mpv has built-in support for youtube-dl (lol), so now my function looks like this:

youtube-stream-mpv() {
    local mpvopts="--ontop --on-all-workspaces" 
    if [[ "$OSTYPE" == "darwin"* ]]
    then
        mpvopts="$mpvopts --ontop-level=window --on-all-workspaces --macos-app-activation-policy=accessory" 
    fi
    mpv $mpvopts ytdl://"$1"
}

Make sure that you have mpv and youtube-dl installed.

Then just type youtube-stream-mpv <LINK-TO-YOUTUBE-VIDEO> in terminal, and magic shall happen.

macOS

Install mpv and youtube-dl from Homebrew:

$ brew install --cask mpv
$ brew install youtube-dl

and it will work just as well as on Linux.

If --on-all-workspaces doesn't work in your mpv, make sure it's fresh enough. You need this patch, not sure when it'll land in brew.

Awesome WM

When I used to use Awesome WM, I also wrote a of plugin for it, to fix mpv window position and properties:

To use the plugin, add to your rc.lua something like this:

local youtube = require('./youtube-stream-mpv')
youtube.set_top_bar_height(mouse.screen.mywibox.height)
youtube.set_default_width(350)
youtube.set_hide_titlebar(true)
If you have any comments, contact me by email.
powered by OpenBSD
© ch1p 2021