Use [NSString stringEncodingForData:encodingOptions:convertedString:usedLossyConversion:] to try to load text files with unknown encoding.
This commit is contained in:
4 files changed
+48
-59
No files matched your search
@@ -100,7 +100,7 @@
|
|||||||
MP42SSAConverter *converter = [[MP42SSAConverter alloc] initWithParser:_parser];
|
MP42SSAConverter *converter = [[MP42SSAConverter alloc] initWithParser:_parser];
|
||||||
|
|
||||||
MP42SubSerializer *ss = [[MP42SubSerializer alloc] init];
|
MP42SubSerializer *ss = [[MP42SubSerializer alloc] init];
|
||||||
[ss setSSA:YES];
|
ss.ssa = YES;
|
||||||
|
|
||||||
for (MP42SSALine *line in _parser.lines) {
|
for (MP42SSALine *line in _parser.lines) {
|
||||||
NSString *text = [converter convertLine:line];
|
NSString *text = [converter convertLine:line];
|
||||||
|
|||||||
+17
-20
@@ -11,6 +11,8 @@
|
|||||||
#import "MP42TextSample.h"
|
#import "MP42TextSample.h"
|
||||||
#import "mp4v2.h"
|
#import "mp4v2.h"
|
||||||
|
|
||||||
|
NS_ASSUME_NONNULL_BEGIN
|
||||||
|
|
||||||
@interface MP42SubLine : NSObject
|
@interface MP42SubLine : NSObject
|
||||||
{
|
{
|
||||||
@public
|
@public
|
||||||
@@ -25,26 +27,18 @@
|
|||||||
@end
|
@end
|
||||||
|
|
||||||
@interface MP42SubSerializer : NSObject
|
@interface MP42SubSerializer : NSObject
|
||||||
{
|
|
||||||
// input lines, sorted by 1. beginning time 2. original insertion order
|
|
||||||
NSMutableArray<MP42SubLine *> *lines;
|
|
||||||
BOOL finished;
|
|
||||||
|
|
||||||
BOOL position_information, forced;
|
|
||||||
unsigned last_begin_time, last_end_time;
|
|
||||||
unsigned linesInput;
|
|
||||||
|
|
||||||
BOOL ssa;
|
|
||||||
}
|
|
||||||
- (void)addLine:(MP42SubLine *)sline;
|
- (void)addLine:(MP42SubLine *)sline;
|
||||||
-(void)setFinished:(BOOL)finished;
|
|
||||||
-(MP42SubLine *)getSerializedPacket;
|
@property (nonatomic) BOOL finished;
|
||||||
-(BOOL)isEmpty;
|
|
||||||
-(BOOL)positionInformation;
|
- (nullable MP42SubLine *)getSerializedPacket;
|
||||||
-(void)setPositionInformation:(BOOL)info;
|
|
||||||
-(BOOL)forced;
|
@property (nonatomic, readonly) BOOL isEmpty;
|
||||||
-(void)setForced:(BOOL)info;
|
|
||||||
-(void)setSSA:(BOOL)ssa;
|
@property (nonatomic) BOOL positionInformation;
|
||||||
|
@property (nonatomic) BOOL forced;
|
||||||
|
@property (nonatomic) BOOL ssa;
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@@ -68,6 +62,9 @@ typedef struct {
|
|||||||
UInt32 duration;
|
UInt32 duration;
|
||||||
} PacketControlData;
|
} PacketControlData;
|
||||||
|
|
||||||
int ExtractVobSubPacket(UInt8 *dest, UInt8 *framedSrc, int srcSize, int *usedSrcBytes, int index);
|
int ExtractVobSubPacket(UInt8 *dest, UInt8 *framedSrc, int srcSize, int * _Nullable usedSrcBytes, int index);
|
||||||
ComponentResult ReadPacketControls(UInt8 *packet, UInt32 palette[16], PacketControlData *controlDataOut,BOOL *forced);
|
ComponentResult ReadPacketControls(UInt8 *packet, UInt32 palette[_Nonnull 16], PacketControlData *controlDataOut, BOOL *forced);
|
||||||
Boolean ReadPacketTimes(uint8_t *packet, uint32_t length, uint16_t *startTime, uint16_t *endTime, uint8_t *forced);
|
Boolean ReadPacketTimes(uint8_t *packet, uint32_t length, uint16_t *startTime, uint16_t *endTime, uint8_t *forced);
|
||||||
|
|
||||||
|
NS_ASSUME_NONNULL_END
|
||||||
|
|
||||||
+29
-37
@@ -11,12 +11,19 @@
|
|||||||
#import "MP42HtmlParser.h"
|
#import "MP42HtmlParser.h"
|
||||||
|
|
||||||
@implementation MP42SubSerializer
|
@implementation MP42SubSerializer
|
||||||
|
{
|
||||||
|
// input lines, sorted by 1. beginning time 2. original insertion order
|
||||||
|
NSMutableArray<MP42SubLine *> *lines;
|
||||||
|
|
||||||
|
unsigned last_begin_time, last_end_time;
|
||||||
|
unsigned linesInput;
|
||||||
|
}
|
||||||
|
|
||||||
-(instancetype)init
|
-(instancetype)init
|
||||||
{
|
{
|
||||||
if ((self = [super init])) {
|
if ((self = [super init])) {
|
||||||
lines = [[NSMutableArray alloc] init];
|
lines = [[NSMutableArray alloc] init];
|
||||||
finished = NO;
|
_finished = NO;
|
||||||
last_begin_time = last_end_time = 0;
|
last_begin_time = last_end_time = 0;
|
||||||
linesInput = 0;
|
linesInput = 0;
|
||||||
}
|
}
|
||||||
@@ -68,7 +75,7 @@ static CFComparisonResult CompareLinesByBeginTime(const void *a, const void *b,
|
|||||||
NSMutableString *str;
|
NSMutableString *str;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
if (!finished) {
|
if (!_finished) {
|
||||||
if (nlines > 1) {
|
if (nlines > 1) {
|
||||||
unsigned maxEndTime = first->end_time;
|
unsigned maxEndTime = first->end_time;
|
||||||
|
|
||||||
@@ -105,7 +112,7 @@ canOutput:
|
|||||||
if (l->begin_time <= begin_time) {
|
if (l->begin_time <= begin_time) {
|
||||||
// Try to be a bit smart and avoid duplicated lines
|
// Try to be a bit smart and avoid duplicated lines
|
||||||
// from ssa.
|
// from ssa.
|
||||||
if (!ssa || [str rangeOfString:l->line].location == NSNotFound) {
|
if (!_ssa || [str rangeOfString:l->line].location == NSNotFound) {
|
||||||
[str appendString:l->line];
|
[str appendString:l->line];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -145,44 +152,15 @@ canOutput:
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
-(void)setFinished:(BOOL)_finished
|
|
||||||
{
|
|
||||||
finished = _finished;
|
|
||||||
}
|
|
||||||
|
|
||||||
-(BOOL)isEmpty
|
-(BOOL)isEmpty
|
||||||
{
|
{
|
||||||
return [lines count] == 0;
|
return [lines count] == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
-(BOOL)positionInformation
|
|
||||||
{
|
|
||||||
return position_information;
|
|
||||||
}
|
|
||||||
|
|
||||||
-(void)setPositionInformation:(BOOL)info
|
|
||||||
{
|
|
||||||
position_information = info;
|
|
||||||
}
|
|
||||||
|
|
||||||
-(BOOL)forced
|
|
||||||
{
|
|
||||||
return forced;
|
|
||||||
}
|
|
||||||
|
|
||||||
-(void)setForced:(BOOL)info
|
|
||||||
{
|
|
||||||
forced = info;
|
|
||||||
}
|
|
||||||
|
|
||||||
-(void)setSSA:(BOOL)isSSA
|
|
||||||
{
|
|
||||||
ssa = isSSA;
|
|
||||||
}
|
|
||||||
|
|
||||||
-(NSString *)description
|
-(NSString *)description
|
||||||
{
|
{
|
||||||
return [NSString stringWithFormat:@"lines left: %lu finished inputting: %d",(unsigned long)[lines count],finished];
|
return [NSString stringWithFormat:@"lines left: %lu finished inputting: %d",(unsigned long)[lines count],_finished];
|
||||||
}
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
@@ -297,12 +275,25 @@ extern NSString *STLoadFileWithUnknownEncoding(NSURL *url)
|
|||||||
{
|
{
|
||||||
NSData *data = [NSData dataWithContentsOfURL:url];
|
NSData *data = [NSData dataWithContentsOfURL:url];
|
||||||
|
|
||||||
if (!data)
|
if (!data) {
|
||||||
return nil;
|
return nil;
|
||||||
|
}
|
||||||
|
|
||||||
UniversalDetector *ud = [[UniversalDetector alloc] init];
|
UniversalDetector *ud = [[UniversalDetector alloc] init];
|
||||||
NSString *res = nil;
|
NSString *res = nil;
|
||||||
NSStringEncoding enc;
|
NSStringEncoding enc;
|
||||||
|
|
||||||
|
BOOL lossy = NO;
|
||||||
|
enc = [NSString stringEncodingForData:data
|
||||||
|
encodingOptions:nil
|
||||||
|
convertedString:&res
|
||||||
|
usedLossyConversion:&lossy];
|
||||||
|
|
||||||
|
if (res && lossy == NO) {
|
||||||
|
NSLog(@"Encoding %lu failed, retrying.\n", (unsigned long)enc);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
float conf;
|
float conf;
|
||||||
NSString *enc_str;
|
NSString *enc_str;
|
||||||
BOOL latin2;
|
BOOL latin2;
|
||||||
@@ -329,12 +320,13 @@ extern NSString *STLoadFileWithUnknownEncoding(NSURL *url)
|
|||||||
|
|
||||||
if (!res) {
|
if (!res) {
|
||||||
if (latin2) {
|
if (latin2) {
|
||||||
NSLog(@"Encoding %s failed, retrying.\n",[enc_str UTF8String]);
|
NSLog(@"Encoding %s failed, retrying.\n", enc_str.UTF8String);
|
||||||
enc = (enc == NSWindowsCP1252StringEncoding) ? NSWindowsCP1250StringEncoding : NSWindowsCP1252StringEncoding;
|
enc = (enc == NSWindowsCP1252StringEncoding) ? NSWindowsCP1250StringEncoding : NSWindowsCP1252StringEncoding;
|
||||||
res = [[NSString alloc] initWithData:data encoding:enc];
|
res = [[NSString alloc] initWithData:data encoding:enc];
|
||||||
if (!res) NSLog(@"Both of latin1/2 failed.\n");
|
if (!res) NSLog(@"Both of latin1/2 failed.\n");
|
||||||
} else {
|
}
|
||||||
res = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
|
if (!res) {
|
||||||
|
res = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
|
||||||
if (!res) { NSLog(@"ASCII failed."); NSLog(@"Failed to load file as guessed encoding %@.",enc_str); }
|
if (!res) { NSLog(@"ASCII failed."); NSLog(@"Failed to load file as guessed encoding %@.",enc_str); }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -102,7 +102,7 @@
|
|||||||
_parser = [[MP42SSAParser alloc] initWithMKVHeader:cookieString];
|
_parser = [[MP42SSAParser alloc] initWithMKVHeader:cookieString];
|
||||||
_converter = [[MP42SSAConverter alloc] initWithParser:_parser];
|
_converter = [[MP42SSAConverter alloc] initWithParser:_parser];
|
||||||
|
|
||||||
[_ss setSSA:YES];
|
_ss.ssa = YES;
|
||||||
}
|
}
|
||||||
|
|
||||||
_trackID = track.sourceId;
|
_trackID = track.sourceId;
|
||||||
|
|||||||
Reference in new issue
Block a user