Browse Source

Scripts: Move streaming videos to play script, update script arguments

master
Riyyi 3 years ago
parent
commit
1ae30d54b1
  1. 31
      .local/bin/aliases
  2. 227
      .local/bin/play

31
.local/bin/aliases

@ -99,37 +99,6 @@ shuffle() {
echo "$1" | fold -w 1 | shuf | tr -d '\n' echo "$1" | fold -w 1 | shuf | tr -d '\n'
} }
stream() {
channel="$1"
quality="$2"
lowLatency="$3"
if [ -z "$quality" ]; then
streamlink --quiet --config "/dev/null" "https://twitch.tv/$1"
[ "$?" -ne 0 ] && return 1
printf "Select stream quality: "
read -r quality
fi
if [ -n "$lowLatency" ]; then
arguments="\
--twitch-low-latency \
--player-args '--cache=yes --demuxer-max-bytes=750k' \
"
fi
echo "Starting stream https://twitch.tv/$channel @ $quality"
command="setsid -f streamlink \
--twitch-disable-hosting \
--twitch-disable-reruns \
--player mpv \
--stream-sorting-excludes '<480p,>=1080p' \
$arguments \
https://twitch.tv/$channel $quality > /dev/null 2>&1"
eval "$command"
}
update_mirrorlist() { update_mirrorlist() {
sudo rm /etc/pacman.d/mirrorlist.pacnew sudo rm /etc/pacman.d/mirrorlist.pacnew
sudo reflector --latest 100 --protocol https --sort rate --save /etc/pacman.d/mirrorlist sudo reflector --latest 100 --protocol https --sort rate --save /etc/pacman.d/mirrorlist

227
.local/bin/play

@ -1,6 +1,6 @@
#!/bin/sh #!/bin/sh
# Depends: socat # Depends: GNU getopt, mpv, socat, streamlink
SOCK="/tmp/umpv-fifo" SOCK="/tmp/umpv-fifo"
@ -18,95 +18,230 @@ ${B}SYNOPSIS${N}
${B}DESCRIPTION${N} ${B}DESCRIPTION${N}
play is a script to manage different mpv viewing bahavior. play is a script to manage different mpv viewing bahavior.
If no ${U}URLS${N} argument is provided, it is read from clipboard instead. If no ${U}URLS${N} argument is provided, it is read from the clipboard instead.
The queue option starts a mpv window with a playlist, which all videos of The queue option starts a mpv window with a playlist, which all videos of
subsequent queue calls get added to. subsequent queue calls get added to.
${B}OPTIONS${N} ${B}OPTIONS${N}
${B}help${N} ${B}-h${N}, ${B}--help${N}
Display usage message and exit. Display usage message and exit.
[${B}play${N}] [${U}URLS${N}...] (default) ${B}-q${N}, ${B}--quality${N} ${U}QUALITY${N}
Plays videos in a new mpv window. Set the video quality, example: ${U}720p60${N}.
${B}shuffle${N} [${U}URLS${N}...] ${B}-f${N}, ${B}--shuffle${N} [${U}URLS${N}...]
Shuffle audio playlist (disables video playback). Shuffle audio playlist (disables video playback).
${B}queue${N} [${U}URLS${N}...] ${B}-q${N}, ${B}--queue${N} [${U}URLS${N}...]
Add multiple videos to the unique mpv's queue. Add multiple videos to the unique mpv's queue.
${B}-s${N}, ${B}--stream${N} [${U}URLS${N}...]
Play video(s) using streamlink.
${B}-l${N}, ${B}--low-latency${N}
Enable twitch low-latency mode in streamlink.
EOF EOF
} }
CLIP="$(xclip -selection clipboard -out)" # --------------------------------------
play() { qualityFormat() {
MPV="mpv --ytdl-raw-options=add-metadata=,external-downloader=aria2c" # , separator = download several formats
if [ -z "$1" ]; then # + separator = merge several formats into a single file
MPV="$MPV $CLIP" # / separator = or
# Cut off everything after space if [ -z "$quality" ]; then
LINK="$(echo "$CLIP" | sed -nE 's/^(\S+).*/\1/p')" echo "bestvideo[height<=?1080]+bestaudio/best"
else else
MPV="$MPV $*" height="$(echo "$quality" | awk -F 'p' '{ print $1 }')"
# Determain which argument holds the urls fps="$(echo "$quality" | awk -F 'p' '{ print $2 }')"
[ "$1" = "${1#-}" ] && DISPLAY="$1" || DISPLAY="$2"
# Cut off everything after space format="bestvideo[height<=?$height]"
LINK="$(echo "$DISPLAY" | sed -nE 's/^(\S+).*/\1/p')" if [ -n "$fps" ]; then
format="${format}[fps<=?$fps]"
fi fi
format="$format+bestaudio/best"
notify-send -t 2500 "Loading video: $LINK" echo "$format"
# Attempt to load video fi
[ "$($MPV)" ] && notify-send -u critical -t 4000 "Loading video failed.." }
play() {
url="$1"
notify-send -t 2500 "Loading video: $url"
setsid -f mpv --no-terminal \
--ytdl-format="$(qualityFormat)" \
--ytdl-raw-options=add-metadata=,external-downloader=aria2c \
"$url"
} }
shuffle() { shuffle() {
# Skip first argument urls="$*"
shift 1
setsid -f mpv --no-video --shuffle \ mpv --no-video --shuffle \
--ytdl-format='bestaudio[ext=m4a]' \ --ytdl-format='bestaudio[ext=m4a]' \
"${@:-$CLIP}" --ytdl-raw-options=add-metadata=,external-downloader=aria2c \
"$urls"
} }
queue() { queue() {
OPTIONS="--no-terminal --force-window --input-ipc-server=$SOCK --" urls="$*"
options="--no-terminal --force-window --input-ipc-server=$SOCK"
# Create mpv cache directory # Create mpv cache directory
DIR="$(dirname "$SOCK")" dir="$(dirname "$SOCK")"
[ ! -d "$DIR" ] && mkdir -p "$DIR" [ ! -d "$dir" ] && mkdir -p "$dir"
# Delete socket if no umpv is running # Delete socket if no umpv is running
if ! pgrep -f "mpv $OPTIONS" > /dev/null; then if ! pgrep -f "mpv $options" > /dev/null; then
rm -f "$SOCK" rm -f "$SOCK"
fi fi
# Skip first argument
shift 1
# Set url to argument if provided, clipboard otherwise
URLS="${*:-$CLIP}"
if [ -S "$SOCK" ]; then if [ -S "$SOCK" ]; then
notify-send -t 2500 "Added video to queue.." notify-send -t 2500 "Added video to queue.."
# Add video to named pipe # Add video to named pipe
echo "$URLS" | awk -v RS=' ' '{ print "raw loadfile "$1" append" }' \ echo "$urls" | awk -v RS=' ' '{ print "raw loadfile "$1" append" }' \
| socat UNIX-CONNECT:"$SOCK" - | socat UNIX-CONNECT:"$SOCK" -
else else
# Play video # Play video
notify-send -t 2500 "Loading video: $URLS" notify-send -t 2500 "Loading video: $urls"
# shellcheck disable=2086 # shellcheck disable=2086
setsid -f mpv $OPTIONS $URLS setsid -f mpv $options \
--ytdl-format="$(qualityFormat)" \
--ytdl-raw-options=add-metadata=,external-downloader=aria2c \
$urls
fi fi
} }
case "$1" in stream() {
help) channel="$1"
quality="$2"
lowLatency="$3"
if [ -z "$quality" ]; then
if ! streamlink --quiet --config /dev/null "$channel"; then
return 1
fi
printf "Select stream quality: "
read -r quality
fi
if [ -n "$lowLatency" ]; then
arguments="\
--twitch-low-latency \
--player-args '--cache=yes --demuxer-max-bytes=750k'"
fi
echo "Starting stream $channel @ $quality"
command="setsid -f streamlink \
--twitch-disable-hosting \
--twitch-disable-reruns \
--player mpv \
--stream-sorting-excludes '<480p,>=1080p' \
$arguments \
$channel $quality > /dev/null 2>&1"
eval "$command"
}
# Option parsing
# --------------------------------------
script="$(basename "$0")"
parsed="$(getopt --options "hslquf" \
--longoptions "help,stream,quality,queue,shuffle" \
-n "$script" -- "$@" 2>&1)"
result="$?"
# Exit if invalid option is provided
if [ "$result" -ne 0 ]; then
echo "$parsed" | head -n 1 >&2
echo "Try './$script --help' for more information." >&2
exit 1
fi
eval set -- "$parsed"
streamOption=0
qualityOption=0
lowLatencyOption=0
queueOption=0
shuffleOption=0
while true; do
case "$1" in
-h | --help)
help help
exit
;;
-s | --stream)
streamOption=1
shift
;;
-l | --low-latency)
lowLatencyOption=1
shift
;;
-q | --quality)
qualityOption=1
shift
;;
-u | --queue)
queueOption=1
shift
;; ;;
shuffle) -f | --shuffle)
shuffle "$@" shuffleOption=1
shift
;; ;;
queue) --)
queue "$@" shift
break
;; ;;
*) *)
play "$@" break
;; ;;
esac esac
done
# Target parsing
# --------------------------------------
# Incompatible flags
result=$((streamOption + queueOption + shuffleOption))
if [ $result -gt 1 ]; then
echo "Incompatible flags." >&2
echo "Try './$script --help' for more information." >&2
exit 1;
fi
# Get quality
if [ $qualityOption -eq 1 ]; then
quality="$1"
shift
fi
# Set url to clipboard if none provided
clip="$(xclip -selection clipboard -out)"
if [ "$#" -eq 0 ]; then
eval set -- "$clip"
fi
[ $lowLatencyOption -eq 1 ] && lowLatency="1"
# Execute
# --------------------------------------
if [ $streamOption -eq 1 ]; then
for url in "$@"; do
stream "$url" "$quality" "$lowLatency"
done
elif [ $shuffleOption -eq 1 ]; then
shuffle "$*"
elif [ $queueOption -eq 1 ]; then
queue "$*"
else
for url in "$@"; do
play "$url"
done
fi

Loading…
Cancel
Save