]> git.treefish.org Git - usetaglib.git/blob - usetaglib.cpp
7909009ca1895dc40cc37fd408b82129876317ab
[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 (const TagLib::FileRef f, const string &key)
58 {
59   TagLib::PropertyMap propmap = f.file()->properties();
60   propmap.erase(key);
61   f.file()->setProperties(propmap);
62 }
63
64 void action_replaceTag (const TagLib::FileRef f, const tagpair &tagPair)
65 {
66   TagLib::PropertyMap propmap = f.file()->properties();
67   propmap.replace(tagPair.first, argToStringList(tagPair.second));
68   f.file()->setProperties(propmap);
69 }
70
71 void action_insertTag (const TagLib::FileRef f, const tagpair &tagPair)
72 {
73   TagLib::PropertyMap propmap = f.file()->properties();
74   propmap.insert(tagPair.first, argToStringList(tagPair.second));
75   f.file()->setProperties(propmap);
76 }
77
78 int action_printTags (const TagLib::FileRef f)
79 {
80   if(f.tag()) {
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();
86       }
87     }
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;
92       }
93     }
94     return 0;
95   }
96   else
97     return 1;
98 }
99
100 int action_printAudio (const TagLib::FileRef f)
101 {
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;
111     return 0;
112   }
113   else
114     return 1;
115 }
116
117 void printHelp ()
118 {
119   cout << "Usage: usetaglib [ACTION]... [FILE]..." << endl;
120   cout << "List and edit tags on mediafiles in formats supported by libtag." << endl;
121   cout << endl;
122   cout << "-h, --help   Show this help" << endl;
123   cout << endl;
124   cout << "ACTIONS" << endl;
125   cout << setfill(' ') << setw(37) << left << "  -l, --list"
126        << "list all tags (implicit if no action specified)"<< endl;
127   cout << setfill(' ') << setw(37) << left << "  -a, --listaudio"
128        << "show audio information"<< endl;
129   cout << setfill(' ') << setw(37) << left << "  -e, --erase=TAGNAME"
130        << "erase tag TAGNAME"<< endl;
131   cout << setfill(' ') << setw(37) << left << "  -r, --replace=TAGNAME=TAGVALSPEC"
132        << "replace tag TAGNAME with value TAGVALSPEC"<< endl;
133   cout << setfill(' ') << setw(37) << left << "  -i, --insert=TAGNAME=TAGVALSPEC"
134        << "insert value TAGVALSPEC for tag TAGNAME"<< endl;
135   cout << endl;
136   cout << "TAGVALSPEC" << endl;
137   cout << "  a list of values separated by '=': val1=val2=val3..." << endl;
138   cout << endl;
139   cout << "EXAMPLES" << endl;
140   cout << "  usetaglib file.ogg" << endl;
141   cout << "  usetaglib -e ALBUM file.flac" << endl;
142   cout << "  usetaglib -i ARTIST=Horst=Hubert file.mp3" << endl;
143   cout << "  usetaglib -r ARTIST=Horst file.ogg" << endl;
144 }
145   
146 int main(int argc, char *argv[])
147 {
148   int c;
149   actionqueue requestedActions;
150   
151   while (1)
152     {
153       static struct option long_options[] =
154         {
155           {"help",      no_argument,       0, 'h'},
156           {"list",      no_argument,       0, 'l'},
157           {"listaudio", no_argument,       0, 'a'},
158           {"insert",    required_argument, 0, 'i'},
159           {"erase",     required_argument, 0, 'e'},
160           {"replace",   required_argument, 0, 'r'},
161           {0, 0, 0, 0}
162         };
163
164       int option_index = 0;      
165       c = getopt_long (argc, argv, "hlai:e:r:",
166                        long_options, &option_index);
167
168       if (c == -1)
169         break;
170
171       switch (c)
172         {
173         case 0:
174           if (long_options[option_index].flag != 0)
175             break;
176
177         case 'h':
178           printHelp();
179           return 0;
180           break;
181           
182         case 'l':
183           requestedActions.push_back( make_pair(LIST, "") );
184           break;
185
186         case 'a':
187           requestedActions.push_back( make_pair(AUDIO, "") );
188           break;
189
190         case 'i':
191           requestedActions.push_back( make_pair(INSERT, optarg) );
192           break;
193
194         case 'e':
195           requestedActions.push_back( make_pair(ERASE, optarg) );
196           break;
197
198         case 'r':
199           requestedActions.push_back( make_pair(REPLACE, optarg) );
200           break;
201
202         case '?':
203           break;
204
205         default:
206           abort ();
207         }
208     }
209
210   if ( optind == argc ) {
211     printHelp();
212     return 0;
213   }
214   
215   if (requestedActions.size() == 0)
216     requestedActions.push_back( make_pair(LIST, "") );
217         
218   for(int i = optind; i < argc; i++) {
219     cout << "******************** \"" << argv[i] << "\" ********************" << endl;
220     TagLib::FileRef f(argv[i]);
221
222     if(f.isNull())
223       continue;
224     
225     for (actionqueue::iterator actit = requestedActions.begin(); actit != requestedActions.end(); ++actit) {
226       switch (actit->first) {
227       case LIST:
228         action_printTags(f);
229         break;
230         
231       case AUDIO:
232         action_printAudio(f);
233         break;
234         
235       case ERASE:
236         action_eraseTag(f, actit->second);
237         break;
238         
239       case REPLACE:
240         action_replaceTag(f, splitToTagPair(actit->second));
241         break;
242
243       case INSERT:
244         action_insertTag(f, splitToTagPair(actit->second));
245         break;
246       }
247     }
248     
249     f.file()->save();
250   }
251   
252   return 0;
253 }