Quite incomplete and untested ssa parser.
This commit is contained in:
6 files changed
+685
-1
No files matched your search
@@ -10,6 +10,7 @@
|
||||
#import "MP42MkvImporter.h"
|
||||
#import "MP42Mp4Importer.h"
|
||||
#import "MP42SrtImporter.h"
|
||||
#import "MP42SSAImporter.h"
|
||||
#import "MP42CCImporter.h"
|
||||
#import "MP42AC3Importer.h"
|
||||
#import "MP42AACImporter.h"
|
||||
@@ -55,7 +56,8 @@ static NSArray<NSString *> *_supportedFileFormats;
|
||||
[MP42H264Importer class],
|
||||
[MP42VobSubImporter class],
|
||||
[MP42AVFImporter class],
|
||||
[MP42AC3Importer class]];
|
||||
[MP42AC3Importer class],
|
||||
[MP42SSAImporter class],];
|
||||
|
||||
NSMutableArray<NSString *> *formats = [[NSMutableArray alloc] init];
|
||||
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
//
|
||||
// MP42SrtImporter.h
|
||||
// Subler
|
||||
//
|
||||
// Created by Damiano Galassi on 31/01/10.
|
||||
// Copyright 2010 Damiano Galassi All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "MP42FileImporter.h"
|
||||
|
||||
|
||||
@interface MP42SSAImporter : MP42FileImporter
|
||||
@end
|
||||
@@ -0,0 +1,174 @@
|
||||
//
|
||||
// MP42SrtImporter.m
|
||||
// Subler
|
||||
//
|
||||
// Created by Damiano Galassi on 31/01/10.
|
||||
// Copyright 2010 Damiano Galassi All rights reserved.
|
||||
//
|
||||
|
||||
#import "MP42SSAImporter.h"
|
||||
#import "MP42FileImporter+Private.h"
|
||||
|
||||
#import "MP42File.h"
|
||||
#import "MP42SSAParser.h"
|
||||
#import "MP42SubUtilities.h"
|
||||
#import "MP42Languages.h"
|
||||
|
||||
#import "mp4v2.h"
|
||||
#import "MP42PrivateUtilities.h"
|
||||
#import "MP42Track+Private.h"
|
||||
|
||||
@interface MP42SSAImporter ()
|
||||
|
||||
@property (nonatomic, readonly) MP42SSAParser *parser;
|
||||
@property (nonatomic, readonly) SBSubSerializer *ss;
|
||||
|
||||
@end
|
||||
|
||||
@implementation MP42SSAImporter
|
||||
|
||||
+ (NSArray<NSString *> *)supportedFileFormats {
|
||||
return @[@"ssa", @"ass"];
|
||||
}
|
||||
|
||||
- (instancetype)initWithURL:(NSURL *)fileURL error:(NSError **)outError
|
||||
{
|
||||
if ((self = [super initWithURL:fileURL])) {
|
||||
MP42SubtitleTrack *newTrack = [[MP42SubtitleTrack alloc] init];
|
||||
|
||||
newTrack.format = kMP42SubtitleCodecType_3GText;
|
||||
newTrack.URL = self.fileURL;
|
||||
newTrack.alternateGroup = 2;
|
||||
newTrack.language = getFilenameLanguage((__bridge CFStringRef)self.fileURL.path);
|
||||
|
||||
NSString *stringFromFileAtURL = [NSString stringWithContentsOfURL:fileURL encoding:NSUTF8StringEncoding error:NULL];
|
||||
|
||||
// Check if a 10.10 only class is available, NSLinguisticTagger crashes on 10.9
|
||||
// if the string contains some characters.
|
||||
if ([newTrack.language isEqualToString:@"und"] && NSClassFromString(@"NSVisualEffectView")) {
|
||||
// we couldn't deduce language from the fileURL
|
||||
// -> Let's look into the file itself
|
||||
|
||||
if (stringFromFileAtURL) { // try auto determining
|
||||
NSArray *tagschemes = @[NSLinguisticTagSchemeLanguage];
|
||||
NSCountedSet *languagesSet = [NSCountedSet new];
|
||||
NSLinguisticTagger *tagger = [[NSLinguisticTagger alloc] initWithTagSchemes:tagschemes options:0];
|
||||
|
||||
[stringFromFileAtURL enumerateLinesUsingBlock:^(NSString *line, BOOL *stop) {
|
||||
|
||||
if (line.length > 1) {
|
||||
|
||||
tagger.string = line;
|
||||
|
||||
NSOrthography *ortho = [tagger orthographyAtIndex:0 effectiveRange:NULL];
|
||||
NSString *dominantLanguage = ortho.dominantLanguage;
|
||||
|
||||
if (dominantLanguage && ![dominantLanguage isEqualToString:@"und"]) {
|
||||
[languagesSet addObject:dominantLanguage];
|
||||
}
|
||||
}
|
||||
}];
|
||||
|
||||
NSArray *sortedValues = [languagesSet.allObjects sortedArrayUsingComparator:^(id obj1, id obj2) {
|
||||
NSUInteger n = [languagesSet countForObject:obj1];
|
||||
NSUInteger m = [languagesSet countForObject:obj2];
|
||||
return (n <= m)? (n < m)? NSOrderedAscending : NSOrderedSame : NSOrderedDescending;
|
||||
}];
|
||||
|
||||
NSString *language = sortedValues.lastObject;
|
||||
|
||||
if (language) {
|
||||
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en"];
|
||||
NSString *languageName = [locale displayNameForKey:NSLocaleLanguageCode
|
||||
value:language];
|
||||
|
||||
if (languageName) {
|
||||
newTrack.language = [MP42Languages.defaultManager extendedTagForLang:languageName];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_parser = [[MP42SSAParser alloc] initWithString:stringFromFileAtURL];
|
||||
|
||||
if (!_parser.lines.count) {
|
||||
if (outError) {
|
||||
*outError = MP42Error(MP42LocalizedString(@"The file could not be opened.", @"ssa error message"),
|
||||
MP42LocalizedString(@"The file is not a ssa file, or it does not contain any subtitles.", @"ssa error message"), 100);
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
|
||||
_ss = [[SBSubSerializer alloc] init];
|
||||
[_ss setSSA:YES];
|
||||
|
||||
for (MP42SSALine *line in _parser.lines) {
|
||||
SBSubLine *sl = [[SBSubLine alloc] initWithLine:line.text start:line.start end: line.end];
|
||||
[_ss addLine:sl];
|
||||
}
|
||||
[_ss setFinished:YES];
|
||||
|
||||
if ([_ss forced]) {
|
||||
newTrack.someSamplesAreForced = YES;
|
||||
}
|
||||
|
||||
newTrack.duration = _parser.lines.lastObject.end;
|
||||
|
||||
[self addTrack:newTrack];
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (nullable NSData *)magicCookieForTrack:(MP42Track *)track
|
||||
{
|
||||
return nil;
|
||||
}
|
||||
|
||||
- (NSUInteger)timescaleForTrack:(MP42Track *)track
|
||||
{
|
||||
return 1000;
|
||||
}
|
||||
|
||||
- (NSSize)sizeForTrack:(MP42Track *)track
|
||||
{
|
||||
return NSMakeSize([(MP42SubtitleTrack *)track trackWidth], [(MP42SubtitleTrack *) track trackHeight]);
|
||||
}
|
||||
|
||||
- (void)demux
|
||||
{
|
||||
@autoreleasepool {
|
||||
MP42SampleBuffer *sample;
|
||||
|
||||
for (MP42SubtitleTrack *track in self.inputTracks) {
|
||||
CGSize trackSize;
|
||||
trackSize.width = track.trackWidth;
|
||||
trackSize.height = track.trackHeight;
|
||||
|
||||
while (!_ss.isEmpty && !self.isCancelled) {
|
||||
SBSubLine *sl = [_ss getSerializedPacket];
|
||||
|
||||
if ([sl->line isEqualToString:@"\n"]) {
|
||||
sample = copyEmptySubtitleSample(track.sourceId, sl->end_time - sl->begin_time, NO);
|
||||
}
|
||||
else {
|
||||
int top = (sl->top == INT_MAX) ? trackSize.height : sl->top;
|
||||
sample = copySubtitleSample(track.sourceId, sl->line, sl->end_time - sl->begin_time, sl->forced, NO, YES, trackSize, top);
|
||||
}
|
||||
|
||||
[self enqueue:sample];
|
||||
}
|
||||
}
|
||||
|
||||
self.progress = 100.0;
|
||||
|
||||
[self setDone];
|
||||
}
|
||||
}
|
||||
|
||||
- (NSString *)description
|
||||
{
|
||||
return @"SRT demuxer";
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,29 @@
|
||||
//
|
||||
// MP42SSAParser.h
|
||||
// Subler
|
||||
//
|
||||
// Created by Damiano Galassi on 02/10/2017.
|
||||
// Copyright © 2017 Damiano Galassi. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
@interface MP42SSALine : NSObject
|
||||
|
||||
@property (nonatomic, readwrite) unsigned start;
|
||||
@property (nonatomic, readwrite) unsigned end;
|
||||
@property (nonatomic, readwrite) NSString *text;
|
||||
|
||||
@end
|
||||
|
||||
@interface MP42SSAParser : NSObject
|
||||
|
||||
- (instancetype)initWithString:(NSString *)string;
|
||||
- (instancetype)initWithHeader:(NSString *)header;
|
||||
|
||||
@property (nonatomic, readonly) NSArray<MP42SSALine *> *lines;
|
||||
|
||||
- (void)addLine:(NSString *)line;
|
||||
|
||||
@end
|
||||
|
||||
@@ -0,0 +1,441 @@
|
||||
//
|
||||
// MP42SSAParser.m
|
||||
// Subler
|
||||
//
|
||||
// Created by Damiano Galassi on 02/10/2017.
|
||||
// Copyright © 2017 Damiano Galassi. All rights reserved.
|
||||
//
|
||||
|
||||
#import "MP42SSAParser.h"
|
||||
|
||||
@interface MP42SSAStyle : NSObject
|
||||
|
||||
- (instancetype)initWith:(NSArray<NSString *> *)style formats:(NSArray<NSString *> *)format;
|
||||
|
||||
@property (nonatomic, readwrite) NSString *name;
|
||||
@property (nonatomic, readwrite) NSString *fontName;
|
||||
@property (nonatomic, readwrite) int fontSize;
|
||||
@property (nonatomic, readwrite) long primaryColour;
|
||||
@property (nonatomic, readwrite) long secondaryColour;
|
||||
@property (nonatomic, readwrite) long outlineColour;
|
||||
@property (nonatomic, readwrite) long backColour;
|
||||
@property (nonatomic, readwrite) BOOL bold;
|
||||
@property (nonatomic, readwrite) BOOL italic;
|
||||
@property (nonatomic, readwrite) BOOL underline;
|
||||
@property (nonatomic, readwrite) BOOL strikeOut;
|
||||
@property (nonatomic, readwrite) float scaleX;
|
||||
@property (nonatomic, readwrite) float scaleY;
|
||||
@property (nonatomic, readwrite) int spacing;
|
||||
@property (nonatomic, readwrite) float angle;
|
||||
@property (nonatomic, readwrite) short borderStyle;
|
||||
@property (nonatomic, readwrite) short outline;
|
||||
@property (nonatomic, readwrite) short shadow;
|
||||
@property (nonatomic, readwrite) short alignment;
|
||||
@property (nonatomic, readwrite) int marginL;
|
||||
@property (nonatomic, readwrite) int marginR;
|
||||
@property (nonatomic, readwrite) int marginV;
|
||||
@property (nonatomic, readwrite) int alphaLevel;
|
||||
@property (nonatomic, readwrite) int encoding;
|
||||
|
||||
@end
|
||||
|
||||
@implementation MP42SSAStyle
|
||||
|
||||
- (instancetype)initWith:(NSArray<NSString *> *)style formats:(NSArray<NSString *> *)format
|
||||
{
|
||||
self = [super init];
|
||||
if (self) {
|
||||
NSUInteger stylesCount = style.count;
|
||||
NSUInteger formatsCount = format.count;
|
||||
|
||||
for (NSUInteger index = 0; index < stylesCount && index < formatsCount; index += 1) {
|
||||
NSString *value = style[index];
|
||||
NSString *formatName = format[index];
|
||||
|
||||
if ([formatName isEqualToString:@"Name"]) {
|
||||
_name = value;
|
||||
}
|
||||
else if ([formatName isEqualToString:@"Fontname"]) {
|
||||
_fontName = value;
|
||||
}
|
||||
else if ([formatName isEqualToString:@"Fontsize"]) {
|
||||
_fontSize = value.intValue;
|
||||
}
|
||||
else if ([formatName isEqualToString:@"PrimaryColour"]) {
|
||||
_primaryColour = value.intValue;
|
||||
}
|
||||
else if ([formatName isEqualToString:@"SecondaryColour"]) {
|
||||
_secondaryColour = value.intValue;
|
||||
}
|
||||
else if ([formatName isEqualToString:@"TertiaryColour"] ||
|
||||
[formatName isEqualToString:@"OutlineColour"] ||
|
||||
[formatName isEqualToString:@"OutlineColor"]) {
|
||||
_outlineColour = value.intValue;
|
||||
}
|
||||
else if ([formatName isEqualToString:@"BackColour"]) {
|
||||
_backColour = value.intValue;
|
||||
}
|
||||
else if ([formatName isEqualToString:@"Bold"]) {
|
||||
_bold = value.intValue == -1 ? YES : NO;
|
||||
}
|
||||
else if ([formatName isEqualToString:@"Italic"]) {
|
||||
_italic = value.intValue == -1 ? YES : NO;
|
||||
}
|
||||
else if ([formatName isEqualToString:@"Underline"]) {
|
||||
_underline = value.intValue == -1 ? YES : NO;
|
||||
}
|
||||
else if ([formatName isEqualToString:@"StrikeOut"]) {
|
||||
_strikeOut = value.intValue == -1 ? YES : NO;
|
||||
}
|
||||
else if ([formatName isEqualToString:@"ScaleX"]) {
|
||||
_scaleX = value.floatValue;
|
||||
}
|
||||
else if ([formatName isEqualToString:@"ScaleY"]) {
|
||||
_scaleY = value.floatValue;
|
||||
}
|
||||
else if ([formatName isEqualToString:@"Spacing"]) {
|
||||
_spacing = value.intValue;
|
||||
}
|
||||
else if ([formatName isEqualToString:@"Angle"]) {
|
||||
_angle = value.floatValue;
|
||||
}
|
||||
else if ([formatName isEqualToString:@"BorderStyle"]) {
|
||||
_borderStyle = value.intValue;
|
||||
}
|
||||
else if ([formatName isEqualToString:@"Outline"]) {
|
||||
_outline = value.intValue;
|
||||
}
|
||||
else if ([formatName isEqualToString:@"Shadow"]) {
|
||||
_shadow = value.intValue;
|
||||
}
|
||||
else if ([formatName isEqualToString:@"Alignment"]) {
|
||||
_alignment = value.intValue;
|
||||
}
|
||||
else if ([formatName isEqualToString:@"MarginL"]) {
|
||||
_marginL = value.intValue;
|
||||
}
|
||||
else if ([formatName isEqualToString:@"MarginR"]) {
|
||||
_marginR = value.intValue;
|
||||
}
|
||||
else if ([formatName isEqualToString:@"MarginV"]) {
|
||||
_marginV = value.intValue;
|
||||
}
|
||||
else if ([formatName isEqualToString:@"AlphaLevel"]) {
|
||||
_alphaLevel = value.intValue;
|
||||
}
|
||||
else if ([formatName isEqualToString:@"Encoding"]) {
|
||||
_encoding = value.intValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
|
||||
@interface MP42SSALine ()
|
||||
|
||||
- (instancetype)initWithString:(NSString *)string format:(NSArray<NSString *> *)format;
|
||||
|
||||
@property (nonatomic, readwrite) int layer;
|
||||
@property (nonatomic, readwrite) NSString *style;
|
||||
@property (nonatomic, readwrite) NSString *name;
|
||||
@property (nonatomic, readwrite) int marginL;
|
||||
@property (nonatomic, readwrite) int marginR;
|
||||
@property (nonatomic, readwrite) int marginV;
|
||||
@property (nonatomic, readwrite) NSString *effect;
|
||||
|
||||
@end
|
||||
|
||||
@implementation MP42SSALine
|
||||
|
||||
- (instancetype)initWithString:(NSString *)string format:(NSArray<NSString *> *)format
|
||||
{
|
||||
self = [super init];
|
||||
if (self) {
|
||||
NSUInteger formatsCount = format.count;
|
||||
NSArray<NSString *> *values = [self parse:string count:formatsCount];
|
||||
NSUInteger valuesCount = values.count;
|
||||
|
||||
for (NSUInteger index = 0; index < valuesCount && index < formatsCount; index += 1) {
|
||||
NSString *value = values[index];
|
||||
NSString *formatName = format[index];
|
||||
|
||||
if ([formatName isEqualToString:@"Layer"]) {
|
||||
_layer = value.intValue;
|
||||
}
|
||||
else if ([formatName isEqualToString:@"Start"]) {
|
||||
_start = ParseSubTime(value.UTF8String, 1000, NO);
|
||||
}
|
||||
else if ([formatName isEqualToString:@"End"]) {
|
||||
_end = ParseSubTime(value.UTF8String, 1000, NO);
|
||||
}
|
||||
else if ([formatName isEqualToString:@"Style"]) {
|
||||
_style = value;
|
||||
}
|
||||
else if ([formatName isEqualToString:@"Name"]) {
|
||||
_name = value;
|
||||
}
|
||||
else if ([formatName isEqualToString:@"MarginL"]) {
|
||||
_marginL = value.intValue;
|
||||
}
|
||||
else if ([formatName isEqualToString:@"MarginR"]) {
|
||||
_marginR = value.intValue;
|
||||
}
|
||||
else if ([formatName isEqualToString:@"MarginV"]) {
|
||||
_marginV = value.intValue;
|
||||
}
|
||||
else if ([formatName isEqualToString:@"Effect"]) {
|
||||
_effect = value;
|
||||
}
|
||||
else if ([formatName isEqualToString:@"Text"]) {
|
||||
_text = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (NSArray<NSString *> *)parse:(NSString *)string count:(NSUInteger)count
|
||||
{
|
||||
NSScanner *sc = [NSScanner scannerWithString:string];
|
||||
NSMutableArray<NSString *> *valuesArray = [NSMutableArray array];
|
||||
|
||||
if ([sc scanUpToString:@"Dialogue:" intoString:nil] || [sc scanString:@"Dialogue:" intoString:nil]) {
|
||||
[sc scanString:@"Dialogue:" intoString:nil];
|
||||
|
||||
NSString *value;
|
||||
|
||||
for (NSUInteger index = 0; index < count - 1; index += 1) {
|
||||
if ([sc scanUpToString:@"," intoString:&value]) {
|
||||
[valuesArray addObject:value];
|
||||
}
|
||||
else {
|
||||
[valuesArray addObject:@""];
|
||||
}
|
||||
[sc scanString:@"," intoString:nil];
|
||||
}
|
||||
|
||||
value = [sc.string substringFromIndex:sc.scanLocation];
|
||||
if (value) {
|
||||
[valuesArray addObject:value];
|
||||
}
|
||||
}
|
||||
|
||||
return valuesArray;
|
||||
}
|
||||
|
||||
static unsigned ParseSubTime(const char *time, unsigned secondScale, BOOL hasSign)
|
||||
{
|
||||
unsigned hour, minute, second, subsecond, timeval;
|
||||
char separator[3];
|
||||
int sign = 1;
|
||||
|
||||
if (hasSign && *time == '-') {
|
||||
sign = -1;
|
||||
time++;
|
||||
}
|
||||
|
||||
if (sscanf(time, "%u:%u:%u%[,.:]%u", &hour, &minute, &second, separator, &subsecond) < 5) {
|
||||
subsecond = 0;
|
||||
if (sscanf(time, "%u:%u:%u", &hour, &minute, &second) < 3) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (second > 60) {
|
||||
second = 0;
|
||||
}
|
||||
|
||||
while (subsecond > secondScale) {
|
||||
subsecond /= 10;
|
||||
}
|
||||
|
||||
timeval = hour * 60 * 60 + minute * 60 + second;
|
||||
timeval = secondScale * timeval + subsecond;
|
||||
|
||||
return timeval * sign;
|
||||
}
|
||||
|
||||
|
||||
@end
|
||||
|
||||
@interface MP42SSAParser ()
|
||||
|
||||
@property (nonatomic, readonly) NSString *info;
|
||||
@property (nonatomic, readonly) NSArray<MP42SSAStyle *> *styles;
|
||||
|
||||
@property (nonatomic, readonly) NSArray<NSString *> *format;
|
||||
@property (nonatomic, readonly) NSMutableArray<MP42SSALine *> *lines_internal;
|
||||
|
||||
@end
|
||||
|
||||
@implementation MP42SSAParser
|
||||
|
||||
- (instancetype)init
|
||||
{
|
||||
self = [super init];
|
||||
if (self) {
|
||||
_styles = @[];
|
||||
_lines_internal = [NSMutableArray array];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (instancetype)initWithString:(NSString *)string
|
||||
{
|
||||
NSParameterAssert(string);
|
||||
self = [self init];
|
||||
if (self) {
|
||||
[self parse:string];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (instancetype)initWithHeader:(NSString *)header
|
||||
{
|
||||
NSParameterAssert(header);
|
||||
self = [self init];
|
||||
if (self) {
|
||||
[self parseHeader:header];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)parse:(NSString *)ssa
|
||||
{
|
||||
[self parseHeader:ssa];
|
||||
[self parseLines:ssa];
|
||||
}
|
||||
|
||||
#pragma mark - Header
|
||||
|
||||
- (void)parseHeader:(NSString *)header
|
||||
{
|
||||
NSString *info;
|
||||
NSString *styles;
|
||||
NSString *format;
|
||||
|
||||
NSScanner *sc = [NSScanner scannerWithString:header];
|
||||
[sc setCharactersToBeSkipped:nil];
|
||||
|
||||
if ([sc scanUpToString:@"[V4+ Styles]" intoString:&info]) {
|
||||
_info = [self parseInfo:info];
|
||||
}
|
||||
|
||||
if ([sc scanUpToString:@"[Events]" intoString:&styles]) {
|
||||
_styles = [self parseStyles:styles];
|
||||
}
|
||||
|
||||
format = [sc.string substringFromIndex:sc.scanLocation];
|
||||
if (format.length) {
|
||||
_format = [self parseFormat:format];
|
||||
}
|
||||
}
|
||||
|
||||
- (NSString *)parseInfo:(NSString *)info
|
||||
{
|
||||
return info;
|
||||
}
|
||||
|
||||
- (NSArray<MP42SSAStyle *> *)parseStyles:(NSString *)styles
|
||||
{
|
||||
NSScanner *sc = [NSScanner scannerWithString:styles];
|
||||
NSMutableArray<NSString *> *formatsArray = [NSMutableArray array];
|
||||
NSMutableArray<MP42SSAStyle *> *stylesArray = [NSMutableArray array];
|
||||
|
||||
if ([sc scanUpToString:@"Format:" intoString:nil] || [sc scanString:@"Format:" intoString:nil]) {
|
||||
[sc scanString:@"Format:" intoString:nil];
|
||||
NSString *formats;
|
||||
if ([sc scanUpToString:@"\n" intoString:&formats]) {
|
||||
NSScanner *formatScanner = [NSScanner scannerWithString:formats];
|
||||
NSString *formatName;
|
||||
while ([formatScanner scanUpToString:@"," intoString:&formatName]) {
|
||||
[formatScanner scanString:@"," intoString:nil];
|
||||
[formatsArray addObject:formatName];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
while ([sc scanUpToString:@"Style:" intoString:nil] || [sc scanString:@"Style:" intoString:nil]) {
|
||||
[sc scanString:@"Style:" intoString:nil];
|
||||
NSString *styles;
|
||||
if ([sc scanUpToString:@"\n" intoString:&styles]) {
|
||||
NSMutableArray<NSString *> *stylesValueArray = [NSMutableArray array];
|
||||
|
||||
NSScanner *stylesScanner = [NSScanner scannerWithString:styles];
|
||||
NSString *styleValue;
|
||||
while ([stylesScanner scanUpToString:@"," intoString:&styleValue]) {
|
||||
[stylesScanner scanString:@"," intoString:nil];
|
||||
[stylesValueArray addObject:styleValue];
|
||||
}
|
||||
|
||||
MP42SSAStyle *style = [[MP42SSAStyle alloc] initWith:stylesValueArray formats:formatsArray];
|
||||
if (style) {
|
||||
[stylesArray addObject:style];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return stylesArray;
|
||||
}
|
||||
|
||||
- (NSArray<NSString *> *)parseFormat:(NSString *)format
|
||||
{
|
||||
NSScanner *sc = [NSScanner scannerWithString:format];
|
||||
NSMutableArray<NSString *> *formatsArray = [NSMutableArray array];
|
||||
|
||||
if ([sc scanUpToString:@"Format:" intoString:nil] || [sc scanString:@"Format:" intoString:nil]) {
|
||||
[sc scanString:@"Format:" intoString:nil];
|
||||
NSString *formats;
|
||||
if ([sc scanUpToString:@"\n" intoString:&formats]) {
|
||||
NSScanner *formatScanner = [NSScanner scannerWithString:formats];
|
||||
NSString *formatName;
|
||||
while ([formatScanner scanUpToString:@"," intoString:&formatName]) {
|
||||
[formatScanner scanString:@"," intoString:nil];
|
||||
[formatsArray addObject:formatName];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return formatsArray;
|
||||
}
|
||||
|
||||
- (void)parseLines:(NSString *)lines
|
||||
{
|
||||
NSScanner *sc = [NSScanner scannerWithString:lines];
|
||||
NSMutableArray<MP42SSALine *> *linesArray = [NSMutableArray array];
|
||||
|
||||
[sc scanUpToString:@"[Events]" intoString:nil];
|
||||
[sc scanUpToString:@"Dialogue: " intoString:nil];
|
||||
|
||||
NSString *lineString;
|
||||
|
||||
while ([sc scanUpToString:@"\n" intoString:&lineString]) {
|
||||
MP42SSALine *line = [[MP42SSALine alloc] initWithString:lineString format:_format];
|
||||
if (line) {
|
||||
[linesArray addObject:line];
|
||||
}
|
||||
}
|
||||
|
||||
_lines_internal = linesArray;
|
||||
}
|
||||
|
||||
#pragma mark - Line
|
||||
|
||||
- (NSArray<MP42SSALine *> *)lines {
|
||||
return [_lines_internal copy];
|
||||
}
|
||||
|
||||
- (void)addLine:(NSString *)lineString
|
||||
{
|
||||
MP42SSALine *line = [[MP42SSALine alloc] initWithString:lineString format:_format];
|
||||
if (line) {
|
||||
[_lines_internal addObject:line];
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in new issue
Block a user