Skip to content

Commit

Permalink
add support for embedded cover art
Browse files Browse the repository at this point in the history
This adds a dependency on libavformat to extract the cover art.

Fixes #91
  • Loading branch information
gnojus authored and hoyon committed Aug 30, 2023
1 parent a96a6ed commit 2f9c126
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ LN := ln
RM := rm

# Base flags, environment CFLAGS / LDFLAGS can be appended.
BASE_CFLAGS = -std=c99 -Wall -Wextra -O2 -pedantic $(shell $(PKG_CONFIG) --cflags gio-2.0 gio-unix-2.0 glib-2.0 mpv)
BASE_LDFLAGS = $(shell $(PKG_CONFIG) --libs gio-2.0 gio-unix-2.0 glib-2.0)
BASE_CFLAGS = -std=c99 -Wall -Wextra -O2 -pedantic $(shell $(PKG_CONFIG) --cflags gio-2.0 gio-unix-2.0 glib-2.0 mpv libavformat)
BASE_LDFLAGS = $(shell $(PKG_CONFIG) --libs gio-2.0 gio-unix-2.0 glib-2.0 libavformat)

SCRIPTS_DIR := $(HOME)/.config/mpv/scripts

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Build requirements:
- mpv development files
- glib development files
- gio development files
- libavformat development files

Building should be as simple as running `make` in the source code directory.

Expand Down
36 changes: 36 additions & 0 deletions mpris.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#include <gio/gio.h>
#include <glib-unix.h>
#include <mpv/client.h>
#include <libavformat/avformat.h>
#include <inttypes.h>
#include <string.h>



static const char *introspection_xml =
"<node>\n"
" <interface name=\"org.mpris.MediaPlayer2\">\n"
Expand Down Expand Up @@ -283,6 +286,38 @@ static void try_put_youtube_thumbnail(GVariantDict *dict, char *path)
g_match_info_free(match_info);
}

static void try_put_embedded_art(GVariantDict *dict, char *path)
{
AVFormatContext *formatContext = NULL;
if (avformat_open_input(&formatContext, path, NULL, NULL) < 0) {
g_printerr("failed to open input file");
return;
}

if (avformat_find_stream_info(formatContext, NULL) < 0) {
g_printerr("failed to find stream info");
return;
}

AVPacket packet = {.size = 0};
for (unsigned int i = 0; i < formatContext->nb_streams; i++) {
if (formatContext->streams[i]->disposition & AV_DISPOSITION_ATTACHED_PIC) {
packet = formatContext->streams[i]->attached_pic;
}
}
if (!packet.size) {
return;
}

gchar *data = g_base64_encode(packet.data, packet.size);
gchar *img = g_strconcat("data:image/jpeg;base64,", data, NULL);
g_variant_dict_insert(dict, "mpris:artUrl", "s", img);

g_free(data);
g_free(img);
avformat_close_input(&formatContext);
}

static void add_metadata_art(mpv_handle *mpv, GVariantDict *dict)
{
char *path = mpv_get_property_string(mpv, "path");
Expand All @@ -294,6 +329,7 @@ static void add_metadata_art(mpv_handle *mpv, GVariantDict *dict)
if (g_str_has_prefix(path, "http")) {
try_put_youtube_thumbnail(dict, path);
} else {
try_put_embedded_art(dict, path);
try_put_local_art(mpv, dict, path);
}

Expand Down

0 comments on commit 2f9c126

Please sign in to comment.