aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Christian Hesse <mail@eworm.de>2015-07-14 18:25:00 +0200
committerGravatar Christian Hesse <mail@eworm.de>2015-07-14 18:25:00 +0200
commitc937af44c912773aa3e8fd9ed88045e483cb9956 (patch)
treea3f22acbb602f65f85c66337c33ac11d0a29d411
downloadextract-artwork-c937af44c912773aa3e8fd9ed88045e483cb9956.tar.gz
extract-artwork-c937af44c912773aa3e8fd9ed88045e483cb9956.tar.zst
init
-rw-r--r--.gitignore6
-rw-r--r--Makefile36
-rw-r--r--README.md4
-rw-r--r--extract-artwork.c80
4 files changed, 126 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..3475d12
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,6 @@
+*~
+*.o
+extract-artwork
+README.html
+extract-artwork-*.tar.xz
+extract-artwork-*.tar.xz.asc
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..a082cb9
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,36 @@
+# extract-artwork - Extract artwork from media files
+
+CC := gcc
+MD := markdown
+INSTALL := install
+CP := cp
+RM := rm
+CFLAGS += -std=c11 -O2 -Wall -Werror
+CFLAGS += $(shell pkg-config --cflags --libs libavformat libavutil)
+# this is just a fallback in case you do not use git but downloaded
+# a release tarball...
+VERSION := 0.0.1
+
+all: extract-artwork README.html
+
+extract-artwork: extract-artwork.c
+ $(CC) $(CFLAGS) -o extract-artwork extract-artwork.c
+
+README.html: README.md
+ $(MD) README.md > README.html
+
+install: install-bin install-doc
+
+install-bin: extract-artwork
+ $(INSTALL) -D -m0755 extract-artwork $(DESTDIR)/usr/bin/extract-artwork
+
+install-doc: README.html
+ $(INSTALL) -D -m0644 README.md $(DESTDIR)/usr/share/doc/extract-artwork/README.md
+ $(INSTALL) -D -m0644 README.html $(DESTDIR)/usr/share/doc/extract-artwork/README.html
+
+clean:
+ $(RM) -f *.o *~ README.html extract-artwork
+
+release:
+ git archive --format=tar.xz --prefix=extract-artwork-$(VERSION)/ $(VERSION) > extract-artwork-$(VERSION).tar.xz
+ gpg -ab extract-artwork-$(VERSION).tar.xz
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..7b5e417
--- /dev/null
+++ b/README.md
@@ -0,0 +1,4 @@
+extract-artwork
+===============
+
+Extract artwork from media files
diff --git a/extract-artwork.c b/extract-artwork.c
new file mode 100644
index 0000000..7d927a5
--- /dev/null
+++ b/extract-artwork.c
@@ -0,0 +1,80 @@
+/*
+ * (C) 2015 by Christian Hesse <mail@eworm.de>
+ *
+ * This software may be used and distributed according to the terms
+ * of the GNU General Public License, incorporated herein by reference.
+ *
+ * This is an example code skeleton provided by vim-skeleton.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <libavformat/avformat.h>
+
+int main(int argc, char **argv) {
+ unsigned int i, j;
+ FILE * album_art;
+ char * album_art_file = NULL;
+ AVPacket pkt;
+ AVFormatContext * pFormatCtx;
+
+ if (argc < 2) {
+ printf("usage: %s file1.mp3 [file2.mp3 ...]\n", argv[0]);
+ return EXIT_FAILURE;
+ }
+
+ /* libav */
+ av_register_all();
+
+ /* only fatal messages from libav */
+ av_log_set_level(AV_LOG_FATAL);
+
+ pFormatCtx = avformat_alloc_context();
+
+ for (i = 1; i < argc; i++) {
+ if (avformat_open_input(&pFormatCtx, argv[i], NULL, NULL) != 0) {
+ fprintf(stderr, "%s: failed to open file for reading", argv[i]);
+ goto next;
+ }
+
+ /* only mp3 file contain artwork, so ignore others */
+ if (strcmp(pFormatCtx->iformat->name, "mp3") != 0) {
+ fprintf(stderr, "%s: not a mp3 file\n", argv[i]);
+ goto next;
+ }
+
+ if (pFormatCtx->iformat->read_header(pFormatCtx) < 0) {
+ fprintf(stderr, "%s: could not read the format header\n", argv[i]);
+ goto next;
+ }
+
+ /* find the first attached picture, if available */
+ for (j = 0; j < pFormatCtx->nb_streams; j++) {
+ if (pFormatCtx->streams[j]->disposition & AV_DISPOSITION_ATTACHED_PIC) {
+ pkt = pFormatCtx->streams[j]->attached_pic;
+
+ album_art_file = malloc(strlen(argv[i]) + 9);
+ sprintf(album_art_file, "%s.%03u.jpg", argv[i], j);
+
+ printf("%s: found artwork, writing to file %s\n", argv[i], album_art_file);
+
+ if ((album_art = fopen(album_art_file, "wb")) == NULL)
+ fprintf(stderr, "%s: failed to open file for writing\n", album_art_file);
+
+ if (fwrite(pkt.data, pkt.size, 1, album_art) != 1)
+ fprintf(stderr, "%s: failed to write to file\n", album_art_file);
+
+ fclose(album_art);
+ free(album_art_file);
+ }
+ }
+next:
+ avformat_close_input(&pFormatCtx);
+ }
+ avformat_free_context(pFormatCtx);
+
+ return EXIT_SUCCESS;
+}
+
+// vim: set syntax=c: