]> git.treefish.org Git - usetaglib.git/blob - usetaglib.cpp
3a8f435850f0527498054fdb1ddda9a209df4e1e
[usetaglib.git] / usetaglib.cpp
1 /* Copyright (C) 2015 Alexander Schmidt <alex@treefish.org>
2  *
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.
7  * 
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.
12  * 
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/>. 
15  */
16
17 #include <iostream>
18 #include <iomanip>
19 #include <iostream>
20 #include <taglib/fileref.h>
21 #include <taglib/tag.h>
22 #include <taglib/tpropertymap.h>
23 #include <taglib/tstringlist.h>
24 #include <getopt.h>
25 #include <vector>
26
27 using namespace std;
28
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;
33
34 TagLib::StringList argToStringList (const string &rawtagarg)
35 {
36   TagLib::StringList newlist;
37
38   size_t delpos = 0;
39   while (1) {
40     size_t nextdelpos = rawtagarg.find('=', delpos+1);
41     if (nextdelpos == -1)
42       break;
43     newlist.append(rawtagarg.substr(delpos,nextdelpos-delpos));
44     delpos = nextdelpos+1;
45   }
46   newlist.append(rawtagarg.substr(delpos,string::npos));  
47
48   return newlist;
49 }
50
51 tagpair splitToTagPair (const string &rawarg)
52 {
53   const size_t delpos = rawarg.find('=');
54   return make_pair(rawarg.substr(0, delpos), rawarg.substr(delpos+1, string::npos));
55 }
56
57 void action_eraseTag (TagLib::PropertyMap &propmap, const string &key)
58 {
59   propmap.erase(key);
60 }
61
62 void action_replaceTag (TagLib::PropertyMap &propmap, const tagpair &tagPair)
63 {
64   propmap.replace(tagPair.first, argToStringList(tagPair.second));
65 }
66
67 void action_insertTag (TagLib::PropertyMap &propmap, const tagpair &tagPair)
68 {
69   propmap.insert(tagPair.first, argToStringList(tagPair.second));
70 }
71
72 int action_printTags (const TagLib::FileRef f)
73 {
74   if(f.tag()) {
75     TagLib::PropertyMap tags = f.file()->properties();
76     unsigned int longest = 0;
77     for(TagLib::PropertyMap::ConstIterator i = tags.begin(); i != tags.end(); ++i) {
78       if (i->first.size() > longest) {
79         longest = i->first.size();
80       }
81     }
82     cout << "-- TAG (properties) --" << endl;
83     for(TagLib::PropertyMap::ConstIterator i = tags.begin(); i != tags.end(); ++i) {
84       for(TagLib::StringList::ConstIterator j = i->second.begin(); j != i->second.end(); ++j) {
85         cout << i->first << "=" << *j << endl;
86       }
87     }
88     return 0;
89   }
90   else
91     return 1;
92 }
93
94 int action_printAudio (const TagLib::FileRef f)
95 {
96   if(f.audioProperties()) {
97     TagLib::AudioProperties *properties = f.audioProperties();
98     int seconds = properties->length() % 60;
99     int minutes = (properties->length() - seconds) / 60;
100     cout << "-- AUDIO --" << endl;
101     cout << "BITRATE=" << properties->bitrate() << endl;
102     cout << "SAMPLERATE=" << properties->sampleRate() << endl;
103     cout << "CHANNELS=" << properties->channels() << endl;
104     cout << "LENGTH=" << minutes << ":" << setfill('0') << setw(2) << seconds << endl;
105     return 0;
106   }
107   else
108     return 1;
109 }
110
111 void printHelp ()
112 {
113   return;
114   
115   cout << "Usage: usetaglib [ACTION]... [FILE]..." << endl;
116   cout << "List and edit tags on mediafiles in formats supported by libtag." << endl;
117   cout << endl;
118   cout << "-h, --help   Show this help" << endl;
119   cout << endl;
120   cout << "ACTIONS" << endl;
121   cout << setfill(' ') << setw(37) << left << "  -l, --list"
122        << "list all tags (implicit if no action specified)"<< endl;
123   cout << setfill(' ') << setw(37) << left << "  -a, --listaudio"
124        << "show audio information"<< endl;
125   cout << setfill(' ') << setw(37) << left << "  -e, --erase=TAGNAME"
126        << "erase tag TAGNAME"<< endl;
127   cout << setfill(' ') << setw(37) << left << "  -r, --replace=TAGNAME=TAGVALSPEC"
128        << "replace tag TAGNAME with value TAGVALSPEC"<< endl;
129   cout << setfill(' ') << setw(37) << left << "  -i, --insert=TAGNAME=TAGVALSPEC"
130        << "insert value TAGVALSPEC for tag TAGNAME"<< endl;
131   cout << endl;
132   cout << "TAGVALSPEC" << endl;
133   cout << "  a list of values separated by '=': val1=val2=val3..." << endl;
134   cout << endl;
135   cout << "EXAMPLES" << endl;
136   cout << "  usetaglib file.ogg" << endl;
137   cout << "  usetaglib -e ALBUM file.flac" << endl;
138   cout << "  usetaglib -i ARTIST=Horst=Hubert file.mp3" << endl;
139   cout << "  usetaglib -r ARTIST=Horst file.ogg" << endl;
140 }
141   
142 int main(int argc, char *argv[])
143 {
144   int c;
145   actionqueue requestedActions;
146   
147   while (1)
148     {
149       static struct option long_options[] =
150         {
151           {"help",      no_argument,       0, 'h'},
152           {"list",      no_argument,       0, 'l'},
153           {"listaudio", no_argument,       0, 'a'},
154           {"insert",    required_argument, 0, 'i'},
155           {"erase",     required_argument, 0, 'e'},
156           {"replace",   required_argument, 0, 'r'},
157           {0, 0, 0, 0}
158         };
159
160       int option_index = 0;      
161       c = getopt_long (argc, argv, "hlai:e:r:",
162                        long_options, &option_index);
163
164       if (c == -1)
165         break;
166
167       switch (c)
168         {
169         case 0:
170           if (long_options[option_index].flag != 0)
171             break;
172
173         case 'h':
174           printHelp();
175           return 0;
176           break;
177           
178         case 'l':
179           requestedActions.push_back( make_pair(LIST, "") );
180           break;
181
182         case 'a':
183           requestedActions.push_back( make_pair(AUDIO, "") );
184           break;
185
186         case 'i':
187           requestedActions.push_back( make_pair(INSERT, optarg) );
188           break;
189
190         case 'e':
191           requestedActions.push_back( make_pair(ERASE, optarg) );
192           break;
193
194         case 'r':
195           requestedActions.push_back( make_pair(REPLACE, optarg) );
196           break;
197
198         case '?':
199           break;
200
201         default:
202           abort ();
203         }
204     }
205
206   if ( optind == argc ) {
207     printHelp();
208     return 0;
209   }
210   
211   if (requestedActions.size() == 0)
212     requestedActions.push_back( make_pair(LIST, "") );
213         
214   for(int i = optind; i < argc; i++) {
215     cout << "******************** \"" << argv[i] << "\" ********************" << endl;
216     TagLib::FileRef f(argv[i]);
217
218     if(f.isNull())
219       continue;
220
221     TagLib::PropertyMap propmap = f.file()->properties();    
222     bool FCHANGED = false;
223     
224     for (actionqueue::iterator actit = requestedActions.begin(); actit != requestedActions.end(); ++actit) {
225       switch (actit->first) {
226       case LIST:
227         action_printTags(f);
228         break;
229         
230       case AUDIO:
231         action_printAudio(f);
232         break;
233         
234       case ERASE:
235         action_eraseTag(propmap, actit->second);
236         FCHANGED = true;
237         break;
238         
239       case REPLACE:
240         action_replaceTag(propmap, splitToTagPair(actit->second));
241         FCHANGED = true;
242         break;
243
244       case INSERT:
245         action_insertTag(propmap, splitToTagPair(actit->second));
246         FCHANGED = true;
247         break;
248       }
249     }
250
251     if (FCHANGED) {
252       f.file()->setProperties(propmap);
253       f.file()->save();
254     }
255   }
256   
257   return 0;
258 }