-
Notifications
You must be signed in to change notification settings - Fork 1
/
AVPlayerViewController+Subtitles.m
228 lines (190 loc) · 8.61 KB
/
AVPlayerViewController+Subtitles.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
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
//
// AVPlayerViewController+Subtitles.m
// LiveClass
//
// Created by wujianbo on 16/10/28.
// Copyright © 2016年 chaoui. All rights reserved.
//
#import <objc/runtime.h>
#import <AVFoundation/AVPlayer.h>
#import "AVPlayerViewController+Subtitles.h"
static NSString *FontKey = @"FontKey";
//static NSString *ColorKey = @"ColorKey";
static NSString *SubtitleKey = @"SubtitleKey";
static NSString *SubtitleHeightKey = @"SubtitleHeightKey";
static NSString *PayloadKey = @"PayloadKey";
@interface AVPlayerViewController ()
@property (nonatomic, strong) NSLayoutConstraint *subtitleLabelHeightConstraint;
//- (NSDictionary*)parseSubRip:(NSString*)payload;
//- (void)searchSubtitles:(CMTime)time;
@end
@implementation AVPlayerViewController (Subtitles)
- (AVPlayerViewController* _Nonnull)addSubtitles
{
[self addSubtitleLabel];
return self;
}
- (void)showSubtitlesWithFile:(NSURL * _Nonnull)srtFilePath
{
NSStringEncoding usedEncoding;
NSString *contents = [NSString stringWithContentsOfURL:srtFilePath usedEncoding:&usedEncoding error:NULL];
if (contents.length)
{
[self showSubtitlesWithContent:contents];
}
}
- (void)showSubtitlesWithContent:(NSString * _Nonnull)srtContent
{
NSDictionary *parsedPayload = [self parseSubRip:srtContent];
objc_setAssociatedObject(self, &PayloadKey, parsedPayload, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
if (parsedPayload && parsedPayload.count && self.player)
{
typeof(self) __weak weakSelf = self;
[self.player addPeriodicTimeObserverForInterval:CMTimeMake(1, 60) queue:dispatch_get_main_queue() usingBlock:^(CMTime time) {
[weakSelf searchSubtitles:time];
}];
}
}
- (void)refreshSubtitleLabel
{
[self searchSubtitles:self.player.currentTime];
}
#pragma mark - Custom setter && getter
- (UILabel*)subtitleLabel
{
return objc_getAssociatedObject(self, &SubtitleKey);
}
- (NSLayoutConstraint*)subtitleLabelHeightConstraint
{
return objc_getAssociatedObject(self, &SubtitleHeightKey);
}
- (NSDictionary*)parsedPayload
{
return objc_getAssociatedObject(self, &PayloadKey);
}
#pragma mark - Private methods
- (void)addSubtitleLabel
{
if (!self.subtitleLabel)
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.translatesAutoresizingMaskIntoConstraints = false;
label.backgroundColor = [UIColor clearColor];
label.textAlignment = NSTextAlignmentCenter;
label.numberOfLines = 0;
label.font = [UIFont boldSystemFontOfSize:(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ? 40.0 : 17.0)];
label.textColor = [UIColor whiteColor];
label.numberOfLines = 0;
label.layer.shadowColor = [UIColor blackColor].CGColor;
label.layer.shadowOffset = CGSizeMake(1.0, 1.0);
label.layer.shadowOpacity = 0.8;
label.layer.shadowRadius = 1.0;
label.layer.shouldRasterize = true;
label.layer.rasterizationScale = [UIScreen mainScreen].scale;
label.lineBreakMode = NSLineBreakByWordWrapping;
[self.contentOverlayView addSubview:label];
// Position
NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(20)-[l]-(20)-|" options:0 metrics:nil views:@{@"l":label}];
[self.contentOverlayView addConstraints:constraints];
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[l]-(15)-|" options:0 metrics:nil views: @{@"l":label}];
[self.contentOverlayView addConstraints:constraints];
NSLayoutConstraint *labelHeightConstraint = [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:30.0];
[self.contentOverlayView addConstraint:labelHeightConstraint];
objc_setAssociatedObject(self, &SubtitleHeightKey, labelHeightConstraint, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
objc_setAssociatedObject(self, &SubtitleKey, label, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
}
- (NSDictionary*)parseSubRip:(NSString*)payload
{
// Prepare payload
NSString *preprocessedPayload = [payload stringByReplacingOccurrencesOfString:@"\n\r\n" withString:@"\n\n"];
preprocessedPayload = [preprocessedPayload stringByReplacingOccurrencesOfString:@"\n\n\n" withString:@"\n\n"];
// Parsed dict
NSMutableDictionary *parsed = [[NSMutableDictionary alloc] init];
// Get groups
NSString *regexStr = @"(?m)(^[0-9]+)([\\s\\S]*?)(?=\n\n)";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regexStr options:NSRegularExpressionCaseInsensitive error:NULL];
NSArray<NSTextCheckingResult *> *matches = [regex matchesInString:preprocessedPayload options:0 range:NSMakeRange(0, preprocessedPayload.length)];
for (NSTextCheckingResult *m in matches)
{
NSString *group = [preprocessedPayload substringWithRange:m.range];
// Get index
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^[0-9]+" options:NSRegularExpressionCaseInsensitive error:NULL];
NSArray<NSTextCheckingResult *> *match = [regex matchesInString:group options:0 range:NSMakeRange(0, group.length)];
NSTextCheckingResult *i = match.firstObject;
if (!i)
{
continue;
}
NSString *index = [group substringWithRange:i.range];
// Get "from" & "to" time
regex = [NSRegularExpression regularExpressionWithPattern:@"\\d{1,2}:\\d{1,2}:\\d{1,2},\\d{1,3}" options:NSRegularExpressionCaseInsensitive error:NULL];
match = [regex matchesInString:group options:0 range:NSMakeRange(0, group.length)];
if (match.count != 2)
{
continue;
}
NSTextCheckingResult *from = match.firstObject;
NSTextCheckingResult *to = match.lastObject;
NSTimeInterval h = 0.0, m = 0.0, s = 0.0, c = 0.0;
NSString *fromStr = [group substringWithRange:from.range];
NSScanner *scanner = [NSScanner scannerWithString:fromStr];
[scanner scanDouble:&h];
[scanner scanString:@":" intoString:NULL];
[scanner scanDouble:&m];
[scanner scanString:@":" intoString:NULL];
[scanner scanDouble:&s];
[scanner scanString:@"," intoString:NULL];
[scanner scanDouble:&c];
NSTimeInterval fromTime = (h * 3600.0) + (m * 60.0) + s + (c / 1000.0);
NSString *toStr = [group substringWithRange:to.range];
scanner = [NSScanner scannerWithString:toStr];
[scanner scanDouble:&h];
[scanner scanString:@":" intoString:NULL];
[scanner scanDouble:&m];
[scanner scanString:@":" intoString:NULL];
[scanner scanDouble:&s];
[scanner scanString:@"," intoString:NULL];
[scanner scanDouble:&c];
NSTimeInterval toTime = (h * 3600.0) + (m * 60.0) + s + (c / 1000.0);
// Get text & check if empty
NSRange range = NSMakeRange(0, to.range.location + to.range.length + 1);
if (group.length - range.length <= 0)
{
continue;
}
NSString *text = [group stringByReplacingCharactersInRange:range withString:@""];
// Create final object
NSMutableDictionary *final = [[NSMutableDictionary alloc] init];
[final setObject:@(fromTime) forKey:@"from"];
[final setObject:@(toTime) forKey:@"to"];
[final setObject:text forKey:@"text"];
[parsed setObject:final forKey:index];
}
return parsed;
}
- (void)searchSubtitles:(CMTime)time
{
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(%f >= %K) AND (%f <= %K)", CMTimeGetSeconds(time), @"from", CMTimeGetSeconds(time), @"to"];
NSArray *values = self.parsedPayload.allValues;
if (values && values.count)
{
NSDictionary *result = [values filteredArrayUsingPredicate:predicate].firstObject;
if (!result)
{
self.subtitleLabel.text = @"";
return;
}
UILabel *label = self.subtitleLabel;
// Set text
label.text = [(NSString*)[result objectForKey:@"text"] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
// Adjust size
CGRect rect = [label.text boundingRectWithSize:CGSizeMake(label.bounds.size.width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:label.font} context:nil];
self.subtitleLabelHeightConstraint.constant = rect.size.height + 5.0;
}
else
{
self.subtitleLabel.text = @"";
}
}
@end