cover-music-generate.sh 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/usr/bin/env bash
  2. set -euo pipefail # bash strict mode
  3. ## !/bin/sh
  4. #-------------------------------#
  5. # Generate current song cover #
  6. # ffmpeg version #
  7. #-------------------------------#
  8. #pkill -RTMIN+12 dwmblocks
  9. MUSIC_DIR="$(awk '/music_directory/ {print $2}' ~/.config/mpd/mpd.conf)"
  10. file="$1"
  11. COVER="/tmp/cover.png"
  12. COVER_SIZE="${2:-250}"
  13. BORDERS=false
  14. BORDER_WIDTH=5
  15. BORDER_COLOR="white"
  16. ffmpeg_cover() {
  17. if $BORDERS; then
  18. ffmpeg -loglevel 0 -y -i "$1" -vf "scale=$COVER_SIZE:-1,pad=$COVER_SIZE+$BORDER_WIDTH:ow:(ow-iw)/2:(oh-ih)/2:$BORDER_COLOR" "$COVER"
  19. else
  20. ffmpeg -loglevel 0 -y -i "$1" -vf "scale=$COVER_SIZE:-1" "$COVER"
  21. fi
  22. # full resolution cover
  23. ffmpeg -loglevel 0 -y -i "$1" "/tmp/cover_full_res.png"
  24. }
  25. fallback_find_cover() {
  26. album="${file%/*}"
  27. album_cover="$(find "$album" -type d -exec find {} -maxdepth 1 -type f -iregex ".*\(cover?s\|folder?s\|artwork?s\|front?s\|scan?s\).*[.]\(jpe?g\|png\|gif\|bmp\)" \;)"
  28. if [ "$album_cover" == "" ]; then
  29. album_cover="$(find "$album" -type d -exec find {} -maxdepth 1 -type f -iregex ".*[.]\(jpe?g\|png\|gif\|bmp\)" \;)"
  30. fi
  31. if [ "$album_cover" == "" ]; then
  32. album_cover="$(find "$album/.." -type d -exec find {} -maxdepth 1 -type f -iregex ".*\(cover?s\|folder?s\|artwork?s\|front?s\|scan?s\|booklet\).*?1[.]\(jpe?g\|png\|gif\|bmp\)" \;)"
  33. fi
  34. album_cover="$(echo -n "$album_cover" | head -n1)"
  35. }
  36. {
  37. # # file="$(audtool --current-song-filename)"
  38. # if [ $(pidof cmus) ]; then
  39. # file="$(cmus-remote -Q | awk '$1=="file" {$1="";print substr($0,2)}')"
  40. # elif [ $(pidof mocp) ]; then
  41. # file="$(mocp -Q %file)"
  42. # elif [ $(pidof mpd)]; then
  43. # file="${MUSIC_DIR}/$(mpc --format %file% current)"
  44. # fi
  45. if [ -n "$file" ] ; then
  46. if ffmpeg_cover "$file"; then
  47. exit
  48. else
  49. fallback_find_cover
  50. ffmpeg_cover "$album_cover"
  51. fi
  52. fi
  53. } &