~mil/f_scripts

ref: dev-f_xorg f_scripts/scripts/f_youtube -rwxr-xr-x 2.4 KiB
2344f048Miles Alan f_xorg: Use fbvncm fork, switch to i3, don't run fbvnc in loop 11 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env osh
shopt -s strict:all
DEP="codemadness-frontends mpv youtube-dl"
DEC="Listen or view youtube videos via mpv"
DOC="
  f_youtube allows you to search and playback either audio or video from
  youtube. It uses codemadness's youtube-cli tool to find results from
  youtube and allows you to either playback in the terminal the video
  (e.g. as low-fidelity ASCII) or allows you to just play the audio back.

  Set \$F_YOUTUBE_SEARCHQUERIES to be a space seperated list of tokens
  to customize the default search queries.
"
VAR="
  F_YOUTUBE_SEARCHQUERIES='asmr cat fail news'
"

search() {
  youtube-cli "$@" |
    grep -Ev '^(Channelid|Atom feed|Channel title|Published|Viewcount|Userid):' |
    sed -E 's/^(URL|Duration):\s+/\t/g' |
    tr -d '\n' |
    sed 's/===/\n/g' |
    awk -F'\t' '{ print $3 " " $1 " " $2}' |
    nl
}

play() {
  local AV URL

  AV="$1"
  URL="$2"

  [ -p /tmp/fbp.fifo ] && { echo -e "\b\f\e"; echo "9 0 [ ] p" | tr " " "\n"; } > /tmp/fbp.fifo
  if [ "$AV" = "video" ]; then
    mpv --ytdl-format="[height<420]" --vo=tct --really-quiet --vo-tct-256=yes --profile=sw-fast "$URL"
  else
    mpv -v --no-video "$URL"
  fi
}

searchandpick() {
  local QUERY AV SEARCHRESULT SEARCHRESULTSIZE NUMBER

  AV="$1"
  QUERY="$2"
  SEARCHRESULT="$(search "$QUERY")"
  if test $? -eq 0; then
    # Pick a number
    SEARCHRESULTSIZE="$(echo "$SEARCHRESULT" | wc -l)"
    printf %b "-----\n${SEARCHRESULT}\n----\n"

    [ -p /tmp/fbp.fifo ] && { echo -e "\b\f\r"; seq "$SEARCHRESULTSIZE"; } > /tmp/fbp.fifo
    read -p "Pick number: " NUMBER

    # Play
    play "$AV" "$(
      echo -e "$SEARCHRESULT" | 
        grep -E "^\s*${NUMBER}\s" | 
        awk -F' ' '{print $NF}'
    )"
  fi
}

devicepine64pinephone() {
  amixer set "AIF1 DA0" "100%" > /dev/null
  amixer set "AIF1 Slot 0 Digital DAC" unmute > /dev/null
}

main() {
  local AV QUERY

  env | grep -q "^$(basename "$0" | tr '[a-z]' '[A-Z]')=" || eval "$VAR"
  eval "$(grep deviceinfo_codename /etc/deviceinfo | cut -d= -f2 | tr -d \"- | xargs -ID echo deviceD)"

  OPTS="audio video"
  [ -p /tmp/fbp.fifo ] && { echo -e "\b\f\r"; echo "$OPTS" | tr " " "\n"; } > /tmp/fbp.fifo
  read -p "Output ($OPTS): " AV

  OPTS="asmr cat fail"
  [ -p /tmp/fbp.fifo ] && { echo -e "\b\f\r"; echo "$F_YOUTUBE_SEARCHQUERIES" | tr " " "\n"; } > /tmp/fbp.fifo
  read -p "Query ($F_YOUTUBE_SEARCHQUERIES): " QUERY

  searchandpick "$AV" "$QUERY"
}

if [ -n "$1" ]; then "$@"; else main; fi