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
|
/* HBTitle.m $
This file is part of the HandBrake source code.
Homepage: <http://handbrake.fr/>.
It may be used under the terms of the GNU General Public License. */
#import "HBTitle.h"
#import "HBTitle+Private.h"
#import "HBChapter.h"
#import "HBPreset.h"
#import "NSDictionary+HBAdditions.h"
#import "HBLocalizationUtilities.h"
#import "HBCodingUtilities.h"
#import "HBSecurityAccessToken.h"
#import "HBUtilities.h"
#include "handbrake/lang.h"
@interface HBMetadata ()
@property (nonatomic, readonly) hb_metadata_t *hb_metadata;
@property (nonatomic, copy) NSString *releaseDate;
@end
@implementation HBMetadata
- (instancetype)initWithMetadata:(hb_metadata_t *)data
{
self = [super init];
if (self)
{
_hb_metadata = data;
}
return self;
}
- (NSString *)releaseDate
{
if (self.hb_metadata->release_date == nil)
{
return nil;
}
return @(self.hb_metadata->release_date);
}
@end
@implementation HBTitleAudioTrack
- (instancetype)initWithDisplayName:(NSString *)displayName
{
self = [super init];
if (self)
{
_displayName = [displayName copy];
_title = @"";
_isoLanguageCode = @"";
}
return self;
}
- (instancetype)initWithAudioTrack:(hb_audio_config_t *)audio index:(int)index
{
self = [super init];
if (self)
{
_displayName = [NSString stringWithFormat: @"%d: %@", index, @(audio->lang.description)];
_title = audio->in.name ? @(audio->in.name) : nil;
_bitRate = audio->in.bitrate / 1000;
_sampleRate = audio->in.samplerate;
_codec = audio->in.codec;
_codecParam = audio->in.codec_param;
_channelLayout = audio->in.channel_layout;
_isoLanguageCode = @(audio->lang.iso639_2);
}
return self;
}
+ (BOOL)supportsSecureCoding { return YES; }
- (void)encodeWithCoder:(nonnull NSCoder *)coder
{
encodeObject(_displayName);
encodeObject(_title);
encodeInt(_bitRate);
encodeInt(_sampleRate);
encodeInt(_codec);
encodeInt(_codecParam);
encodeInteger(_channelLayout);
encodeObject(_isoLanguageCode);
}
- (nullable instancetype)initWithCoder:(nonnull NSCoder *)decoder
{
self = [super init];
if (self)
{
decodeObjectOrFail(_displayName, NSString);
decodeObject(_title, NSString);
decodeInt(_bitRate);
decodeInt(_sampleRate);
decodeInt(_codec);
decodeInt(_codecParam);
decodeInteger(_channelLayout);
decodeObjectOrFail(_isoLanguageCode, NSString);
}
return self;
fail:
return nil;
}
@end
@interface HBTitleSubtitlesTrack ()
@property (nonatomic, readonly, nullable) NSData *bookmark;
@property (nonatomic, readwrite) NSInteger accessCount;
@property (nonatomic, readwrite) HBSecurityAccessToken *fileURLToken;
@end
@implementation HBTitleSubtitlesTrack
- (instancetype)initWithDisplayName:(NSString *)displayName type:(int)type fileURL:(nullable NSURL *)fileURL
{
self = [super init];
if (self)
{
_displayName = [displayName copy];
_type = type;
_isoLanguageCode = @"und";
_fileURL = fileURL;
}
return self;
}
- (instancetype)initWithSubtitlesTrack:(hb_subtitle_t *)subtitle index:(int)index
{
self = [super init];
if (self)
{
_displayName = [NSString stringWithFormat:@"%d: %@", index, @(subtitle->lang)];
_title = subtitle->name ? @(subtitle->name) : nil;
_type = subtitle->source;
_isoLanguageCode = @(subtitle->iso639_2);
}
return self;
}
- (void)refreshSecurityScopedResources
{
if (_bookmark)
{
NSURL *resolvedURL = [HBUtilities URLFromBookmark:_bookmark];
if (resolvedURL)
{
_fileURL = resolvedURL;
}
}
}
- (BOOL)startAccessingSecurityScopedResource
{
#ifdef __SANDBOX_ENABLED__
if (self.accessCount == 0)
{
if (_fileURL)
{
self.fileURLToken = [HBSecurityAccessToken tokenWithObject:_fileURL];
}
}
self.accessCount += 1;
return YES;
#else
return NO;
#endif
}
- (void)stopAccessingSecurityScopedResource
{
#ifdef __SANDBOX_ENABLED__
self.accessCount -= 1;
NSAssert(self.accessCount >= 0, @"[HBSubtitles stopAccessingSecurityScopedResource:] unbalanced call");
if (self.accessCount == 0)
{
self.fileURLToken = nil;
}
#endif
}
+ (BOOL)supportsSecureCoding { return YES ;}
- (void)encodeWithCoder:(nonnull NSCoder *)coder
{
encodeObject(_displayName);
encodeObject(_title);
encodeInt(_type);
encodeObject(_isoLanguageCode);
#ifdef __SANDBOX_ENABLED__
if (_fileURL)
{
if (!_bookmark)
{
_bookmark = [HBUtilities bookmarkFromURL:_fileURL
options:NSURLBookmarkCreationWithSecurityScope | NSURLBookmarkCreationSecurityScopeAllowOnlyReadAccess];
}
encodeObject(_bookmark);
}
#endif
encodeObject(_fileURL);
}
- (nullable instancetype)initWithCoder:(nonnull NSCoder *)decoder
{
self = [super init];
if (self)
{
decodeObjectOrFail(_displayName, NSString);
decodeObject(_title, NSString);
decodeInt(_type);
decodeObjectOrFail(_isoLanguageCode, NSString);
#ifdef __SANDBOX_ENABLED__
decodeObject(_bookmark, NSData);
if (_bookmark)
{
decodeObjectOrFail(_fileURL, NSURL);
}
#else
decodeObject(_fileURL, NSURL);
#endif
}
return self;
fail:
return nil;
}
@end
@interface HBTitle ()
@property (nonatomic, readonly) hb_title_t *hb_title;
@property (nonatomic, readonly) hb_handle_t *hb_handle;
@property (nonatomic, readwrite, copy) NSString *name;
@property (nonatomic, readwrite) HBMetadata *metadata;
@property (nonatomic, readwrite) NSArray *audioTracks;
@property (nonatomic, readwrite) NSArray<HBTitleSubtitlesTrack *> *subtitlesTracks;
@property (nonatomic, readwrite) NSArray<HBChapter *> *chapters;
@end
@implementation HBTitle
- (instancetype)initWithTitle:(hb_title_t *)title handle:(hb_handle_t *)handle featured:(BOOL)featured
{
self = [super init];
if (self)
{
if (!title)
{
return nil;
}
_hb_title = title;
_hb_handle = handle;
_featured = featured;
_metadata = [[HBMetadata alloc] initWithMetadata:title->metadata];
}
return self;
}
- (NSString *)name
{
if (!_name)
{
_name = @(self.hb_title->name);
// Use the bundle name for eyetv
NSURL *parentURL = self.url.URLByDeletingLastPathComponent;
if ([parentURL.pathExtension caseInsensitiveCompare:@"eyetv"] == NSOrderedSame)
{
_name = parentURL.URLByDeletingPathExtension.lastPathComponent;
}
// If the name is empty use file/directory name
if (_name.length == 0)
{
_name = @(self.hb_title->path).lastPathComponent;
}
}
return _name;
}
- (BOOL)isStream
{
return (self.hb_title->type == HB_STREAM_TYPE || self.hb_title->type == HB_FF_STREAM_TYPE);
}
- (NSString *)description
{
if (self.hb_title->type == HB_BD_TYPE)
{
return [NSString stringWithFormat:@"%d - %@ - %05d.MPLS",
self.hb_title->index, self.timeCode, self.hb_title->playlist];
}
else if (self.hb_title->type == HB_DVD_TYPE)
{
return [NSString stringWithFormat:@"%d - %@",
self.hb_title->index, self.timeCode];
}
else
{
return [NSString stringWithFormat:@"%d - %@ - %@",
self.hb_title->index, self.timeCode, @(self.hb_title->name)];
}
}
- (NSString *)shortFormatDescription
{
NSMutableString *format = [[NSMutableString alloc] init];
[format appendFormat:@"%dx%d", _hb_title->geometry.width, _hb_title->geometry.height];
if (_hb_title->geometry.par.num != 1 || _hb_title->geometry.par.den != 1)
{
[format appendFormat:@" (%dx%d)", _hb_title->geometry.width * _hb_title->geometry.par.num / _hb_title->geometry.par.den,
_hb_title->geometry.height];
}
[format appendString:@", "];
NSString *fps = [NSString localizedStringWithFormat:HBKitLocalizedString(@"%.6g FPS", @"Title short description -> video format"), _hb_title->vrate.num / (double)_hb_title->vrate.den];
[format appendString:fps];
hb_list_t *audioList = _hb_title->list_audio;
int audioCount = hb_list_count(audioList);
if (audioCount > 1)
{
[format appendFormat:HBKitLocalizedString(@", %d audio tracks", @"Title short description -> audio format"), audioCount];
}
else if (audioCount == 1)
{
[format appendString:HBKitLocalizedString(@", 1 audio track", @"Title short description -> audio format")];
}
hb_list_t *subList = _hb_title->list_subtitle;
int subCount = hb_list_count(subList);
if (subCount > 1)
{
[format appendFormat:HBKitLocalizedString(@", %d subtitles tracks", @"Title short description -> subtitles format"), subCount];
}
else if (subCount == 1)
{
[format appendString:HBKitLocalizedString(@", 1 subtitles track", @"Title short description -> subtitles format")];
}
return format;
}
- (NSURL *)url
{
return [NSURL fileURLWithPath:@(_hb_title->path)];
}
- (int)index
{
return self.hb_title->index;
}
- (int)angles
{
return self.hb_title->angle_count;
}
- (int)duration
{
return (self.hb_title->hours * 3600) + (self.hb_title->minutes * 60) + (self.hb_title->seconds);
}
- (int)frames
{
double frames = (double)self.hb_title->duration / 90000.f * self.hb_title->vrate.num / self.hb_title->vrate.den;
return (int)ceil(frames);
}
- (NSString *)timeCode
{
return [NSString stringWithFormat:@"%02d:%02d:%02d",
self.hb_title->hours, self.hb_title->minutes, self.hb_title->seconds];
}
- (int)width
{
return _hb_title->geometry.width;
}
- (int)height
{
return _hb_title->geometry.height;
}
- (int)parWidth
{
return _hb_title->geometry.par.num;
}
- (int)parHeight
{
return _hb_title->geometry.par.den;
}
- (int)autoCropTop
{
return _hb_title->crop[0];
}
- (int)autoCropBottom
{
return _hb_title->crop[1];
}
- (int)autoCropLeft
{
return _hb_title->crop[2];
}
- (int)autoCropRight
{
return _hb_title->crop[3];
}
- (NSArray<HBTitleAudioTrack *> *)audioTracks
{
if (!_audioTracks)
{
NSMutableArray<HBTitleAudioTrack *> *tracks = [NSMutableArray array];
hb_list_t *list = self.hb_title->list_audio;
int count = hb_list_count(list);
// Initialize the audio list of available audio tracks from this title
for (int i = 0; i < count; i++)
{
hb_audio_config_t *audio = hb_list_audio_config_item(list, i);
[tracks addObject:[[HBTitleAudioTrack alloc ] initWithAudioTrack:audio index:i]];
}
_audioTracks = [tracks copy];
}
return _audioTracks;
}
- (NSArray<HBTitleSubtitlesTrack *> *)subtitlesTracks
{
if (!_subtitlesTracks)
{
NSMutableArray<HBTitleSubtitlesTrack *> *tracks = [NSMutableArray array];
hb_list_t *list = self.hb_title->list_subtitle;
int count = hb_list_count(list);
for (int i = 0; i < count; i++)
{
hb_subtitle_t *subtitle = hb_list_item(self.hb_title->list_subtitle, i);
[tracks addObject:[[HBTitleSubtitlesTrack alloc] initWithSubtitlesTrack:subtitle index:i]];
}
_subtitlesTracks = [tracks copy];
}
return _subtitlesTracks;
}
- (NSArray<HBChapter *> *)chapters
{
if (!_chapters)
{
NSMutableArray<HBChapter *> *chapters = [NSMutableArray array];
uint64_t currentTime = 0;
for (int i = 0; i < hb_list_count(self.hb_title->list_chapter); i++)
{
hb_chapter_t *chapter = hb_list_item(self.hb_title->list_chapter, i);
if (chapter != NULL)
{
NSString *title;
if (chapter->title != NULL)
{
title = @(chapter->title);
}
else
{
title = [NSString stringWithFormat:HBKitLocalizedString(@"Chapter %d", "Title -> chapter name"), i + 1];
}
[chapters addObject:[[HBChapter alloc] initWithTitle:title
index:i + 1
timestamp:currentTime
duration:chapter->duration]];
currentTime += chapter->duration;
}
}
_chapters = [chapters copy];
}
return _chapters;
}
- (NSDictionary *)jobSettingsWithPreset:(HBPreset *)preset
{
NSDictionary *result = nil;
hb_dict_t *hb_preset = [preset content].hb_value;
hb_dict_t *job = hb_preset_job_init(self.hb_handle, self.hb_title->index, hb_preset);
if (job)
{
result = [[NSDictionary alloc] initWithHBDict:job];
}
hb_dict_free(&hb_preset);
hb_dict_free(&job);
return result;
}
@end
|