forked from gonzoua/AudioBookBinder
-
Notifications
You must be signed in to change notification settings - Fork 1
/
AudioBookBinder.m
182 lines (163 loc) · 5.81 KB
/
AudioBookBinder.m
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
//
// Copyright (c) 2009, Oleksandr Tymoshenko <[email protected]>
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
// notice unmodified, this list of conditions, and the following
// disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
// SUCH DAMAGE.
//
#import <Foundation/Foundation.h>
#import <Foundation/NSDebug.h>
#import <QTKit/QTKit.h>
#include <getopt.h>
#import "AudioBinder.h"
#import "ABLog.h"
#import "ConsoleDelegate.h"
#import "MP4File.h"
void usage(char *cmd)
{
printf("Usage: %s [-hsv] [-a author] [-t title] [-i filelist] outfile [infile ...]\n", cmd);
printf("\t-a author\tset book author\n");
printf("\t-h\t\tshow this message\n");
printf("\t-i file\t\tget input files list from file, \"-\" for standard input\n");
printf("\t-s\t\tskip errors and go on with conversion\n");
printf("\t-t title\tset book title\n");
printf("\t-v\t\tprint some info on files being converted\n");
}
int main (int argc, char * argv[]) {
int c;
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
AudioBinder *binder = [[AudioBinder alloc] init];
NSString *bookAuthor = nil;
NSString *bookTitle = nil;
NSString *outFile = nil;
NSString *inputFileList = nil;
NSMutableArray *inputFiles;
NSError *error;
ConsoleDelegate *delegate;
BOOL verbose = NO;
BOOL skipErrors = NO;
NSZombieEnabled = YES;
while ((c = getopt(argc, argv, "a:hi:st:v")) != -1) {
switch (c) {
case 'h':
usage(argv[0]);
exit(0);
case 'a':
bookAuthor = [NSString stringWithUTF8String:optarg];
break;
case 't':
bookTitle = [NSString stringWithUTF8String:optarg];
break;
case 'i':
inputFileList = [NSString stringWithUTF8String:optarg];
break;
case 'v':
verbose = YES;
break;
case 's':
skipErrors = YES;
break;
default:
usage(argv[0]);
break;
}
}
// Do we have output file et al?
if (optind < argc)
{
outFile = [NSString stringWithUTF8String:argv[optind]];
optind++;
}
else
{
fprintf(stderr, "No output file specified\n");
usage(argv[0]);
exit(1);
}
// Get input files from all possible sources:
//
inputFiles = [[NSMutableArray alloc] init];
if (inputFileList != nil)
{
// Add files from the list file
NSString *listContent;
if ([inputFileList isEqualToString:@"-"])
{
NSData *data =
[[NSFileHandle fileHandleWithStandardInput] readDataToEndOfFile];
listContent = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
}
else
listContent = [[NSString alloc]
initWithContentsOfFile:inputFileList
encoding:NSUTF8StringEncoding
error:&error];
if (listContent == nil) {
ABLog(@"Error reading file at %@\n%@",
inputFileList, [error localizedFailureReason]);
usage(argv[0]);
exit(1);
}
NSArray *filesList = [listContent componentsSeparatedByString: @"\n"];
for (NSString *file in filesList)
if ([file length] > 0)
[inputFiles addObject:file];
}
// Now get input files from the remain of arguments
while (optind < argc)
{
[inputFiles addObject:[NSString stringWithUTF8String:argv[optind]]];
optind++;
}
if ([inputFiles count] == 0)
{
fprintf(stderr, "No input file specified\n");
usage(argv[0]);
exit(1);
}
// Feed files to binder
[binder setOutputFile:outFile];
for (NSString *file in inputFiles)
[binder addInputFile:file];
// Setup delegate, it will print progress messages on console
delegate = [[ConsoleDelegate alloc] init];
[delegate setVerbose:verbose];
[delegate setSkipErrors:skipErrors];
[binder setDelegate:delegate];
if (![binder convert])
{
ABLog(@"Conversion failed");
exit(255);
}
if ((bookAuthor != nil) || (bookTitle != nil))
{
printf("Adding metadata, it may take a while...");
MP4File *mp4 = [[MP4File alloc] initWithFileName:outFile];
[mp4 setArtist:bookAuthor];
[mp4 setTitle:bookTitle];
[mp4 updateFile];
printf("done\n");
}
[pool drain];
return 0;
}