[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Emms-patches] [PATCH 2/2] New metadata extraction program
From: |
Petteri Hintsanen |
Subject: |
[Emms-patches] [PATCH 2/2] New metadata extraction program |
Date: |
Thu, 14 May 2015 00:11:03 +0300 |
New program emms-print-taglib-metadata uses TagLib to extract track
information.
---
Makefile | 5 ++
src/emms-print-taglib-metadata.cpp | 130 +++++++++++++++++++++++++++++++++++++
2 files changed, 135 insertions(+)
create mode 100644 src/emms-print-taglib-metadata.cpp
diff --git a/Makefile b/Makefile
index c0846d6..dda3640 100644
--- a/Makefile
+++ b/Makefile
@@ -4,6 +4,8 @@ DOCDIR=doc/
LISPDIR=lisp
SRCDIR=src
+CXXFLAGS=-std=c++11
+
ALLSOURCE=$(wildcard $(LISPDIR)/*.el)
ALLCOMPILED=$(wildcard $(LISPDIR)/*.elc)
@@ -37,6 +39,9 @@ docs:
emms-print-metadata: $(SRCDIR)/emms-print-metadata.c
$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $(SRCDIR)/$@ $<
`taglib-config --cflags --libs` -ltag_c
+emms-print-taglib-metadata: $(SRCDIR)/emms-print-taglib-metadata.cpp
+ $(CXX) $(CPPFLAGS) $(CXXFLAGS) $(LDFLAGS) -o $(SRCDIR)/$@ $<
`taglib-config --cflags --libs`
+
install:
test -d $(SITELISP) || mkdir -p $(SITELISP)
test -d $(INFODIR) || install -d $(INFODIR)
diff --git a/src/emms-print-taglib-metadata.cpp
b/src/emms-print-taglib-metadata.cpp
new file mode 100644
index 0000000..ef3c213
--- /dev/null
+++ b/src/emms-print-taglib-metadata.cpp
@@ -0,0 +1,130 @@
+/* emms-print-taglib-metadata.cpp --- Info function for TagLib
+ Copyright (C) 2015 Free Software Foundation, Inc.
+
+Author: Petteri Hintsanen <address@hidden>
+
+This file is part of EMMS.
+
+EMMS is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 3, or (at your option)
+any later version.
+
+EMMS is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with EMMS; see the file COPYING. If not, write to
+the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+Boston, MA 02110-1301, USA. */
+
+#include <taglib/fileref.h>
+#include <taglib/tag.h>
+#include <taglib/tpropertymap.h>
+#include <iostream>
+
+struct Tag
+{
+ // Primary key for a tag (e.g., "artist").
+ std::string key;
+ // If the primary key is not found, try these keys.
+ std::vector<std::string> fallbacks;
+ // If output_key is non-empty, it will be printed instead of the
+ // primary key. This is used to rename keys, e.g., convert "date"
+ // to "year".
+ std::string output_key;
+};
+
+static const std::vector<Tag> tags_to_extract {
+ { "album", { }, "" },
+ { "albumartist", { "artist" }, "" },
+ { "albumartistsort", { "artistsort" }, "" },
+ { "artist", { }, "" },
+ { "artistsort", { }, "" },
+ { "composer", { }, "" },
+ { "performer", { }, "" },
+ { "date", { "year" }, "year" },
+ { "discnumber", { }, "" },
+ { "genre", { }, "" },
+ { "label", { }, "" },
+ { "originaldate", { "date", "year" }, "originalyear" },
+ { "title", { }, "" },
+ { "tracknumber", { }, "" }
+};
+
+void print_tag (const TagLib::PropertyMap& tags, const Tag& tag);
+
+int
+main (int argc, char* argv[])
+{
+ if (argc != 2)
+ {
+ std::cerr << "Usage: emms-print-metadata file.{mp3,ogg,flac}\n"
+ "Other formats may work as well.\n";
+ return 1;
+ }
+
+ TagLib::FileRef file (argv[1]);
+ if (file.isNull ()) return 1;
+
+ const auto tags = file.file ()->properties ();
+
+ for (const auto& tag : tags_to_extract)
+ {
+ print_tag (tags, tag);
+ }
+
+ int length = 0;
+ if (file.audioProperties ())
+ {
+ TagLib::AudioProperties* properties = file.audioProperties ();
+ length = properties->length ();
+ }
+ std::cout << "info-playing-time=" << length << std::endl;
+
+ return 0;
+}
+
+void
+print_tag (const TagLib::PropertyMap& tags, const Tag& tag)
+{
+ std::string t { tag.key };
+ TagLib::StringList values = tags[t];
+ if (values.isEmpty ())
+ {
+ for (auto fallback : tag.fallbacks)
+ {
+ values = tags[fallback];
+ if (!values.isEmpty ()) break;
+ }
+ }
+
+ if (!values.isEmpty ())
+ {
+ // TODO: Extract all values, not only the first one. EMMS needs
+ // to be extended to handle such values.
+ auto value = values.front ();
+ if (t == "tracknumber" || t == "discnumber")
+ {
+ // Take only the actual track or disc index.
+ auto tokens = value.split ("/");
+ value = tokens[0];
+ }
+
+ // Convert date and originaldate to year. This assumes
+ // YYYY-MM-DD or YYYY/MM/DD date format.
+ if (t == "date" || t == "originaldate")
+ {
+ auto tokens = value.split ("-");
+ if (tokens.size () == 1) tokens = value.split ("/");
+ value = tokens[0];
+ }
+
+ if (!tag.output_key.empty ()) t = tag.output_key;
+ std::cout << "info-" << t << "=" << value.to8Bit (true) << std::endl;
+ }
+}
+
+/* emms-print-taglib-metadata.cpp ends here. */
--
2.1.4