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 (const TagLib::FileRef f, const string &key)
59 TagLib::PropertyMap propmap = f.file()->properties();
61 f.file()->setProperties(propmap);
64 void action_replaceTag (const TagLib::FileRef f, const tagpair &tagPair)
66 TagLib::PropertyMap propmap = f.file()->properties();
67 propmap.replace(tagPair.first, argToStringList(tagPair.second));
68 f.file()->setProperties(propmap);
71 void action_insertTag (const TagLib::FileRef f, const tagpair &tagPair)
73 TagLib::PropertyMap propmap = f.file()->properties();
74 propmap.insert(tagPair.first, argToStringList(tagPair.second));
75 f.file()->setProperties(propmap);
78 int action_printTags (const TagLib::FileRef f)
81 TagLib::PropertyMap tags = f.file()->properties();
82 unsigned int longest = 0;
83 for(TagLib::PropertyMap::ConstIterator i = tags.begin(); i != tags.end(); ++i) {
84 if (i->first.size() > longest) {
85 longest = i->first.size();
88 cout << "-- TAG (properties) --" << endl;
89 for(TagLib::PropertyMap::ConstIterator i = tags.begin(); i != tags.end(); ++i) {
90 for(TagLib::StringList::ConstIterator j = i->second.begin(); j != i->second.end(); ++j) {
91 cout << i->first << "=" << *j << endl;
100 int action_printAudio (const TagLib::FileRef f)
102 if(f.audioProperties()) {
103 TagLib::AudioProperties *properties = f.audioProperties();
104 int seconds = properties->length() % 60;
105 int minutes = (properties->length() - seconds) / 60;
106 cout << "-- AUDIO --" << endl;
107 cout << "BITRATE=" << properties->bitrate() << endl;
108 cout << "SAMPLERATE=" << properties->sampleRate() << endl;
109 cout << "CHANNELS=" << properties->channels() << endl;
110 cout << "LENGTH=" << minutes << ":" << setfill('0') << setw(2) << seconds << endl;
119 cout << "Usage: usetaglib [ACTION]... [FILE]..." << endl;
120 cout << "List and edit tags on mediafiles in formats supported by libtag." << endl;
122 cout << "-h, --help Show this help" << endl;
124 cout << "ACTIONS" << endl;
125 cout << setfill(' ') << setw(45) << left << " -l, --list"
126 << "list all tags (implicit if no action specified)"<< endl;
127 cout << setfill(' ') << setw(45) << left << " -e, --erase=TAGNAME"
128 << "erase tag with name TAGNAME"<< endl;
129 cout << setfill(' ') << setw(45) << left << " -r, --replace=TAGNAME=TAGVAL[=TAGVAL...]"
130 << "replace tag TAGNAME with list of values TAGVAL"<< endl;
131 cout << setfill(' ') << setw(45) << left << " -i, --insert=TAGNAME=TAGVAL[=TAGVAL...]"
132 << "insert list of values TAGVAL for tag TAGNAME"<< endl;
135 int main(int argc, char *argv[])
138 actionqueue requestedActions;
142 static struct option long_options[] =
144 {"help", no_argument, 0, 'h'},
145 {"list", no_argument, 0, 'l'},
146 {"listaudio", no_argument, 0, 'a'},
147 {"insert", required_argument, 0, 'i'},
148 {"erase", required_argument, 0, 'e'},
149 {"replace", required_argument, 0, 'r'},
153 int option_index = 0;
154 c = getopt_long (argc, argv, "hlai:e:r:",
155 long_options, &option_index);
163 if (long_options[option_index].flag != 0)
172 requestedActions.push_back( make_pair(LIST, "") );
176 requestedActions.push_back( make_pair(AUDIO, "") );
180 requestedActions.push_back( make_pair(INSERT, optarg) );
184 requestedActions.push_back( make_pair(ERASE, optarg) );
188 requestedActions.push_back( make_pair(REPLACE, optarg) );
199 if (requestedActions.size() == 0)
200 requestedActions.push_back( make_pair(LIST, "") );
202 for(int i = optind; i < argc; i++) {
203 cout << "******************** \"" << argv[i] << "\" ********************" << endl;
204 TagLib::FileRef f(argv[i]);
209 for (actionqueue::iterator actit = requestedActions.begin(); actit != requestedActions.end(); ++actit) {
210 switch (actit->first) {
216 action_printAudio(f);
220 action_eraseTag(f, actit->second);
224 action_replaceTag(f, splitToTagPair(actit->second));
228 action_insertTag(f, splitToTagPair(actit->second));