Skip to content

Instantly share code, notes, and snippets.

@queertypes
Last active March 6, 2018 23:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save queertypes/587ed35a16c7253d027547cea882bbea to your computer and use it in GitHub Desktop.
Save queertypes/587ed35a16c7253d027547cea882bbea to your computer and use it in GitHub Desktop.
Read a playlist from a file using bash, while keeping input-priority for player
#!/bin/bash
# Read the playlist in a line at-a-time from the 4th file descriptor.
# This preserves stdin for mpv below, so mpv can still receive
# keyboard commands.
while read -r -u 4 songPath; do
duration=$(
ffmpeg -i "$songPath" |& ## use ffmpeg in info mode
grep Duration | ## use `cut` to parse the song duration
cut -d, -f 1 | ## "Duration: 00:02:21.234"
cut -d: -f 3,4 | ## -> "02:21.234"
cut -d. -f 1); ## assumes no song is longer than 59 minutes.
# Issue a notification to the desktop with the song path and duration.
notify-send "Now playing: $songPath - $duration";
# Play the song. `mpv` can receive most keyboard commands, except
# for those involving the shift key for some reason.
mpv --no-video $songPath;
# final step: redirect the contents of the playlist into file
# descriptor 4 so that `read` above can receive it. The playlist file
# must be titled "playlist" and must reside in the current working
# directory.
done 4< playlist
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment