Skip to content

Instantly share code, notes, and snippets.

@noromanba
Last active April 8, 2017 18:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noromanba/f2e3efb9e0185e9cfe2a7c0888ee1e54 to your computer and use it in GitHub Desktop.
Save noromanba/f2e3efb9e0185e9cfe2a7c0888ee1e54 to your computer and use it in GitHub Desktop.
play yt livestreaming from Terminal
#!/usr/bin/env bash
# play yt livestreaming from Terminal;
# and/or workaround of mpv HLS buffer secs to empty BUG
# @version 2017.4.8.0
# @author noromanba
# @license https://opensource.org/licenses/MIT
# https://www.shellcheck.net/
set -euC
MY_NAME=$(basename "${0}")
# TODO player-options
if [ $# -lt 1 ]; then
echo "usage: $MY_NAME {YT_URL} [quality]"
exit 1
fi
# TODO handle channel w/ multi stream
STREAM_URL="${1}"
# quality e.g.
# Available streams: 144p (worst), 240p, 360p, 480p, 720p, 1080p (best)
# 360p> same audio quality ~128kbps
RESOLUTION="${2:-360p}"
CHANNEL_NAME=$(GET "$STREAM_URL" | grep -P -o '"name":\s*"\K[^"]+')
# --restrict-filenames can not apply to stream title
#TITLE=$(youtube-dl --get-title "$STREAM_URL" | tr -d "'")
# c.f.
# https://gist.github.com/noromanba/34b7ba05dbccb4fa844c18a88ff38d89#title
# TODO completely replace of numeric/number character entity/reference
# "'" : single quote "'"
TITLE=$(GET "$STREAM_URL" | grep -o -P '<title>\K.+(?=</title>)' | sed 's/&#39;//')
# stdin method; can not use key-input from Terminal {{{
# stdout workaround due to `-p` complex escaping " and ' within $TITLE
# $ streamlink -p "mpv -title "$TITLE" ..."
# https://streamlink.github.io/cli.html#cmdoption-stdout
streamlink --verbose-player "$STREAM_URL" "$RESOLUTION" --stdout | \
mpv -volume 50 -cache-initial 1000 -title "$TITLE - $CHANNEL_NAME" - #"${@:3}"
# TODO
# M3U heredoc
# arguments for player
# }}}
# available key-input method {{{
# XXX unstable buffer same as use mpv only
#mpv -volume 50 -cache-initial 1000 -cache-secs 20 -title "$TITLE - $CHANNEL_NAME" \
# REMOVE_THIS_PREFIX_WHEN_ENABLE_BELOW_shellcheck disable=2016
# https://github.com/koalaman/shellcheck/wiki/SC2016
# -term-playing-msg '${title}' \
# "$(streamlink --stream-url "$STREAM_URL" "$RESOLUTION")"
#
# }}}
# DBG
# -msg-level 'all=v'
# https://mpv.io/manual/master/#options-msg-level
# no complete silence
# fatal fatal messages only
# error error messages
# warn warning messages
# info informational messages
# status status messages (default)
# v verbose messages
# debug debug messages
# trace very noisy debug messages
# Example
# mpv --msg-level=ao/sndio=no
# Completely silences the output of ao_sndio, which uses the log prefix [ao/sndio].
# mpv --msg-level=all=warn,ao/alsa=error
# Only show warnings or worse, and let the ao_alsa output show errors only.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment