BMPlayer
BMPlayer copied to clipboard
The video player doesn't display remote subtitles with VTT format.
Why the video player doesn't supports subtitles with VTT format.
Installed with
- [x] Cocoapods
Issue Description
The video player doesn't display remote subtitles with VTT format. I tried it for remote link url
subtitleURL = URL(string:"https://subtitlesLink")
let asset = BMPlayerResource(url: URL(string: viewModel.externalURL)!,
name: viewModel.name,
cover: nil,
subtitle:strURL)
I also tried a locally vtt file
let strURL = Bundle.main.url(forResource: "subtitles_englishc", withExtension: "vtt")!
let subtitle = BMSubtitles(url: strURL)
let asset = BMPlayerResource(name: viewModel.name,
definitions: [BMPlayerResourceDefinition(url: URL(string: viewModel.externalURL)!, definition: "")],
cover: nil,
subtitles: subtitle)
self.player.setVideo(resource: asset)
```the same issue, no subtitles displayed
### What
I only implemented str subtitle parser, so it can't support vtt file.
To whom who wants to use this library with VTT format subtitle, you can do the following,
- read the remote VTT subtitle file.
- Convert it to SRT format.
- Save it and load it from the documents directory.
Here you can find my formatter. Stackoverflow question link
+ (NSString *)srtSubtitleFromVTT:(NSString *)content {
NSArray *lines = [self split:content];
NSString *output = @"";
NSInteger i = 0;
NSError *error;
NSString *newLine;
for (NSString *line in lines) {
newLine = line;
NSCharacterSet* notDigits = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
if([[line stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"\n\r "]] rangeOfCharacterFromSet:notDigits].location == NSNotFound) {
continue;
}
NSString *pattern1 = @"(\\d{2}):(\\d{2}):(\\d{2})\\.(\\d{3})"; // '01:52:52.554'
NSString *pattern2 = @"(\\d{2}):(\\d{2})\\.(\\d{3})"; // '00:08.301'
NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern: pattern1 options:0 error:&error];
NSArray* matches = [regex matchesInString:line options:0 range: NSMakeRange(0, line.length)];
NSString *pattern ;
NSString *template;
if (matches.count) {
pattern = pattern1;
template = @"$1:$2:$3,$4";
} else {
regex = [NSRegularExpression regularExpressionWithPattern: pattern2 options:0 error:&error];
matches = [regex matchesInString:line options:0 range: NSMakeRange(0, line.length)];
if (matches.count) {
pattern = pattern2;
template = @"00:$1:$2,$3";
}
}
if (matches.count && !error) {
i++;
output = [output stringByAppendingString:@"\r\n\r\n"];
output = [output stringByAppendingString:[@(i) stringValue]];
output = [output stringByAppendingString:@"\r\n"];
newLine = [line stringByReplacingWithPattern:pattern withTemplate:template error:&error];
}
if([newLine containsString:@"WEBVTT"]) {
continue;
}
output = [output stringByAppendingString:newLine];
output = [output stringByAppendingString:@"\r\n"];
}
return output;
}
+ (NSArray *)split:(NSString *)content {
NSArray *lines = [content componentsSeparatedByString: @"\n"];
if (lines.count == 1) {
lines = [content componentsSeparatedByString: @"\r\n"];
if (lines.count == 1) {
lines = [content componentsSeparatedByString: @"\n"];
}
}
return lines;
}
Hi @a-radwan, the function stringByReplacingWithPattern
is deprecated I think.
@allenlinli ,its a utility I have implemented
- (NSString *)stringByReplacingWithPattern:(NSString *)pattern withTemplate:(NSString *)template error:(NSError **)error {
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern
options:NSRegularExpressionCaseInsensitive
error:error];
return [regex stringByReplacingMatchesInString:self
options:0
range:NSMakeRange(0, self.length)
withTemplate:template];
}