Created
May 3, 2025 16:15
-
-
Save kabirnayeem99/e01a2e8742b07c2b1476c30559f02f6a to your computer and use it in GitHub Desktop.
Minimalistic MPV Audio Player
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
command_exists() { | |
command -v "$1" &>/dev/null | |
} | |
error_exit() { | |
gum style --foreground 1 "$1" | |
exit 1 | |
} | |
validate_url() { | |
[[ "$1" =~ ^https?:// ]] && return 0 || return 1 | |
} | |
get_audio_url() { | |
local url=$(pbpaste) | |
if ! validate_url "$url"; then | |
gum style --foreground 3 "⚠️ Invalid URL detected. Please provide a valid audio URL." | |
url=$(gum input --placeholder "Enter a valid audio URL or path" --prompt "🎵 URL > ") | |
if ! validate_url "$url"; then | |
error_exit "⚠️ Invalid URL again. Exiting." | |
fi | |
fi | |
echo "$url" | |
} | |
stop_existing_mpv() { | |
if pgrep -x mpv >/dev/null; then | |
local action=$(gum choose "Stop current mpv" "Cancel and exit") | |
if [[ "$action" == "Stop current mpv" ]]; then | |
gum spin --title "Stopping existing mpv process..." -- sleep 1 | |
pkill -9 -x mpv | |
gum style --foreground 2 "✔️ Stopped existing mpv." | |
else | |
gum style --foreground 3 "⚠️ Exiting. No action taken." | |
exit 1 | |
fi | |
fi | |
} | |
get_audio_title() { | |
mpv --no-video --really-quiet --stats-for-chapters --script-opts=ontop=no -- "$1" | awk -F 'Title: ' '{print $2}' | sed 's/\r//g' | xargs | |
} | |
start_mpv() { | |
gum spin --title "🎧 Starting mpv..." -- nohup mpv --no-video --no-loop-playlist "$1" >/dev/null 2>&1 & | |
} | |
display_title() { | |
gum style \ | |
--foreground 212 --border-foreground 212 --border double \ | |
--align center --width 50 --margin "1 2" --padding "2 4" \ | |
"$1" "Enjoy your audio!" | |
} | |
main() { | |
command_exists gum || error_exit "❌ gum is not installed. Install it with: brew install gum" | |
command_exists mpv || error_exit "❌ mpv is not installed. Please install mpv first." | |
local audio_url=$(get_audio_url) | |
stop_existing_mpv | |
local title=$(get_audio_title "$audio_url") | |
start_mpv "$audio_url" | |
display_title "$title" | |
gum style --foreground 2 "✔️ Audio is now playing!" | |
} | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment