-
-
Save zipang/c1fd6b06a172081f537be506b9fbf37d to your computer and use it in GitHub Desktop.
Play music files inside a dir (Linux + 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 | |
# Name of the playlist file to generate/use | |
playlist="playlist.m3u" | |
# Check if the playlist file doesn't exist | |
if [ ! -f "$playlist" ]; then | |
# Enable nullglob to handle patterns with no matches | |
shopt -s nullglob | |
# Collect all music files with common extensions | |
# Add or remove extensions as needed for your use case | |
music_files=(*.mp3 *.m4a *.aac *.flac) | |
# Check if we found any music files | |
if [ ${#music_files[@]} -eq 0 ]; then | |
echo "Error: No playlist and no music files found." | |
exit 1 | |
fi | |
# Sort files case-insensitively and create playlist | |
# - Use 'sort -f' for case-insensitive sorting | |
# - 'printf %s\n' ensures proper handling of filenames with spaces | |
printf "%s\n" "${music_files[@]}" | sort -f > "$playlist" | |
echo "Created new playlist with ${#music_files[@]} tracks." | |
fi | |
# Check if playlist exists but is empty | |
if [ ! -s "$playlist" ]; then | |
echo "Error: Playlist file exists but is empty." | |
echo " Delete it to regenerate or add entries manually." | |
exit 2 | |
fi | |
# Launch mpv player with the playlist | |
echo "Starting playback..." | |
exec mpv --playlist="$playlist" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment