PLAY NU MPV Anonymous https://bbs.gikopoi.com/atom/thread/1758939734 2025-09-27T03:37:09+00:00 PLAY NU MPV https://bbs.gikopoi.com/post/1758939734/1 2025-09-27T02:22:14+00:00 2025-09-27T02:22:14+00:00 https://x0.at/30jh.png<br>#!/bin/bash<br># ┏━┓╻ ┏━┓╻ ╻┏┓╻╻ ╻┏┳┓┏━┓╻ ╻ USAGE: playnumpv <days><br># ┣━┛┃ ┣━┫┗┳┛┃┗┫┃ ┃┃┃┃┣━┛┃┏┛ (where days = number of days to <br># ╹ ┗━╸╹ ╹ ╹ ╹ ╹┗━┛╹ ╹╹ ┗┛ search back for newer media files)<br><br># Check if argument is provided<br>if [ -z "$1" ] || [[ ! "$1" =~ ^[0-9]+$ ]]; then<br> echo "Usage: $0 <days>"<br> exit 1<br>fi<br><br># Create a temporary playlist file in a safe location<br>playlist=$(mktemp) || { echo "Failed to create temporary file"; exit 1; }<br><br># Set trap to clean up temporary file on exit<br>trap 'rm -f "$playlist"' EXIT<br><br># Get absolute path of the playlist to exclude it from search<br>abs_playlist=$(realpath "$playlist")<br><br># Find all media files modified in the last $1 days and add them to the playlist<br># Using absolute paths to avoid issues with relative paths<br>find "$PWD" -type f -regextype posix-extended -regex '.*\.(mp3|flac|opus|ogg|m4a|wav|nsf|mod|xm)' \<br> -mtime -"${1}" ! -path "$abs_playlist" -print0 | \<br> sort -z | \<br> tr '\0' '\n' > "$playlist"<br><br># Check if any files were found<br>if [ ! -s "$playlist" ]; then<br> echo "No files found newer than ${1} days."; exit 0<br>fi<br><br># Count the number of files in the playlist<br>file_count=$(wc -l < "$playlist")<br>echo "Found $file_count files to play."<br><br># Play playlist with mpv using unbuffer to maintain terminal connection<br># and process substitution to capture output without breaking stdin<br>unbuffer -p mpv --shuffle --term-osd=auto --osd-level=2 --no-audio-display \<br> --playlist="$playlist" 2>&1 > >(grep -E "Playing") < /dev/tty<br> explanation console stdin, stdout, redirection https://bbs.gikopoi.com/post/1758939734/2 2025-09-27T03:37:09+00:00 2025-09-27T03:37:09+00:00 #!/bin/bash<br># ┏━┓╻ ┏━┓╻ ╻┏┓╻╻ ╻┏┳┓┏━┓╻ ╻ USAGE: playnumpv <days><br># ┣━┛┃ ┣━┫┗┳┛┃┗┫┃ ┃┃┃┃┣━┛┃┏┛ (where days = number of days to <br># ╹ ┗━╸╹ ╹ ╹ ╹ ╹┗━┛╹ ╹╹ ┗┛ search back for newer media files)<br><br># Check if argument is provided<br>if [ -z "$1" ] || [[ ! "$1" =~ ^[0-9]+$ ]]; then<br> echo "Usage: $0 <days>"<br> exit 1<br>fi<br><br># Create a temporary playlist file in a safe location<br>playlist=$(mktemp) || { echo "Failed to create temporary file"; exit 1; }<br><br># Set trap to clean up temporary file on exit<br>trap 'rm -f "$playlist"' EXIT<br><br># Get absolute path of the playlist to exclude it from search<br>abs_playlist=$(realpath "$playlist")<br><br># Find all media files modified in the last $1 days and add them to the playlist<br># Using absolute paths to avoid issues with relative paths<br>find "$PWD" -type f -regextype posix-extended -regex '.*\.(mp3|flac|opus|ogg|m4a|wav|nsf|mod|xm)' \<br> -mtime -"${1}" ! -path "$abs_playlist" -print0 | \<br> sort -z | \<br> tr '\0' '\n' > "$playlist"<br><br># Check if any files were found<br>if [ ! -s "$playlist" ]; then<br> echo "No files found newer than ${1} days."<br> exit 0<br>fi<br><br># Count the number of files in the playlist<br>file_count=$(wc -l < "$playlist")<br>echo "Found $file_count files to play."<br><br># Play the playlist with mpv, using unbuffer to maintain terminal connection<br># and process substitution to capture output without breaking stdin<br>unbuffer -p mpv --shuffle --term-osd=auto --osd-level=2 --no-audio-display \<br> --playlist="$playlist" 2>&1 > >(grep -E "Playing") < /dev/tty<br><br># Multi-line comment explaining the redirection technique:<br>: <<'EOF'<br>EXPLANATION OF THE REDIRECTION TECHNIQUE:<br><br>The command uses several advanced shell features to maintain keyboard control while filtering output:<br><br>1. `unbuffer -p`:<br> - Creates a pseudo-terminal for mpv<br> - Ensures mpv maintains interactive behavior including keyboard input handling<br><br>2. `2>&1`:<br> - Redirects stderr to stdout<br> - Combines both output streams<br><br>3. `> >(grep -E "Playing")`:<br> - Uses process substitution (the `>(...)` syntax)<br> - Creates a temporary file descriptor connected to grep<br> - Sends mpv's output to grep without breaking terminal connection<br> - Unlike a regular pipe, this doesn't disrupt keyboard input<br><br>4. `< /dev/tty`:<br> - Explicitly redirects stdin to the terminal device<br> - Ensures mpv gets keyboard input directly from the terminal<br> - This is critical for maintaining keyboard control<br><br>WHY THIS WORKS WHEN REGULAR PIPES FAIL:<br><br>Regular pipes (mpv | grep) break keyboard input because:<br>- The shell puts commands in separate process groups<br>- mpv detects its output is going to a pipe and may change behavior<br>- Terminal control gets disrupted<br><br>Our solution works because:<br>- unbuffer maintains the terminal connection<br>- Process substitution doesn't break the terminal connection like a pipe<br>- Explicit terminal input redirection ensures keyboard control<br>- The combination preserves mpv's interactive capabilities<br><br>DATA FLOW:<br>Keyboard Input → /dev/tty → mpv (via unbuffer) → Process Substitution → grep → Terminal<br>EOF<br>