mirror of
https://github.com/ksyasuda/dotfiles.git
synced 2026-02-28 00:22:41 -08:00
udpate
This commit is contained in:
@@ -2,28 +2,82 @@
|
||||
|
||||
set -Eeuo pipefail
|
||||
|
||||
URL="${1:-$(wl-paste -p)}"
|
||||
MPV_SOCKET=/tmp/mpvsocket
|
||||
URL="${1:-}"
|
||||
MPV_SOCKET="/tmp/mpvsocket"
|
||||
ICON_PATH="$HOME/.local/share/icons/Magna-Glassy-Dark-Icons/apps/48/mpv.svg"
|
||||
TITLE="mpv-add.sh"
|
||||
|
||||
notify() {
|
||||
local message="$1"
|
||||
notify-send -i "$ICON_PATH" "$TITLE" "$message"
|
||||
}
|
||||
|
||||
trim() {
|
||||
printf '%s' "$1" | sed 's/^[[:space:]]\+//;s/[[:space:]]\+$//'
|
||||
}
|
||||
|
||||
play_direct() {
|
||||
mpv -- "$URL" &> /dev/null &
|
||||
}
|
||||
|
||||
wait_for_socket() {
|
||||
local timeout_ms=2000
|
||||
local waited=0
|
||||
while ((waited < timeout_ms)); do
|
||||
if [[ -S "$MPV_SOCKET" ]]; then
|
||||
return 0
|
||||
fi
|
||||
sleep 0.05
|
||||
waited=$((waited + 50))
|
||||
done
|
||||
return 1
|
||||
}
|
||||
|
||||
is_valid_input() {
|
||||
local input="$1"
|
||||
|
||||
if [[ -f "$input" ]]; then
|
||||
return 0
|
||||
fi
|
||||
|
||||
if ! command -v yt-dlp > /dev/null 2>&1; then
|
||||
return 1
|
||||
fi
|
||||
|
||||
if yt-dlp --simulate "$input" > /dev/null 2>&1; then
|
||||
return 0
|
||||
fi
|
||||
|
||||
return 1
|
||||
}
|
||||
|
||||
if [[ -z "$URL" ]] && command -v wl-paste > /dev/null 2>&1; then
|
||||
URL="$(wl-paste -p || true)"
|
||||
fi
|
||||
|
||||
URL="$(trim "$URL")"
|
||||
|
||||
if [[ -z "$URL" ]]; then
|
||||
notify-send -i "$ICON_PATH" "$TITLE" "No URL provided"
|
||||
notify "No URL provided"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! [[ -f "$URL" ]] && ! yt-dlp --simulate "$URL"; then
|
||||
notify-send -i "$ICON_PATH" "$TITLE" "Invalid URL"
|
||||
if ! is_valid_input "$URL"; then
|
||||
notify "Invalid input: provide a local file path or a yt-dlp supported URL"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! pgrep -x mpv &> /dev/null; then
|
||||
mpv "$URL" &> /dev/null &
|
||||
notify-send -i "$ICON_PATH" "$TITLE" "Playing $URL"
|
||||
rm -f "$MPV_SOCKET"
|
||||
mpv --input-ipc-server="$MPV_SOCKET" -- "$URL" &> /dev/null &
|
||||
notify "Playing $URL"
|
||||
else
|
||||
if echo "{ \"command\": [\"script-message\", \"add_to_queue\", \"$URL\" ] }" | socat - "$MPV_SOCKET" &> /dev/null; then
|
||||
notify-send -i "$ICON_PATH" "$TITLE" "Added $URL to queue"
|
||||
else
|
||||
notify-send -i "$ICON_PATH" "$TITLE" "Failed to add $URL to queue"
|
||||
if [[ -S "$MPV_SOCKET" ]] && wait_for_socket && echo "{ \"command\": [\"script-message\", \"add_to_queue\", \"$URL\" ] }" | socat - "$MPV_SOCKET" &> /dev/null; then
|
||||
notify "Added $URL to queue"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
notify "Queue unavailable, opening in new player"
|
||||
play_direct
|
||||
notify "Playing $URL"
|
||||
fi
|
||||
|
||||
@@ -4,6 +4,7 @@ THEME="${THEME:-$HOME/.local/share/SubMiner/themes/subminer.rasi}"
|
||||
FONTCONFIG_FILE=$HOME/.config/mpv/mpv-fonts.conf
|
||||
COMMAND=mpv
|
||||
VIDEO_EXTENSIONS="mkv|mp4|avi|webm|mov|flv|wmv|m4v|ts|m2ts"
|
||||
TARGET_DIRECTORY=""
|
||||
|
||||
# Parse command-line options first
|
||||
while getopts ":st:" opt; do
|
||||
@@ -26,6 +27,19 @@ while getopts ":st:" opt; do
|
||||
done
|
||||
shift $((OPTIND - 1))
|
||||
|
||||
if [[ $# -gt 1 ]]; then
|
||||
echo "Usage: $0 [-s] [-t theme] [directory]" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [[ $# -eq 1 ]]; then
|
||||
if [[ ! -d "$1" ]]; then
|
||||
echo "Invalid directory: $1" >&2
|
||||
exit 1
|
||||
fi
|
||||
TARGET_DIRECTORY="$(realpath "$1")"
|
||||
fi
|
||||
|
||||
find_videos() {
|
||||
find "$PWD" -maxdepth 1 -type f -regextype posix-extended \
|
||||
-iregex ".*\.($VIDEO_EXTENSIONS)$" 2> /dev/null | sort -V
|
||||
@@ -40,6 +54,60 @@ build_rofi_menu() {
|
||||
done < <(find_videos)
|
||||
}
|
||||
|
||||
is_video_file() {
|
||||
local file="$1"
|
||||
local regex="\\.($VIDEO_EXTENSIONS)$"
|
||||
|
||||
if [[ ! "${file,,}" =~ $regex ]]; then
|
||||
return 1
|
||||
fi
|
||||
|
||||
if command -v file > /dev/null 2>&1; then
|
||||
local mime
|
||||
mime=$(file -b --mime-type "$file" 2>/dev/null || true)
|
||||
[[ "$mime" == video/* ]] && return 0
|
||||
fi
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
select_video_via_rofi() {
|
||||
if [[ -n "$TARGET_DIRECTORY" ]]; then
|
||||
local selection=""
|
||||
local prompt="Choose Video "
|
||||
while true; do
|
||||
selection=$(rofi -show filebrowser -show-icons -theme "$THEME" \
|
||||
-theme-str 'configuration {font: "JetBrainsMono Nerd Font 10";} listview {columns: 1; lines: 15;} window {width: 88%;}' \
|
||||
-p "$prompt" \
|
||||
-filebrowser-directory "$TARGET_DIRECTORY" \
|
||||
-filebrowser-cancel-returns-1 true)
|
||||
|
||||
[[ -z "$selection" ]] && return 1
|
||||
|
||||
if [[ "$selection" != /* ]]; then
|
||||
selection="$TARGET_DIRECTORY/$selection"
|
||||
fi
|
||||
|
||||
if [[ -d "$selection" ]]; then
|
||||
prompt="Choose Video (folders are not selectable)"
|
||||
continue
|
||||
fi
|
||||
|
||||
if is_video_file "$selection"; then
|
||||
echo "$selection"
|
||||
return 0
|
||||
fi
|
||||
|
||||
prompt="Choose Video (select a video file)"
|
||||
done
|
||||
fi
|
||||
|
||||
build_rofi_menu | rofi -dmenu -i -show-icons -theme "$THEME" \
|
||||
-theme-str 'configuration {font: "JetBrainsMono Nerd Font 10";} listview {columns: 1; lines: 15;} window {width: 88%;}' -p "Choose Video "
|
||||
}
|
||||
|
||||
selection=$(select_video_via_rofi)
|
||||
|
||||
get_video_thumbnail() {
|
||||
local video="$1"
|
||||
local thumb_dir="$HOME/.cache/thumbnails/large"
|
||||
@@ -60,15 +128,24 @@ get_video_thumbnail() {
|
||||
fi
|
||||
}
|
||||
|
||||
selection=$(build_rofi_menu | rofi -dmenu -i -show-icons -theme "$THEME" \
|
||||
-theme-str 'configuration {font: "JetBrainsMono Nerd Font 10";} listview {columns: 1; lines: 15;} window {width: 88%;}' -p "Choose Video ")
|
||||
|
||||
if [[ -z "$selection" ]]; then
|
||||
echo "No video selected."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
choice="./$selection"
|
||||
if [[ -n "$TARGET_DIRECTORY" ]]; then
|
||||
choice="$selection"
|
||||
if [[ "$choice" != /* ]]; then
|
||||
choice="$TARGET_DIRECTORY/$choice"
|
||||
fi
|
||||
else
|
||||
choice="./$selection"
|
||||
fi
|
||||
|
||||
if [[ ! -f "$choice" ]]; then
|
||||
echo "Selected item is not a valid file: $choice" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
THUMBNAIL_PATH=$(get_video_thumbnail "$choice")
|
||||
if [[ -n "$THUMBNAIL_PATH" && -f "$THUMBNAIL_PATH" ]]; then
|
||||
|
||||
Reference in New Issue
Block a user