Last active
November 27, 2024 15:18
-
-
Save shawnyeager/ba153c9d1f18d17b37dc299ea9b0bbf2 to your computer and use it in GitHub Desktop.
quick & dirty script to take command line input, search youtube with ytfzf, and play audio with mpv
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
#!/bin/bash | |
# Check if a search string is provided as a command-line argument | |
SEARCH_STRING="$*" | |
# Function to handle playback | |
play_content() { | |
# Use `ytfzf` to fetch the video URL | |
VIDEO_URL=$(ytfzf -L "$SEARCH_STRING" 2>/dev/null) | |
# Handle errors during search | |
if [[ $? -ne 0 || -z "$VIDEO_URL" ]]; then | |
echo "Error: No results found for '$SEARCH_STRING'. Please try another search." | |
return 1 | |
fi | |
# Play the video with `mpv` | |
echo "Playing: $VIDEO_URL" | |
mpv --volume=60 --no-video "$VIDEO_URL" | |
# Handle errors during playback | |
if [[ $? -ne 0 ]]; then | |
echo "Error: Playback failed." | |
return 1 | |
fi | |
return 0 | |
} | |
# Main loop | |
while true; do | |
# Prompt for input only if no search string is set | |
if [[ -z "$SEARCH_STRING" ]]; then | |
echo -n "Enter search string (or press Enter to exit): " | |
read -r SEARCH_STRING | |
# Exit if no input is provided | |
if [[ -z "$SEARCH_STRING" ]]; then | |
echo "Goodbye!" | |
exit 0 | |
fi | |
fi | |
# Ensure dependencies are installed | |
if ! command -v ytfzf &> /dev/null; then | |
echo "Error: 'ytfzf' is not installed. Please install it and try again." | |
exit 1 | |
fi | |
if ! command -v mpv &> /dev/null; then | |
echo "Error: 'mpv' is not installed. Please install it and try again." | |
exit 1 | |
fi | |
if ! command -v yt-dlp &> /dev/null; then | |
echo "Error: 'yt-dlp' is not installed. Please install it and try again." | |
exit 1 | |
fi | |
# Play content and reset search string for next prompt | |
play_content | |
SEARCH_STRING="" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment