-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
187 lines (167 loc) · 5.35 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
* mp3chop
*
* (C) 2000-2020 Mike Crowe <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*/
#include "config.h"
#include <iostream>
#include "header.h"
#include "mp3_timecode.h"
#include "buffer.h"
#include "file_data_source.h"
#include "mp3_processor.h"
#include <stdio.h>
#include <stdlib.h>
// If we have GNU getopt_long then make sure we include its declaration.
#if defined(HAVE_GETOPT_LONG)
#if !defined(_GNU_SOURCE)
#define _GNU_SOURCE
#endif
#include <getopt.h>
#endif
#if defined(HAVE_GETOPT)
#include <getopt.h>
#endif
void Help(bool usage_info)
{
fprintf(stderr,
"mp3chop: A program for chopping out bits of ADTS MP3 and AAC files.\n"
"(C) 1999-2020 Mike Crowe, <[email protected]>\n"
"Version " VERSION "\n"
"\n"
"mp3chop is distributed with ABSOLUTELY NO WARRANTY under the terms\n"
"of the GNU General Public License Version 2.\n");
if (usage_info)
fprintf(stderr,
"\n"
"Usage: mp3chop [options] < infile > outfile\n"
"\n"
" --strip-id3=1, -s 1 String ID3 version 1 tags\n"
" --strip-id3=2, -s 2 String ID3 version 2 tags\n"
" --begin=<time>, -b <time> Do not output anything before <time>\n"
" --end=<time>, -e <time> Do not output anything after <time>\n"
" --help, -h Display this help\n"
" --dump-header, -d Dump the header of the first valid frame\n"
" --dump-all-headers -D Dump the headers of every frame\n"
" --copyright=0|1, -c 0|1 Force copyright flag\n"
" --original=0|1, -o 0|1 Force original flag\n"
"\n"
" <time> is of the form 'mm:ss.hh' mm=minutes, ss=seconds, hh=hundredths\n"
"\n");
}
class UnknownStripSpecificationException : public MP3Processor::Exception
{
virtual void Report()
{
std::cerr << "Unknown strip specification." << std::endl;
}
};
int main(int ac, char *av[])
{
#ifdef HAVE_GETOPT_LONG
static struct option long_options[] =
{
{ "begin", 1, NULL, 'b' },
{ "end", 1, NULL, 'e' },
{ "copyright", 1, NULL, 'c' },
{ "original", 1, NULL, 'o' },
{ "strip-id3", 1, NULL, 's' },
{ "help", 0, NULL, 'h' },
{ "dump-header", 0, NULL, 'd' },
{ "dump-all", 0, NULL, 'D' }, // for backward compatibility
{ "dump-all-headers", 0, NULL, 'D' },
{ "version", 0, NULL, 'V' },
{ 0, 0, 0, 0 }
};
#endif // HAVE_GETOPT_LONG
#if defined(HAVE_GETOPT) || defined(HAVE_GETOPT_LONG)
const char *short_options = "-b:e:c:o:s:hdDvV";
#endif // HAVE_GETOPT
MP3Processor processor;
processor.SetKeepID3V1(true);
processor.SetKeepID3V2(true);
try
{
while (optind < ac)
{
int option_index;
#ifdef HAVE_GETOPT_LONG
const int option = getopt_long(ac, av, short_options, long_options, &option_index);
#else
#ifdef HAVE_GETOPT
const int option = getopt(ac, av, short_options);
#else
#error Need getopt or getopt_long to work
#endif
#endif
switch (option)
{
case 1:
processor.HandleFile(optarg);
break;
case 'b':
processor.HandleBeginTimeCode(optarg);
break;
case 'e':
processor.HandleEndTimeCode(optarg);
break;
case 'c':
processor.HandleForceCopyright(atoi(optarg));
break;
case 'o':
processor.HandleForceOriginal(atoi(optarg));
break;
case 's':
if (strcmp(optarg, "1") == 0)
{
processor.SetKeepID3V1(false);
}
else if (strcmp(optarg, "2") == 0)
{
processor.SetKeepID3V2(false);
}
else
{
throw UnknownStripSpecificationException();
}
break;
case 'h':
Help(true);
return 0;
case 'V':
Help(false);
return 0;
case 'd':
processor.HandleMode('d');
break;
case 'D':
processor.HandleMode('D');
break;
case '?':
return 1;
}
}
processor.HandleEnd();
}
catch (MP3Processor::Exception &e)
{
e.Report();
return 1;
}
return 0;
}