Switched to mpd native albumart command

- Removed `http_base_url` and `http_cover_filenames` from config
- removed `_getAlbumArtFromFile` and `_getAlbumArtFromHttpServer` functions
- The `getAlbumArt` function now fetches the album art using `MPDClient.albumart`
- Removed unused imports.
This commit is contained in:
Erica
2022-06-09 00:57:09 +00:00
parent 86a7d7f52c
commit db7baa44f2
2 changed files with 12 additions and 73 deletions

View File

@ -1,10 +1,7 @@
#!/bin/python #!/bin/python
import curses import curses
import os import os
import posixpath from mpd import MPDClient, CommandError
import requests
from mpd import MPDClient
import ffmpeg
import pixcat import pixcat
import time import time
import configparser import configparser
@ -29,9 +26,7 @@ if "player" not in config.sections():
} }
if "art" not in config.sections(): if "art" not in config.sections():
config["art"] = {"music_directory": "~/Music", config["art"] = {"image_method": "pixcat"}
"image_method": "pixcat",
}
if "mpd" not in config.sections(): if "mpd" not in config.sections():
config["mpd"] = {"host": "localhost", config["mpd"] = {"host": "localhost",
@ -209,9 +204,9 @@ class Player:
i += 1 i += 1
# Init pairs # Init pairs
curses.init_pair(1, color_array[0], -1) # accent color curses.init_pair(1, color_array[0], -1) # accent color
curses.init_pair(2, color_array[1], -1) # time color curses.init_pair(2, color_array[1], -1) # time color
curses.init_pair(3, color_array[2], -1) # bar color curses.init_pair(3, color_array[2], -1) # bar color
# Get progress bar characters # Get progress bar characters
self.bar_body = theme_config.get("bar_body", "─") self.bar_body = theme_config.get("bar_body", "─")
@ -241,11 +236,6 @@ class Player:
else: else:
self.repeat = 0 self.repeat = 0
# Album art HTTP server
if art_config.get("http_base_url"):
self.art_http_session = requests.Session()
# Album art only flag # Album art only flag
self.album_art_only = player_config.getboolean("album_art_only", False) self.album_art_only = player_config.getboolean("album_art_only", False)
@ -512,67 +502,19 @@ class Player:
self.screen_height, self.screen_width = window_height, window_width self.screen_height, self.screen_width = window_height, window_width
def getAlbumArt(self, song_file): def getAlbumArt(self, song_file):
""" """
A function that fetches the album art and saves A function that fetches the album art and saves
it to self.album_art_loc it to self.album_art_loc
""" """
http_base_url = art_config.get("http_base_url")
if http_base_url:
self._getAlbumArtFromHttpServer(http_base_url, song_file)
else:
self._getAlbumArtFromFile(song_file)
def _getAlbumArtFromHttpServer(self, base_url, song_file):
"""
A function that fetches the album art from the configured
HTTP server, and saves it to self.album_art_loc
"""
album = os.path.dirname(song_file)
for cover_filename in art_config.get("http_cover_filenames", "cover.jpg").split():
album_art_url = posixpath.join(base_url, album, cover_filename)
try:
album_art_resp = self.art_http_session.get(album_art_url)
except requests.RequestException:
# If any exception occurs, simply give up and show default art.
self.drawDefaultAlbumArt()
break
if album_art_resp.ok:
with open(self.album_art_loc, "wb") as f:
f.write(album_art_resp.content)
break
elif album_art_resp.status_code == 404:
continue
else:
self.drawDefaultAlbumArt()
def _getAlbumArtFromFile(self, song_file):
"""
A function that extracts the album art from song_file and
saves it to self.album_art_loc
"""
music_dir = os.path.expanduser(
art_config.get("music_directory", "~/Music"))
song_file_abs = os.path.join(music_dir, song_file)
process = (
ffmpeg
.input(song_file_abs)
.output(self.album_art_loc)
)
try: try:
process.run(quiet=True, overwrite_output=True) albumart_data = self.client.albumart(song_file)
except ffmpeg._run.Error:
with open(self.album_art_loc, "wb") as f:
f.write(albumart_data["binary"])
except CommandError:
self.drawDefaultAlbumArt() self.drawDefaultAlbumArt()

View File

@ -8,9 +8,6 @@ show_playlist = true
[art] [art]
image_method = pixcat image_method = pixcat
music_directory = ~/Music
# http_base_url = http://localhost:6667/cover-art
# http_cover_filenames = cover.jpg cover.png folder.jpg folder.png art.jpg art.png artwork.jpg artwork.png
[mpd] [mpd]
host = localhost host = localhost