-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSublerCLI.mm
executable file
·513 lines (444 loc) · 18.5 KB
/
SublerCLI.mm
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
//
// SublerCLI.mm
// Subler
//
// Copyright 2009-2013 Damiano Galassi. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "MP42File.h"
#import "MP42FileImporter.h"
#import "RegexKitLite.h"
void print_help() {
printf("usage:\n");
printf("\t -dest <destination file> \n");
printf("\t -dest options:\n");
printf("\t\t -chapters <chapters file> \n");
printf("\t\t -chapterspreview Create chapters preview images \n");
printf("\t\t -remove Remove existing subtitles \n");
printf("\t\t -optimize Optimize \n");
printf("\t\t -metadata {Tag Name:Tag Value} \n");
printf("\t\t -removemetadata remove all the tags \n");
printf("\t\t -organizegroups enable tracks and create altenate groups in the iTunes friendly way\n");
printf("\t\t -vprofile sets the video profile to any of <baseline, main, [high]>\n");
printf("\t\t -vlevel sets the video level <21, 31, [41]>\n");
printf("\n");
printf("\t -source <source file> \n");
printf("\t -source options:\n");
printf("\t\t -listtracks For source file only, lists the tracks in the source movie. \n");
printf("\t\t -listmetadata For source file only, lists the metadata in the source movie. \n");
printf("\n");
printf("\t\t -delay Delay in ms \n");
printf("\t\t -height Height in pixel \n");
printf("\t\t -language Track language (i.e. English) \n");
printf("\t\t -downmix Downmix audio (mono, stereo, dolby, pl2) \n");
printf("\t\t -64bitchunk 64bit file (only when -dest isn't an existing file) \n");
printf("\n");
printf("\t -help Print this help information \n");
printf("\t -version Print version \n");
}
void print_version() {
printf("\t\tversion 0.26\n");
}
int main (int argc, const char * argv[]) {
@autoreleasepool {
NSString *destinationPath = nil;
NSString *sourcePath = nil;
NSString *chaptersPath = nil;
NSString *metadata = nil;
NSString *language = NULL;
int delay = 0;
unsigned int height = 0;
BOOL removeExisting = false;
BOOL chapterPreview = false;
BOOL modified = false;
BOOL optimize = false;
BOOL listtracks = false;
BOOL listmetadata = false;
BOOL removemetadata = false;
BOOL organizegroups = NO;
BOOL _64bitchunk = NO;
BOOL downmixAudio = NO;
NSString *downmixArg = nil;
NSString *downmixType = nil;
unsigned int videoprofile = 100; //high
unsigned int videolevel = 41; //4.1
if (argc == 1) {
print_help();
exit(-1);
}
argv += 1;
argc--;
// Handle args
while (argc > 0 && **argv == '-') {
const char *args = &(*argv)[1];
argc--;
argv++;
if ( ! strcmp ( args, "source" ) )
{
sourcePath = @(*argv++);
argc--;
}
else if (( ! strcmp ( args, "dest" )) || ( ! strcmp ( args, "destination" )) )
{
destinationPath = @(*argv++);
argc--;
}
else if ( ! strcmp ( args, "chapters" ) )
{
chaptersPath = @(*argv++);
argc--;
}
else if ( ! strcmp ( args, "chapterspreview" ) )
{
chapterPreview = YES;
}
else if ( ! strcmp ( args, "metadata" ) )
{
metadata = @(*argv++);
argc--;
}
else if ( ! strcmp ( args, "optimize" ) )
{
optimize = YES;
}
else if ( ! strcmp ( args, "downmix" ) )
{
downmixAudio = YES;
downmixArg = @(*argv++);
if(![downmixArg caseInsensitiveCompare:@"mono"]) downmixType = SBMonoMixdown;
else if(![downmixArg caseInsensitiveCompare:@"stereo"]) downmixType = SBStereoMixdown;
else if(![downmixArg caseInsensitiveCompare:@"dolby"]) downmixType = SBDolbyMixdown;
else if(![downmixArg caseInsensitiveCompare:@"pl2"]) downmixType = SBDolbyPlIIMixdown;
else {
printf( "Error: unsupported downmix type '%s'\n", optarg );
printf( "Valid downmix types are: 'mono', 'stereo', 'dolby' and 'pl2'\n" );
exit( -1 );
}
argc--;
}
else if ( ! strcmp ( args, "64bitchunk" ) )
{
_64bitchunk = YES;
}
else if ( ! strcmp ( args, "delay" ) )
{
delay = atoi(*argv++);
argc--;
}
else if ( ! strcmp ( args, "height" ) )
{
height = atoi(*argv++);
argc--;
}
else if ( ! strcmp ( args, "language" ) )
{
language = @(*argv++);
argc--;
}
else if ( ! strcmp ( args, "remove" ) )
{
removeExisting = YES;
}
else if (( ! strcmp ( args, "version" )) || ( ! strcmp ( args, "v" )) )
{
print_version();
}
else if ( ! strcmp ( args, "help" ) )
{
print_help();
}
else if ( ! strcmp ( args, "listtracks" ) )
{
listtracks = YES;
}
else if ( ! strcmp ( args, "listmetadata" ) )
{
listmetadata = YES;
}
else if ( ! strcmp ( args, "removemetadata" ) )
{
removemetadata = YES;
}
else if ( ! strcmp ( args, "organizegroups" ) )
{
organizegroups = YES;
}
else if ( ! strcmp ( args, "vprofile" ) )
{
if (argc) {
const char *arg = *argv++;
if (!strcmp(arg, "baseline")) videoprofile = 66;
else if(!strcmp(arg, "main")) videoprofile = 77;
else if(!strcmp(arg, "high")) videoprofile = 100;
else {
printf( "Error: unsupported profile '%s'\n", arg );
printf( "Valid profiles are: 'baseline', 'main' and 'high'\n" );
exit( -1 );
}
argc--;
}
else {
print_help();
exit(-1);
}
}
else if ( ! strcmp ( args, "vlevel" ) )
{
if (argc) {
videolevel = atoi(*argv++);
argc--;
}
else {
print_help();
exit(-1);
}
}
else {
printf("Invalid input parameter: %s\n", args );
print_help();
return 1;
}
}
// Don't let the user mux a file to the file itself
if ([sourcePath isEqualToString:destinationPath]) {
printf("The destination path need to be different from the source path\n");
exit(1);
}
// Lists tracks or metadatq, works with -source
if (sourcePath && (listtracks || listmetadata)) {
MP42File *mp4File = nil;
if ([[NSFileManager defaultManager] fileExistsAtPath:sourcePath]) {
mp4File = [[MP42File alloc] initWithExistingFile:[NSURL fileURLWithPath:sourcePath]
andDelegate:nil];
}
if (!mp4File) {
printf("Error: %s\n", "the mp4 file couln't be opened.");
return -1;
}
if (listtracks) {
for (MP42Track* track in mp4File.tracks) {
printf("%s\n", [[track description] UTF8String]);
}
}
if (listmetadata) {
NSArray * availableMetadata = [[mp4File metadata] availableMetadata];
NSDictionary * tagsDict = [[mp4File metadata] tagsDict];
for (NSString* key in availableMetadata) {
NSString* tag = [tagsDict valueForKey:key];
if (tag) {
if ([tag isKindOfClass:[NSString class]])
printf("%s: %s\n", [key UTF8String], [tag UTF8String]);
if ([tag isKindOfClass:[NSNumber class]])
printf("%s: %ld\n", [key UTF8String], (long)[tag integerValue]);
}
}
if ([[mp4File metadata] hdVideo]) {
printf("HD Video: %d\n", [[mp4File metadata] hdVideo]);
}
if ([[mp4File metadata] gapless]) {
printf("Gapless: %d\n", [[mp4File metadata] gapless]);
}
if ([[mp4File metadata] contentRating]) {
printf("Content Rating: %d\n", [[mp4File metadata] contentRating]);
}
if ([[mp4File metadata] podcast]) {
printf("Podcast: %d\n", [[mp4File metadata] podcast]);
}
if ([[mp4File metadata] mediaKind]) {
printf("Media Kind: %d\n", [[mp4File metadata] mediaKind]);
}
}
return 0;
}
// Other options, works with -dest
if ((sourcePath && [[NSFileManager defaultManager] fileExistsAtPath:sourcePath]) ||
organizegroups || chaptersPath || removeExisting || metadata || chapterPreview || removemetadata) {
NSError *outError = nil;
MP42File *mp4File = nil;
NSMutableDictionary *attributes = [[[NSMutableDictionary alloc] init] autorelease];
if ([[NSFileManager defaultManager] fileExistsAtPath:destinationPath]) {
mp4File = [[MP42File alloc] initWithExistingFile:[NSURL fileURLWithPath:destinationPath]
andDelegate:nil];
} else {
mp4File = [[MP42File alloc] initWithDelegate:nil];
}
if (!mp4File) {
printf("Error: %s\n", "the mp4 file couln't be opened.");
return -1;
}
// Remove Metadata
if (removemetadata) {
for (NSString* key in mp4File.metadata.writableMetadata) {
[mp4File.metadata removeTagForKey:key];
}
if ([[mp4File metadata] hdVideo]) {
mp4File.metadata.hdVideo = 0;
}
if ([[mp4File metadata] gapless]) {
mp4File.metadata.gapless = 0;
}
if ([[mp4File metadata] contentRating]) {
mp4File.metadata.contentRating = 0;
}
if ([[mp4File metadata] podcast]) {
mp4File.metadata.podcast = 0;
}
if ([[mp4File metadata] mediaKind]) {
mp4File.metadata.mediaKind = 0;
}
modified = YES;
}
// Remove subtitles tracks
if (removeExisting) {
NSMutableIndexSet *subtitleTrackIndexes = [[NSMutableIndexSet alloc] init];
MP42Track *track;
for (track in mp4File.tracks)
if ([track isMemberOfClass:[MP42SubtitleTrack class]]) {
[subtitleTrackIndexes addIndex:[mp4File.tracks indexOfObject:track]];
modified = YES;
}
[mp4File removeTracksAtIndexes:subtitleTrackIndexes];
[subtitleTrackIndexes release];
}
// Import source
if ((sourcePath && [[NSFileManager defaultManager] fileExistsAtPath:sourcePath])) {
NSURL *sourceURL = [NSURL fileURLWithPath:sourcePath];
MP42FileImporter *fileImporter = [[MP42FileImporter alloc] initWithURL:sourceURL
error:&outError];
for (MP42Track *track in fileImporter.tracks) {
if (language) {
track.language = language;
}
if (delay) {
track.startOffset = delay;
}
if (height && [track isMemberOfClass:[MP42SubtitleTrack class]]) {
[(MP42VideoTrack *)track setTrackHeight:height];
}
[mp4File addTrack:track];
modified = YES;
}
[fileImporter release];
}
// Import chapters
if (chaptersPath) {
MP42Track *oldChapterTrack = NULL;
MP42ChapterTrack *newChapterTrack = NULL;
MP42Track *track;
for (track in mp4File.tracks)
if ([track isMemberOfClass:[MP42ChapterTrack class]]) {
oldChapterTrack = track;
break;
}
if (oldChapterTrack != NULL) {
[mp4File removeTrackAtIndex:[mp4File.tracks indexOfObject:oldChapterTrack]];
modified = YES;
}
newChapterTrack = [MP42ChapterTrack chapterTrackFromFile:[NSURL fileURLWithPath:chaptersPath]];
if([newChapterTrack chapterCount] > 0) {
[mp4File addTrack:newChapterTrack];
modified = YES;
}
}
// Select audio dowmix
if (downmixAudio) {
for (MP42AudioTrack *track in [mp4File tracks]) {
if (![track isKindOfClass: [MP42AudioTrack class]]) continue;
[track setNeedConversion: YES];
[track setMixdownType: downmixType];
modified = YES;
}
}
// Set H.264 video profile
for (MP42VideoTrack *track in [mp4File tracks]) {
if ([track isKindOfClass: [MP42VideoTrack class]] && [track.format isEqualToString:@"H.264"]) {
track.newProfile = videoprofile;
track.newLevel = videolevel;
track.isEdited = YES;
modified = YES;
}
}
// Set metadata
if (metadata) {
NSString *searchString = metadata;
NSString *regexCheck = @"(\\{[^:]*:[^\\}]*\\})*";
// escaping the {, } and : charachters
NSString *left_normal = @"{";
NSString *right_normal = @"}";
NSString *semicolon_normal = @":";
NSString *left_escaped = @"{";
NSString *right_escaped = @"}";
NSString *semicolon_escaped = @":";
if (searchString != nil && [searchString isMatchedByRegex:regexCheck]) {
NSString *regexSplitArgs = @"^\\{|\\}\\{|\\}$";
NSString *regexSplitValue = @"([^:]*):(.*)";
NSArray *argsArray = nil;
NSString *arg = nil;
NSString *key = nil;
NSString *value = nil;
argsArray = [searchString componentsSeparatedByRegex:regexSplitArgs];
for (arg in argsArray) {
key = [arg stringByMatching:regexSplitValue capture:1L];
value = [arg stringByMatching:regexSplitValue capture:2L];
key = [key stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
value = [value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
value = [value stringByReplacingOccurrencesOfString:left_escaped withString:left_normal];
value = [value stringByReplacingOccurrencesOfString:right_escaped withString:right_normal];
value = [value stringByReplacingOccurrencesOfString:semicolon_escaped withString:semicolon_normal];
if (key != nil) {
if (value != nil && [value length] > 0) {
[mp4File.metadata setTag:value forKey:key];
} else {
[mp4File.metadata removeTagForKey:key];
}
modified = YES;
}
}
}
}
// Set chapters preview
if (chapterPreview) {
[attributes setObject:@YES forKey:MP42GenerateChaptersPreviewTrack];
modified = YES;
}
// Organize groups
if (organizegroups) {
[mp4File organizeAlternateGroups];
modified = YES;
}
BOOL success = NO;
if (modified && [mp4File hasFileRepresentation]) {
success = [mp4File updateMP4FileWithAttributes:attributes error:&outError];
} else if (destinationPath) {
if ([mp4File dataSize] > 4100000000 || _64bitchunk) {
[attributes setObject:@YES forKey:MP4264BitData];
}
success = [mp4File writeToUrl:[NSURL fileURLWithPath:destinationPath]
withAttributes:attributes
error:&outError];
}
if (!success) {
if (outError) {
printf("Error: %s\n", [[outError localizedDescription] UTF8String]);
}
return -1;
}
[mp4File release];
}
// -optimize option
if (optimize) {
MP42File *mp4File;
mp4File = [[MP42File alloc] initWithExistingFile:[NSURL fileURLWithPath:destinationPath]
andDelegate:nil];
if (!mp4File) {
printf("Error: %s\n", "the mp4 file couln't be opened.");
return -1;
}
printf("Optimizing...\n");
[mp4File optimize];
[mp4File release];
printf("Done.\n");
}
}
return 0;
}