From 91fd7b03788b29ae8cd7d09277d2f7ca32aba26c Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Fri, 7 Oct 2016 09:54:54 +0200 Subject: use libmagic to decide whether to search for media artwork --- Makefile | 1 + mpd-notification.c | 44 +++++++++++++++++++++++++++++++++----------- mpd-notification.h | 6 ++++-- 3 files changed, 38 insertions(+), 13 deletions(-) diff --git a/Makefile b/Makefile index f597bf0..98fbd93 100644 --- a/Makefile +++ b/Makefile @@ -15,6 +15,7 @@ CFLAGS += $(shell pkg-config --cflags --libs libnotify) LIBAV_CFLAGS := $(shell pkg-config --cflags --libs libavformat libavutil 2>/dev/null) ifneq ($(LIBAV_CFLAGS),) CFLAGS += -DHAVE_LIBAV $(LIBAV_CFLAGS) +CFLAGS += -lmagic endif LDFLAGS += -Wl,-z,now -Wl,-z,relro -pie diff --git a/mpd-notification.c b/mpd-notification.c index 543f60a..194655b 100644 --- a/mpd-notification.c +++ b/mpd-notification.c @@ -71,14 +71,34 @@ GdkPixbuf * retrieve_artwork(const char * music_dir, const char * uri) { #ifdef HAVE_LIBAV int i; - AVPacket pkt; - AVFormatContext * pFormatCtx; + magic_t magic = NULL; + const char *magic_mime; + AVFormatContext * pFormatCtx = NULL; GdkPixbufLoader * loader; /* try album artwork first */ uri_path = malloc(strlen(music_dir) + strlen(uri) + 2); sprintf(uri_path, "%s/%s", music_dir, uri); + if ((magic = magic_open(MAGIC_MIME_TYPE)) == NULL) { + fprintf(stderr, "unable to initialize magic library\n"); + goto image; + } + + if (magic_load(magic, NULL) != 0) { + fprintf(stderr, "cannot load magic database: %s\n", magic_error(magic)); + magic_close(magic); + goto image; + } + + if ((magic_mime = magic_file(magic, uri_path)) == NULL) { + fprintf(stderr, "No MIME...\n"); + goto image; + } + + if (strcmp(magic_mime, "audio/mpeg") != 0) + goto image; + pFormatCtx = avformat_alloc_context(); if (avformat_open_input(&pFormatCtx, uri_path, NULL, NULL) != 0) { @@ -86,10 +106,6 @@ GdkPixbuf * retrieve_artwork(const char * music_dir, const char * uri) { goto image; } - /* only mp3 file contain artwork, so ignore others */ - if (strcmp(pFormatCtx->iformat->name, "mp3") != 0) - goto image; - if (pFormatCtx->iformat->read_header(pFormatCtx) < 0) { printf("could not read the format header\n"); goto image; @@ -98,7 +114,7 @@ GdkPixbuf * retrieve_artwork(const char * music_dir, const char * uri) { /* find the first attached picture, if available */ for (i = 0; i < pFormatCtx->nb_streams; i++) { if (pFormatCtx->streams[i]->disposition & AV_DISPOSITION_ATTACHED_PIC) { - pkt = pFormatCtx->streams[i]->attached_pic; + AVPacket pkt = pFormatCtx->streams[i]->attached_pic; loader = gdk_pixbuf_loader_new(); gdk_pixbuf_loader_write(loader, pkt.data, pkt.size, NULL); @@ -143,11 +159,17 @@ image: done: fail: - avformat_close_input(&pFormatCtx); - avformat_free_context(pFormatCtx); + if (pFormatCtx != NULL) { + avformat_close_input(&pFormatCtx); + avformat_free_context(pFormatCtx); + } + +#ifdef HAVE_LIBAV + if (magic != NULL) + magic_close(magic); +#endif - if (uri_path) - free(uri_path); + free(uri_path); return pixbuf; } diff --git a/mpd-notification.h b/mpd-notification.h index 591b763..bb27007 100644 --- a/mpd-notification.h +++ b/mpd-notification.h @@ -19,11 +19,13 @@ #include #include +#include +#include + #ifdef HAVE_LIBAV #include +#include #endif -#include -#include #include "config.h" #include "version.h" -- cgit v1.2.3-54-g00ecf