From d32f79f406a323c60fca00939a10f3b194fa17d3 Mon Sep 17 00:00:00 2001 From: sudacode Date: Sun, 1 Feb 2026 23:09:36 -0800 Subject: [PATCH] update --- .config/mpv/scripts/run_websocket_server.lua | 109 ++++++++++++------- projects/scripts/rmpv | 105 +++++++----------- 2 files changed, 108 insertions(+), 106 deletions(-) diff --git a/.config/mpv/scripts/run_websocket_server.lua b/.config/mpv/scripts/run_websocket_server.lua index 840fe25..c547794 100644 --- a/.config/mpv/scripts/run_websocket_server.lua +++ b/.config/mpv/scripts/run_websocket_server.lua @@ -1,6 +1,8 @@ -- mpv_websocket -- https://github.com/kuroahna/mpv_websocket +local use_secondary_subs = false + local utils = require("mp.utils") local platform = mp.get_property_native("platform") @@ -8,76 +10,101 @@ local platform = mp.get_property_native("platform") local config_file_path = mp.find_config_file("mpv.conf") local config_folder_path, config_file = utils.split_path(config_file_path) local mpv_websocket_path = - utils.join_path(config_folder_path, platform == "windows" and "mpv_websocket.exe" or "mpv_websocket") + utils.join_path(config_folder_path, platform == "windows" and "mpv_websocket.exe" or "mpv_websocket") local initialised_websocket local _, err = utils.file_info(config_file_path) if err then - error("failed to open mpv config file `" .. config_file_path .. "`") + error("failed to open mpv config file `" .. config_file_path .. "`") end local _, err = utils.file_info(mpv_websocket_path) if err then - error("failed to open mpv_websocket") + error("failed to open mpv_websocket") end local function find_mpv_socket(config_file_path) - local file = io.open(config_file_path, "r") - if file == nil then - error("failed to read mpv config file `" .. config_file_path .. "`") - end + -- First, try to get from mpv properties (includes command-line options) + local mpv_socket = mp.get_property("input-ipc-server") + if mpv_socket and mpv_socket ~= "" then + return mpv_socket + end - local mpv_socket - for line in file:lines() do - mpv_socket = line:match("^input%-ipc%-server%s*=%s*(%g+)%s*") - if mpv_socket then - break - end - end + -- Fall back to reading config file + local file = io.open(config_file_path, "r") + if file == nil then + error("failed to read mpv config file `" .. config_file_path .. "`") + end - file:close() + for line in file:lines() do + mpv_socket = line:match("^input%-ipc%-server%s*=%s*(%g+)%s*") + if mpv_socket then + break + end + end - if not mpv_socket then - error("input-ipc-server option does not exist in `" .. config_file_path .. "`") - end + file:close() - return mpv_socket + if not mpv_socket then + error("input-ipc-server option does not exist in `" .. config_file_path .. "`") + end + + return mpv_socket end local mpv_socket = find_mpv_socket(config_file_path) if platform == "windows" then - mpv_socket = "\\\\.\\pipe" .. mpv_socket:gsub("/", "\\") + mpv_socket = "\\\\.\\pipe" .. mpv_socket:gsub("/", "\\") end local function start_websocket() - initialised_websocket = mp.command_native_async({ - name = "subprocess", - playback_only = false, - capture_stdout = true, - capture_stderr = true, - args = { - mpv_websocket_path, - "-m", - mpv_socket, - "-w", - "6677", - }, - }) + local args = { + mpv_websocket_path, + "-m", + mpv_socket, + "-w", + "6677", + } + + if use_secondary_subs then + table.insert(args, "-s") + end + + initialised_websocket = mp.command_native_async({ + name = "subprocess", + playback_only = false, + capture_stdout = true, + capture_stderr = true, + args = args, + }) end local function end_websocket() - mp.abort_async_command(initialised_websocket) - initialised_websocket = nil + mp.abort_async_command(initialised_websocket) + initialised_websocket = nil end local function toggle_websocket() - local paused = mp.get_property_bool("pause") - if initialised_websocket and paused then - end_websocket() - elseif not initialised_websocket and not paused then - start_websocket() - end + local paused = mp.get_property_bool("pause") + if initialised_websocket and paused then + end_websocket() + elseif not initialised_websocket and not paused then + start_websocket() + end +end + +local function toggle_subs_type() + if use_secondary_subs then + use_secondary_subs = false + else + use_secondary_subs = true + end + if initialised_websocket then + end_websocket() + start_websocket() + end end mp.register_script_message("togglewebsocket", toggle_websocket) +mp.register_script_message("togglesubstype", toggle_subs_type) start_websocket() diff --git a/projects/scripts/rmpv b/projects/scripts/rmpv index 13e5221..48d8815 100755 --- a/projects/scripts/rmpv +++ b/projects/scripts/rmpv @@ -1,9 +1,9 @@ #!/usr/bin/env bash -THEME="${THEME:-$HOME/.config/rofi/launchers/type-2/style-2.rasi}" -THUMBNAIL_PATH="/tmp/rmpv-thumbnail.jpg" +THEME="${THEME:-/opt/mpv-yomitan/catppuccin-macchiato.rasi}" FONTCONFIG_FILE=$HOME/.config/mpv/mpv-fonts.conf COMMAND=mpv +VIDEO_EXTENSIONS="mkv|mp4|avi|webm|mov|flv|wmv|m4v|ts|m2ts" # Parse command-line options first while getopts ":it:" opt; do @@ -26,81 +26,56 @@ while getopts ":it:" opt; do done shift $((OPTIND - 1)) -generate_thumbnail() { - local video_file="$1" - local temp_thumb="/tmp/rmpv-thumbnail-$$.jpg" - local thumbnail_file="${video_file%.*}.jpg" +find_videos() { + find "$PWD" -maxdepth 1 -type f -regextype posix-extended \ + -iregex ".*\.($VIDEO_EXTENSIONS)$" 2>/dev/null | sort -V +} - # Clean up previous thumbnail - rm -f "$THUMBNAIL_PATH" +build_rofi_menu() { + while IFS= read -r video; do + [ -z "$video" ] && continue + local display_name + display_name=$(basename "$video") + printf '%s\0icon\x1fthumbnail://%s\n' "$display_name" "$video" + done < <(find_videos) +} - # Validate input - if [[ -z "$video_file" ]]; then - echo "Error: No video file specified" >&2 - return 1 +get_video_thumbnail() { + local video="$1" + local thumb_dir="$HOME/.cache/thumbnails/large" + local video_uri="file://$(realpath "$video")" + local thumb_hash=$(echo -n "$video_uri" | md5sum | cut -d' ' -f1) + local thumb_path="$thumb_dir/$thumb_hash.png" + + if [[ -f "$thumb_path" ]]; then + echo "$thumb_path" + return 0 fi - if [[ ! -f "$video_file" ]]; then - echo "Error: Video file '$video_file' not found" >&2 - return 1 - fi - - # Check if it's a video file - if ! file "$video_file" | grep -qE "(video|Video)"; then - echo "Error: '$video_file' doesn't appear to be a video file" >&2 - return 1 - fi - - # Generate thumbnail if it doesn't exist - if [[ ! -f "$thumbnail_file" ]]; then - echo "Generating thumbnail for $(basename "$video_file")..." - # Try generating thumbnail side-by-side - if ! ffmpeg -ss 00:00:01 -i "$video_file" \ - -vf "scale=320:240:force_original_aspect_ratio=decrease,pad=320:240:(ow-iw)/2:(oh-ih)/2" \ - -frames:v 1 \ - -q:v 4 \ - "$thumbnail_file" \ - -loglevel error -y 2>/dev/null; then - - # Fallback to temp file if side-by-side fails (e.g. read-only fs) - echo "Warning: Failed to write to $thumbnail_file, trying temp location" >&2 - thumbnail_file="$temp_thumb" - - if ! ffmpeg -ss 00:00:01 -i "$video_file" \ - -vf "scale=320:240:force_original_aspect_ratio=decrease,pad=320:240:(ow-iw)/2:(oh-ih)/2" \ - -frames:v 1 \ - -q:v 4 \ - "$thumbnail_file" \ - -loglevel error -y 2>/dev/null; then - echo "Error: Failed to generate thumbnail" >&2 - return 1 - fi - fi - fi - - # Copy to consistent location for notify-send - # We use a fixed path so notify-send always finds it - if cp "$thumbnail_file" "$THUMBNAIL_PATH" 2>/dev/null; then - echo "Thumbnail ready at: $THUMBNAIL_PATH" - ls -l "$THUMBNAIL_PATH" - file "$THUMBNAIL_PATH" - else - echo "Error: Failed to copy thumbnail to $THUMBNAIL_PATH" >&2 + local tmp_thumb="/tmp/rmpv-thumb-$$.jpg" + if command -v ffmpegthumbnailer &>/dev/null; then + ffmpegthumbnailer -i "$video" -o "$tmp_thumb" -s 512 -q 5 2>/dev/null && echo "$tmp_thumb" + elif command -v ffmpeg &>/dev/null; then + ffmpeg -i "$video" -ss 00:00:30 -vframes 1 -vf "scale=512:-1" "$tmp_thumb" 2>/dev/null && echo "$tmp_thumb" fi } -choice="$(find . -iname "*[.mkv|.mp4]" | sort -h | rofi -dmenu -i -theme "$THEME" -theme-str 'listview {columns: 1; lines: 15;} window {width: 88%;}' -p "Choose Video")" -if [[ -z "$choice" ]]; then +selection=$(build_rofi_menu | rofi -dmenu -i -show-icons -theme "$THEME" \ + -theme-str 'listview {columns: 1; lines: 15;} window {width: 88%;}' -p "Choose Video ") + +if [[ -z "$selection" ]]; then echo "No video selected." exit 1 fi -generate_thumbnail "$choice" -if [[ ! -f "$THUMBNAIL_PATH" ]]; then - echo "Warning: Thumbnail not created, notification will have no icon" >&2 +choice="./$selection" + +THUMBNAIL_PATH=$(get_video_thumbnail "$choice") +if [[ -n "$THUMBNAIL_PATH" && -f "$THUMBNAIL_PATH" ]]; then + notify-send -i "$THUMBNAIL_PATH" "Playing Video" "$(basename "$choice")" +else + notify-send "Playing Video" "$(basename "$choice")" fi -notify-send -i "$THUMBNAIL_PATH" "Playing Video" "$(basename "$choice")" $COMMAND "$choice" & # vim: ft=sh -