| 1 | /* emms-print-metadata.cpp --- Info function for TagLib |
| 2 | Copyright (C) 2016 Free Software Foundation, Inc. |
| 3 | |
| 4 | Author: Petteri Hintsanen <petterih@iki.fi> |
| 5 | |
| 6 | This file is part of EMMS. |
| 7 | |
| 8 | EMMS is free software; you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation; either version 3, or (at your option) |
| 11 | any later version. |
| 12 | |
| 13 | EMMS is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with EMMS; see the file COPYING. If not, write to |
| 20 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 21 | Boston, MA 02110-1301, USA. */ |
| 22 | |
| 23 | #include <taglib/fileref.h> |
| 24 | #include <taglib/tag.h> |
| 25 | #include <taglib/tpropertymap.h> |
| 26 | #include <iostream> |
| 27 | |
| 28 | static const char* const tags_to_extract[] = { |
| 29 | "album", |
| 30 | "albumsort", |
| 31 | "albumartist", |
| 32 | "albumartistsort", |
| 33 | "artist", |
| 34 | "artistsort", |
| 35 | "composer", |
| 36 | "composersort", |
| 37 | "performer", |
| 38 | "year", |
| 39 | "originalyear", |
| 40 | "date", |
| 41 | "originaldate", |
| 42 | "genre", |
| 43 | "label", |
| 44 | "title", |
| 45 | "titlesort", |
| 46 | "tracknumber", |
| 47 | "discnumber" |
| 48 | }; |
| 49 | |
| 50 | void print_tag (const TagLib::PropertyMap& tags, const std::string& tag); |
| 51 | |
| 52 | int |
| 53 | main (int argc, char* argv[]) |
| 54 | { |
| 55 | if (argc != 2) |
| 56 | { |
| 57 | std::cerr << argv[0] << ": " |
| 58 | << "usage: emms-print-metadata FILENAME" |
| 59 | << std::endl |
| 60 | << "FILENAME must end to one of these extensions: " |
| 61 | << TagLib::FileRef::defaultFileExtensions ().toString () |
| 62 | << std::endl; |
| 63 | return 1; |
| 64 | } |
| 65 | |
| 66 | TagLib::FileRef file (argv[1]); |
| 67 | if (file.isNull ()) { |
| 68 | std::cerr << argv[0] << ": " |
| 69 | << argv[1] << ": " |
| 70 | << "file does not exist or is of an unknown type" |
| 71 | << std::endl; |
| 72 | return 1; |
| 73 | } |
| 74 | |
| 75 | const TagLib::PropertyMap tags = file.file ()->properties (); |
| 76 | if (tags.isEmpty ()) { |
| 77 | std::cerr << argv[0] << ": " |
| 78 | << argv[1] << ": " |
| 79 | << "file does not have tags or is of an unknown type" |
| 80 | << std::endl; |
| 81 | return 1; |
| 82 | } |
| 83 | |
| 84 | for (unsigned int i = 0; i < sizeof (tags_to_extract) / sizeof (char*); |
| 85 | i++) |
| 86 | { |
| 87 | print_tag (tags, tags_to_extract[i]); |
| 88 | } |
| 89 | |
| 90 | int length = 0; |
| 91 | if (file.audioProperties ()) |
| 92 | { |
| 93 | const TagLib::AudioProperties* properties = file.audioProperties (); |
| 94 | length = properties->length (); |
| 95 | } |
| 96 | std::cout << "info-playing-time=" << length << std::endl; |
| 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | void |
| 102 | print_tag (const TagLib::PropertyMap& tags, const std::string& tag) |
| 103 | { |
| 104 | TagLib::StringList values = tags[tag]; |
| 105 | if (!values.isEmpty ()) |
| 106 | { |
| 107 | const TagLib::String& value = values.front (); |
| 108 | std::cout << "info-" << tag << "=" << value.to8Bit (true) << std::endl; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | /* emms-print-taglib-metadata.cpp ends here. */ |