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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
|
//
// HBSubtitles.m
// HandBrake
//
// Created by Damiano Galassi on 12/01/15.
//
//
#import "HBSubtitles.h"
#import "HBSubtitlesDefaults.h"
#import "HBTitle.h"
#import "NSCodingMacro.h"
#include "lang.h"
NSString *keySubTrackSelectionIndex = @"keySubTrackSelectionIndex";
NSString *keySubTrackName = @"keySubTrackName";
NSString *keySubTrackIndex = @"keySubTrackIndex";
NSString *keySubTrackLanguage = @"keySubTrackLanguage";
NSString *keySubTrackLanguageIsoCode = @"keySubTrackLanguageIsoCode";
NSString *keySubTrackType = @"keySubTrackType";
NSString *keySubTrackForced = @"keySubTrackForced";
NSString *keySubTrackBurned = @"keySubTrackBurned";
NSString *keySubTrackDefault = @"keySubTrackDefault";
NSString *keySubTrackSrtOffset = @"keySubTrackSrtOffset";
NSString *keySubTrackSrtFilePath = @"keySubTrackSrtFilePath";
NSString *keySubTrackSrtCharCode = @"keySubTrackSrtCharCode";
NSString *keySubTrackSrtCharCodeIndex = @"keySubTrackSrtCharCodeIndex";
NSString *keySubTrackLanguageIndex = @"keySubTrackLanguageIndex";
#define CHAR_CODE_DEFAULT_INDEX 11
@implementation HBSubtitles
- (instancetype)initWithTitle:(HBTitle *)title
{
self = [super init];
if (self)
{
_container = HB_MUX_MP4;
_tracks = [[NSMutableArray alloc] init];
_defaults = [[HBSubtitlesDefaults alloc] init];
_masterTrackArray = [title.subtitlesTracks mutableCopy];
NSMutableArray *forcedSourceNamesArray = [NSMutableArray array];
for (NSDictionary *dict in _masterTrackArray)
{
enum subsource source = [dict[keySubTrackType] intValue];
NSString *subSourceName = @(hb_subsource_name(source));
// if the subtitle track can be forced, add its source name to the array
if (hb_subtitle_can_force(source) && [forcedSourceNamesArray containsObject:subSourceName] == NO)
{
[forcedSourceNamesArray addObject:subSourceName];
}
}
// now set the name of the Foreign Audio Search track
if (forcedSourceNamesArray.count)
{
[forcedSourceNamesArray sortUsingComparator:^(id obj1, id obj2)
{
return [((NSString *)obj1) compare:((NSString *)obj2)];
}];
NSString *tempList = @"";
for (NSString *tempString in forcedSourceNamesArray)
{
if (tempList.length)
{
tempList = [tempList stringByAppendingString:@", "];
}
tempList = [tempList stringByAppendingString:tempString];
}
self.foreignAudioSearchTrackName = [NSString stringWithFormat:@"Foreign Audio Search (Bitmap) (%@)", tempList];
}
else
{
self.foreignAudioSearchTrackName = @"Foreign Audio Search (Bitmap)";
}
}
return self;
}
- (void)addAllTracks
{
[self.tracks removeAllObjects];
// Add the foreign audio search pass
[self addTrack:[self trackFromSourceTrackIndex:-1]];
// Add the remainings tracks
for (NSDictionary *track in self.masterTrackArray)
{
NSInteger sourceIndex = [track[keySubTrackIndex] integerValue];
[self addTrack:[self trackFromSourceTrackIndex:sourceIndex]];
}
[self.tracks addObject:[self createSubtitleTrack]];
[self validatePassthru];
}
- (void)removeAll
{
[self.tracks removeAllObjects];
[self.tracks addObject:[self createSubtitleTrack]];
}
- (void)reloadDefaults
{
[self addTracksFromDefaults];
}
// This gets called whenever the video container changes.
- (void)containerChanged:(int)container
{
self.container = container;
[self validatePassthru];
}
/**
* Convenience method to add a track to subtitlesArray.
* It calculates the keySubTrackSelectionIndex.
*
* @param track the track to add.
*/
- (void)addTrack:(NSMutableDictionary *)newTrack
{
newTrack[keySubTrackSelectionIndex] = @([newTrack[keySubTrackIndex] integerValue] + 1 + (self.tracks.count == 0));
[self insertObject:newTrack inTracksAtIndex:[self countOfTracks]];
}
/**
* Creates a new subtitle track.
*/
- (NSMutableDictionary *)createSubtitleTrack
{
NSMutableDictionary *newSubtitleTrack = [[NSMutableDictionary alloc] init];
newSubtitleTrack[keySubTrackIndex] = @(-2);
newSubtitleTrack[keySubTrackSelectionIndex] = @0;
newSubtitleTrack[keySubTrackName] = @"None";
newSubtitleTrack[keySubTrackForced] = @0;
newSubtitleTrack[keySubTrackBurned] = @0;
newSubtitleTrack[keySubTrackDefault] = @0;
return newSubtitleTrack;
}
/**
* Creates a new track dictionary from a source track.
*
* @param index the index of the source track in the subtitlesSourceArray,
* -1 means a Foreign Audio Search pass.
*
* @return a new mutable track dictionary.
*/
- (NSMutableDictionary *)trackFromSourceTrackIndex:(NSInteger)index
{
NSMutableDictionary *track = [self createSubtitleTrack];
if (index == -1)
{
/*
* we are foreign lang search, which is inherently bitmap
*
* since it can be either VOBSUB or PGS and the latter can't be
* passed through to MP4, we need to know whether there are any
* PGS tracks in the source - otherwise we can just set the
* source track type to VOBSUB
*/
int subtitleTrackType = VOBSUB;
if ([self.foreignAudioSearchTrackName rangeOfString:@(hb_subsource_name(PGSSUB))].location != NSNotFound)
{
subtitleTrackType = PGSSUB;
}
// Use -1 to indicate the foreign lang search
track[keySubTrackIndex] = @(-1);
track[keySubTrackName] = self.foreignAudioSearchTrackName;
track[keySubTrackType] = @(subtitleTrackType);
// foreign lang search is most useful when combined w/Forced Only - make it default
track[keySubTrackForced] = @1;
}
else
{
NSDictionary *sourceTrack = self.masterTrackArray[index];
track[keySubTrackIndex] = @(index);
track[keySubTrackName] = sourceTrack[keySubTrackName];
/* check to see if we are an srt, in which case set our file path and source track type kvp's*/
if ([self.masterTrackArray[index][keySubTrackType] intValue] == SRTSUB)
{
track[keySubTrackType] = @(SRTSUB);
track[keySubTrackSrtFilePath] = sourceTrack[keySubTrackSrtFilePath];
track[keySubTrackLanguageIndex] = @(self.languagesArrayDefIndex);
track[keySubTrackLanguageIsoCode] = self.languagesArray[self.languagesArrayDefIndex][1];
track[keySubTrackSrtCharCodeIndex] = @(CHAR_CODE_DEFAULT_INDEX);
track[keySubTrackSrtCharCode] = self.charCodeArray[CHAR_CODE_DEFAULT_INDEX];
}
else
{
track[keySubTrackType] = sourceTrack[keySubTrackType];
}
}
if (!hb_subtitle_can_burn([track[keySubTrackType] intValue]))
{
/* the source track cannot be burned in, so uncheck the widget */
track[keySubTrackBurned] = @0;
}
if (!hb_subtitle_can_force([track[keySubTrackType] intValue]))
{
/* the source track does not support forced flags, so uncheck the widget */
track[keySubTrackForced] = @0;
}
return track;
}
/**
* Remove all the subtitles tracks and
* add new ones based on the defaults settings
*/
- (IBAction)addTracksFromDefaults
{
// Keeps a set of the indexes of the added track
// so we don't add the same track twice.
NSMutableIndexSet *tracksAdded = [NSMutableIndexSet indexSet];
[self.tracks removeAllObjects];
// Add the foreign audio search pass
if (self.defaults.addForeignAudioSearch)
{
[self addTrack:[self trackFromSourceTrackIndex:-1]];
}
// Add the tracks for the selected languages
if (self.defaults.trackSelectionBehavior != HBSubtitleTrackSelectionBehaviorNone)
{
for (NSString *lang in self.defaults.trackSelectionLanguages)
{
for (NSDictionary *track in self.masterTrackArray)
{
if ([lang isEqualToString:@"und"] || [track[keySubTrackLanguageIsoCode] isEqualToString:lang])
{
NSInteger sourceIndex = [track[keySubTrackIndex] intValue];
if (![tracksAdded containsIndex:sourceIndex])
{
[self addTrack:[self trackFromSourceTrackIndex:sourceIndex]];
}
[tracksAdded addIndex:sourceIndex];
if (self.defaults.trackSelectionBehavior == HBSubtitleTrackSelectionBehaviorFirst)
{
break;
}
}
}
}
}
// Add the closed captions track if there is one.
if (self.defaults.addCC)
{
for (NSDictionary *track in self.masterTrackArray)
{
if ([track[keySubTrackType] intValue] == CC608SUB)
{
NSInteger sourceIndex = [track[keySubTrackIndex] intValue];
if (![tracksAdded containsIndex:sourceIndex])
{
[self addTrack:[self trackFromSourceTrackIndex:sourceIndex]];
}
if (self.defaults.trackSelectionBehavior == HBSubtitleTrackSelectionBehaviorFirst)
{
break;
}
}
}
}
// Set the burn key for the appropriate track.
if (self.defaults.burnInBehavior != HBSubtitleTrackBurnInBehaviorNone && self.tracks.count)
{
if (self.defaults.burnInBehavior == HBSubtitleTrackBurnInBehaviorFirst)
{
if ([self.tracks.firstObject[keySubTrackIndex] integerValue] != -1)
{
self.tracks.firstObject[keySubTrackBurned] = @YES;
}
else if (self.tracks.count > 1)
{
self.tracks[1][keySubTrackBurned] = @YES;
}
}
else if (self.defaults.burnInBehavior == HBSubtitleTrackBurnInBehaviorForeignAudio)
{
if ([self.tracks.firstObject[keySubTrackIndex] integerValue] == -1)
{
self.tracks.firstObject[keySubTrackBurned] = @YES;
}
}
else if (self.defaults.burnInBehavior == HBSubtitleTrackBurnInBehaviorForeignAudioThenFirst)
{
self.tracks.firstObject[keySubTrackBurned] = @YES;
}
}
// Burn-in the first dvd or bluray track and remove the others.
if (self.defaults.burnInDVDSubtitles || self.defaults.burnInBluraySubtitles)
{
// Ugly settings for ugly players
BOOL bitmapSubtitlesFound = NO;
NSMutableArray *tracksToDelete = [[NSMutableArray alloc] init];
for (NSMutableDictionary *track in self.tracks)
{
if ([track[keySubTrackIndex] integerValue] != -1)
{
if ((([track[keySubTrackType] intValue] == VOBSUB && self.defaults.burnInDVDSubtitles) ||
([track[keySubTrackType] intValue] == PGSSUB && self.defaults.burnInBluraySubtitles)) &&
!bitmapSubtitlesFound)
{
track[keySubTrackBurned] = @YES;
bitmapSubtitlesFound = YES;
}
else if ([track[keySubTrackType] intValue] == VOBSUB || [track[keySubTrackType] intValue] == PGSSUB)
{
[tracksToDelete addObject:track];
}
}
}
[self.tracks removeObjectsInArray:tracksToDelete];
}
// Add an empty track
[self insertObject:[self createSubtitleTrack] inTracksAtIndex:[self countOfTracks]];
[self validatePassthru];
}
/**
* Checks whether any subtitles in the list cannot be passed through.
* Set the first of any such subtitles to burned-in, remove the others.
*/
- (void)validatePassthru
{
BOOL convertToBurnInUsed = NO;
NSMutableArray *tracksToDelete = [[NSMutableArray alloc] init];
// convert any non-None incompatible tracks to burn-in or remove them
for (NSMutableDictionary *track in self.tracks)
{
if (track[keySubTrackType] == nil)
{
continue;
}
int subtitleTrackType = [track[keySubTrackType] intValue];
if (!hb_subtitle_can_pass(subtitleTrackType, self.container))
{
if (convertToBurnInUsed == NO)
{
//we haven't set any track to burned-in yet, so we can
track[keySubTrackBurned] = @1;
convertToBurnInUsed = YES; //remove any additional tracks
}
else
{
//we already have a burned-in track, we must remove others
[tracksToDelete addObject:track];
}
}
}
//if we converted a track to burned-in, unset it for tracks that support passthru
if (convertToBurnInUsed == YES)
{
for (NSMutableDictionary *track in self.tracks)
{
if (track[keySubTrackType] == nil)
{
continue;
}
int subtitleTrackType = [track[keySubTrackType] intValue];
if (hb_subtitle_can_pass(subtitleTrackType, self.container))
{
track[keySubTrackBurned] = @0;
}
}
}
[self willChangeValueForKey:@"tracks"];
if (tracksToDelete.count)
{
[self.tracks removeObjectsInArray:tracksToDelete];
}
[self didChangeValueForKey:@"tracks"];
}
#pragma mark - Languages
@synthesize languagesArray = _languagesArray;
- (NSArray *)languagesArray
{
if (!_languagesArray)
{
_languagesArray = [self populateLanguageArray];
}
return _languagesArray;
}
- (NSArray *)populateLanguageArray
{
NSMutableArray *languages = [[NSMutableArray alloc] init];
for (const iso639_lang_t * lang = lang_get_next(NULL); lang != NULL; lang = lang_get_next(lang))
{
[languages addObject:@[@(lang->eng_name),
@(lang->iso639_2)]];
if (!strcasecmp(lang->eng_name, "English"))
{
_languagesArrayDefIndex = [languages count] - 1;
}
}
return [languages copy];
}
@synthesize charCodeArray = _charCodeArray;
- (NSArray *)charCodeArray
{
if (!_charCodeArray)
{
// populate the charCodeArray.
_charCodeArray = @[@"ANSI_X3.4-1968", @"ANSI_X3.4-1986", @"ANSI_X3.4", @"ANSI_X3.110-1983", @"ANSI_X3.110", @"ASCII",
@"ECMA-114", @"ECMA-118", @"ECMA-128", @"ECMA-CYRILLIC", @"IEC_P27-1", @"ISO-8859-1", @"ISO-8859-2",
@"ISO-8859-3", @"ISO-8859-4", @"ISO-8859-5", @"ISO-8859-6", @"ISO-8859-7", @"ISO-8859-8", @"ISO-8859-9",
@"ISO-8859-9E", @"ISO-8859-10", @"ISO-8859-11", @"ISO-8859-13", @"ISO-8859-14", @"ISO-8859-15", @"ISO-8859-16",
@"UTF-7", @"UTF-8", @"UTF-16", @"UTF-16LE", @"UTF-16BE", @"UTF-32", @"UTF-32LE", @"UTF-32BE"];
}
return _charCodeArray;
}
#pragma mark - NSCopying
- (instancetype)copyWithZone:(NSZone *)zone
{
HBSubtitles *copy = [[[self class] alloc] init];
if (copy)
{
copy->_container = _container;
copy->_masterTrackArray = [_masterTrackArray mutableCopy];
copy->_foreignAudioSearchTrackName = [_foreignAudioSearchTrackName copy];
copy->_tracks = [[NSMutableArray alloc] init];
[_tracks enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if (idx < _tracks.count)
{
NSMutableDictionary *trackCopy = [obj copy];
[copy->_tracks addObject:trackCopy];
}
}];
copy->_defaults = [_defaults copy];
}
return copy;
}
#pragma mark - NSCoding
- (void)encodeWithCoder:(NSCoder *)coder
{
[coder encodeInt:1 forKey:@"HBAudioVersion"];
encodeInt(_container);
encodeObject(_masterTrackArray);
encodeObject(_foreignAudioSearchTrackName);
encodeObject(_tracks);
encodeObject(_defaults);
}
- (id)initWithCoder:(NSCoder *)decoder
{
self = [super init];
decodeInt(_container);
decodeObject(_masterTrackArray);
decodeObject(_foreignAudioSearchTrackName);
decodeObject(_tracks);
decodeObject(_defaults);
return self;
}
#pragma mark - Presets
- (void)writeToPreset:(NSMutableDictionary *)preset
{
[self.defaults writeToPreset:preset];
}
- (void)applyPreset:(NSDictionary *)preset
{
[self.defaults applyPreset:preset];
[self addTracksFromDefaults];
}
#pragma mark -
#pragma mark KVC
- (NSUInteger) countOfTracks
{
return self.tracks.count;
}
- (id)objectInTracksAtIndex:(NSUInteger)index
{
return self.tracks[index];
}
- (void)insertObject:(id)track inTracksAtIndex:(NSUInteger)index;
{
[self.tracks insertObject:track atIndex:index];
}
- (void)removeObjectFromTracksAtIndex:(NSUInteger)index
{
[self.tracks removeObjectAtIndex:index];
}
@end
|