u/SupermarketAntique32

YouTube search and play in 20 lines of bash
🔥 Hot ▲ 76 r/commandline

YouTube search and play in 20 lines of bash

I was searching for YouTube TUI and stumble upon YT-X. But I don't need all the features and only need to search and play a video. So I wrote a simple 20 lines bash script.

#!/usr/bin/env bash
#
# `ytfp`: Search YouTube video using `fzf` and play it in `mpv`
# Require `fzf`, `yt-dlp`, `chafa`, `mpv`
#

SEARCH_VID() {
  yt-dlp "https://www.youtube.com/results?search_query=$1" \
    --flat-playlist --playlist-items 1:20 --print \
    $'%(thumbnails.0.url)s\t%(title)s\t%(channel)s\t%(view_count)s\t%(url)s' \
    | grep --extended-regexp --invert-match 'playlist|channel'
}
RENDER_VID_INFO() {
  curl --silent "$1" | chafa --format=sixels --size=x14 --clear
  echo "Title   : $2"
  echo "Channel : $3"
  echo "Views   : $4"
}
export -f SEARCH_VID RENDER_VID_INFO

fzf --preview-window down --layout reverse --disabled --with-shell 'bash -c' \
  --bind 'start:reload:SEARCH_VID fzf' \
  --bind 'change:reload:sleep 0.3; SEARCH_VID {q} || true' \
  --delimiter '\t' --with-nth 2 \
  --preview 'RENDER_VID_INFO {1} {2} {3} {4}' \
  --bind 'enter:execute-silent(mpv --fullscreen {5})'
u/SupermarketAntique32 — 6 hours ago