diff --git a/.config/i3/config b/.config/i3/config index 80423df..f15c8d0 100644 --- a/.config/i3/config +++ b/.config/i3/config @@ -89,6 +89,10 @@ bindsym $mod+e layout toggle split # Lock bindsym $mod+$mod2+l exec $HOME/.scripts/wm/lock.sh +# mpv +bindsym $mod+m exec --no-startup-id $HOME/.scripts/mpv.sh +bindsym $mod+Shift+m exec --no-startup-id $HOME/.scripts/mpv.sh queue + # Restart i3 inplace bindsym $mod+Shift+r restart # Exit i3 diff --git a/.config/zsh/.zshrc b/.config/zsh/.zshrc index f7148d6..04383df 100644 --- a/.config/zsh/.zshrc +++ b/.config/zsh/.zshrc @@ -127,6 +127,7 @@ alias gl="git log --graph --abbrev-commit --decorate --format=format:'%C(bold bl alias mc="$HOME/.scripts/mediacontrol.sh" alias nw="$HOME/.scripts/network.sh" alias vp="$HOME/.scripts/vimplugin.sh" +alias mpvshuffle="$HOME/.scripts/mpv.sh shuffle" # Laptop alias offtouchpad='sudo rmmod i2c_hid' @@ -135,6 +136,5 @@ alias ontouchpad="sudo modprobe i2c_hid && $HOME/.scripts/touchscreen.sh 0" # Other alias mysql-workbench="GDK_SCALE=1 GDK_DPI_SCALE=1 mysql-workbench 1>/dev/null 2>&1 &; disown" alias weather="curl -s 'http://wttr.in/dordrecht?q&n&p' | head -n -3" -alias mpvshuffle='mpv "$(xclip -o)" --no-video --shuffle --ytdl-format="bestaudio[ext=m4a]"' [ -f "$ZDOTDIR/.zshrc_extended" ] && source "$ZDOTDIR/.zshrc_extended" diff --git a/.scripts/mpv.sh b/.scripts/mpv.sh new file mode 100755 index 0000000..a9be3ab --- /dev/null +++ b/.scripts/mpv.sh @@ -0,0 +1,110 @@ +#!/bin/sh + +PIPE="$XDG_CACHE_HOME/mpv/umpv_fifo" + +help() { + B=$(tput bold) + U=$(tput smul) + N=$(tput sgr0) + + cat << EOF +${B}NAME${N} + mpv.sh - mpv playback functions + +${B}SYNOPSIS${N} + ${B}./mpv.sh${N} [${U}OPTION${N}] [${U}ARGS${N}...] + +${B}DESCRIPTION${N} + mpv.sh is a script to manage different mpv viewing bahavior. + If no ${U}URLS${N} argument is provided, it is read from clipboard instead. + The queue option starts a mpv window with a playlist, which all videos of + subsequent queue calls get added to. + +${B}OPTIONS${N} + ${B}help${N} + Display usage message and exit. + + [${B}play${N}] [${U}URLS${N}...] (default) + Plays videos in a new mpv window. + + ${B}shuffle${N} [${U}URLS${N}...] + Shuffle audio playlist (disables video playback). + + ${B}queue${N} [${U}URLS${N}...] + Add multiple videos to the unique mpv's queue. +EOF +} + +CLIP="$(xclip -se c -o)" + +play() { + if [ -z "$1" ]; then + MPV="mpv $CLIP" + # Cut off everything after space + LINK=" $(echo $CLIP | sed -nE 's/^(\S+).*/\1/p')" + else + MPV="mpv $*" + # Determain which argument holds the urls + [ "$1" = "${1#-}" ] && DISPLAY="$1" || DISPLAY="$2" + # Cut off everything after space + LINK=" $(echo $DISPLAY | sed -nE 's/^(\S+).*/\1/p')" + fi + + notify-send -t 2500 "Loading video: $LINK" + # Attempt to load video + [ "$($MPV)" ] && notify-send -u critical -t 4000 "Loading video failed.." +} + +shuffle() { + # Skip first argument + shift 1 + mpv --no-video --shuffle --ytdl-format='bestaudio[ext=m4a]' "${@:-$CLIP}" +} + +queue() { + OPTIONS="--no-terminal --force-window --input-file=$PIPE" + + # Create mpv cache directory + DIR="$(dirname "$PIPE")" + [ ! -d "$DIR" ] && mkdir -p "$DIR" + + # Delete named pipe if no umpv is running + if ! pgrep -f 'mpv $OPTIONS' > /dev/null; then + rm -f "$PIPE" + fi + + # Skip first argument + shift 1 + # Set url to argument if provided, clipboard otherwise + URLS="${@:-$CLIP}" + + if [ -p $PIPE ]; then + notify-send -t 2500 "Added video to queue.." + # Add video to named pipe + echo "$URLS" \ + | awk -v RS=' ' '{ print "raw loadfile "$1" append" }' > "$PIPE" + else + # Create named pipe + mkfifo "$PIPE" + + # Play video + play "$OPTIONS" "$URLS" + + rm -f "$PIPE" + fi +} + +case "$1" in + help) + help + ;; + shuffle) + shuffle "$@" + ;; + queue) + queue "$@" + ;; + *) + play "$@" + ;; +esac