1 /* Copyright (C) 2015 Alexander Schmidt <alex@treefish.org>
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <taglib/fileref.h>
21 #include <taglib/tag.h>
22 #include <taglib/tpropertymap.h>
23 #include <taglib/tstringlist.h>
29 enum action {LIST, REPLACE, INSERT, ERASE, AUDIO};
30 typedef pair<action,string> actionpair;
31 typedef vector<actionpair> actionqueue;
32 typedef pair<string,string> tagpair;
34 TagLib::StringList argToStringList (const string &rawtagarg)
36 TagLib::StringList newlist;
40 size_t nextdelpos = rawtagarg.find('=', delpos+1);
43 newlist.append(rawtagarg.substr(delpos,nextdelpos-delpos));
44 delpos = nextdelpos+1;
46 newlist.append(rawtagarg.substr(delpos,string::npos));
51 tagpair splitToTagPair (const string &rawarg)
53 const size_t delpos = rawarg.find('=');
54 return make_pair(rawarg.substr(0, delpos), rawarg.substr(delpos+1, string::npos));
57 void action_eraseTag (TagLib::PropertyMap &propmap, const string &key)
62 void action_replaceTag (TagLib::PropertyMap &propmap, const tagpair &tagPair)
64 propmap.replace(tagPair.first, argToStringList(tagPair.second));
67 void action_insertTag (TagLib::PropertyMap &propmap, const tagpair &tagPair)
69 propmap.insert(tagPair.first, argToStringList(tagPair.second));
72 int action_printTags (const TagLib::FileRef f, TagLib::PropertyMap &propmap)
75 unsigned int longest = 0;
76 for(TagLib::PropertyMap::ConstIterator i = propmap.begin(); i != propmap.end(); ++i) {
77 if (i->first.size() > longest) {
78 longest = i->first.size();
81 cout << "----[ TAGS ]----" << endl;
82 for(TagLib::PropertyMap::ConstIterator i = propmap.begin(); i != propmap.end(); ++i) {
83 for(TagLib::StringList::ConstIterator j = i->second.begin(); j != i->second.end(); ++j) {
84 cout << i->first << "=" << *j << endl;
93 int action_printAudio (const TagLib::FileRef f)
95 if(f.audioProperties()) {
96 TagLib::AudioProperties *properties = f.audioProperties();
97 int seconds = properties->length() % 60;
98 int minutes = (properties->length() - seconds) / 60;
99 cout << "----[ AUDIO PROPERTIES ]----" << endl;
100 cout << "BITRATE=" << properties->bitrate() << endl;
101 cout << "SAMPLERATE=" << properties->sampleRate() << endl;
102 cout << "CHANNELS=" << properties->channels() << endl;
103 cout << "LENGTH=" << minutes << ":" << setfill('0') << setw(2) << seconds << endl;
113 "Usage: usetaglib [ACTION]... [FILE]...\n"
114 "Read and edit meta-data of audio formats supported by taglib.\n"
115 "Multiple ACTIONS and FILES may be given in arbitrary order.\n"
117 "-h, --help show help\n"
118 "-H, --longhelp show long help\n"
121 " If multiple actions are specified they are executed in given order.\n"
123 " -l, --list list all tags (implicit if no action given)\n"
124 " -a, --audio show audio properties\n"
125 " -e, --erase=TAGNAME erase tag TAGNAME\n"
126 " -r, --replace=TAGNAME=TAGVAL replace tag TAGNAME with value TAGVAL\n"
127 " -i, --insert=TAGNAME=TAGVAL insert tag TAGNAME with value TAGVAL\n";
130 void printExtraHelp ()
135 " TAGNAME is a media format independent id encoding the type of a tag.\n"
136 " Note that also in --list output, format specific tag ids are translated\n"
137 " to unified TAGNAMES.\n"
139 " Some \"well-known\" tags you might want to use are:\n"
140 " TITLE ALBUM ARTIST ALBUMARTIST SUBTITLE TRACKNUMBER DISCNUMBER DATE\n"
141 " ORIGINALDATE GENRE COMMENT TITLESORT ALBUMSORT ARTISTSORT\n"
142 " ALBUMARTISTSORT COMPOSER LYRICIST CONDUCTOR REMIXER PERFORMER ISRC ASIN\n"
143 " BPM COPYRIGHT ENCODEDBY MOOD COMMENT MEDIA LABEL CATALOGNUMBER BARCODE\n"
146 " TAGVAL has to be either a single string or a list of strings separated\n"
147 " by equal signs (=). If a list is given, multiple tags of type TAGNAME\n"
148 " will be created and set to the respective values given in the list.\n"
151 " usetaglib file.ogg\n"
152 " usetaglib -e ALBUM file.flac\n"
153 " usetaglib -r \"ALBUM=New Album\" -i ARTIST=Horst=Hubert file.mp3\n"
154 " usetaglib -r ARTIST=Horst -l file1.ogg file2.mp3\n"
155 " usetaglib -i \"ALBUMARTIST=Horst und Hubert\" file.ogg\n"
156 " usetaglib --insert=\"ALBUMARTIST=Horst und Hubert\" file.ogg\n";
159 int main(int argc, char *argv[])
162 actionqueue requestedActions;
166 static struct option long_options[] =
168 {"help", no_argument, 0, 'h'},
169 {"longhelp", no_argument, 0, 'H'},
170 {"list", no_argument, 0, 'l'},
171 {"audio", no_argument, 0, 'a'},
172 {"insert", required_argument, 0, 'i'},
173 {"erase", required_argument, 0, 'e'},
174 {"replace", required_argument, 0, 'r'},
178 int option_index = 0;
179 c = getopt_long (argc, argv, "hHlai:e:r:",
180 long_options, &option_index);
188 if (long_options[option_index].flag != 0)
200 requestedActions.push_back( make_pair(LIST, "") );
203 requestedActions.push_back( make_pair(AUDIO, "") );
206 requestedActions.push_back( make_pair(INSERT, optarg) );
209 requestedActions.push_back( make_pair(ERASE, optarg) );
212 requestedActions.push_back( make_pair(REPLACE, optarg) );
221 if ( optind == argc ) {
226 if (requestedActions.size() == 0)
227 requestedActions.push_back( make_pair(LIST, "") );
229 for(int i = optind; i < argc; i++) {
230 cout << " _________________________________________ _ _ _" << endl;
232 cout << " " << argv[i] << endl;
233 cout << "\\_________________________________________ _ _ _" << endl;
235 TagLib::FileRef f(argv[i]);
240 TagLib::PropertyMap propmap = f.file()->properties();
241 bool FCHANGED = false;
243 for (actionqueue::iterator actit = requestedActions.begin(); actit != requestedActions.end(); ++actit) {
244 switch (actit->first) {
246 action_printTags(f, propmap);
249 action_printAudio(f);
252 action_eraseTag(propmap, actit->second);
256 action_replaceTag(propmap, splitToTagPair(actit->second));
260 action_insertTag(propmap, splitToTagPair(actit->second));
267 f.file()->setProperties(propmap);