-
Notifications
You must be signed in to change notification settings - Fork 8
/
SubtitlesConverter.m
298 lines (248 loc) · 10.6 KB
/
SubtitlesConverter.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
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
//
// MGSubtitlesConverter.m
// WhatSub
//
// Created by Marcin Grabda on 1/26/10.
// Copyright 2010 Marcin Grabda. All rights reserved.
//
#import "SubtitlesConverter.h"
#import "FrameRateCalculator.h"
#import "RegexKitLite.h"
#import "AppController.h"
@implementation SubtitlesConverter
NSString* const TMP_REGEX = @"^(\\d+):(\\d+):(\\d+):(.*)";
NSString* const MDVD_REGEX = @"^\\{(\\d+)\\}\\{(\\d+)\\}(.*)";
NSString* const MPL2_REGEX = @"^\\[(\\d+)\\]\\[(\\d+)\\](.*)";
- (id)initWithSupportedMovieExtensions:(NSArray*)extensions andDefaultFrameRate:(NSNumber*)frameRate
{
self = [super init];
if (self)
{
movieExtensions = extensions;
defaultFrameRate = frameRate;
}
return self;
}
- (void)convert:(NSString*)inputPath toFile:(NSString*)outputPath forMovie:(NSString*)moviePath withEncoding:(NSStringEncoding)encoding
{
NSLog(@"Processing file %@", inputPath);
NSArray* fileContents = [self readFile:inputPath];
NSString* firstLine = [fileContents objectAtIndex:0];
NSArray* subRipArray = nil;
if ([[firstLine captureComponentsMatchedByRegex:TMP_REGEX] count] > 0)
{
subRipArray = [self processTMPlayer:fileContents];
}
else if ([[firstLine captureComponentsMatchedByRegex:MDVD_REGEX] count] > 0)
{
if (moviePath == nil)
{
NSString* partialPath = [inputPath stringByDeletingPathExtension];
for (NSString* ext in movieExtensions)
{
NSString* path = [partialPath stringByAppendingPathExtension:ext];
if ([[NSFileManager defaultManager] fileExistsAtPath:path])
{
NSLog(@"Movie file found at %@", path);
moviePath = path;
break;
}
}
}
subRipArray = [self processMicroDVD:fileContents forMovie:moviePath];
}
else if ([[firstLine captureComponentsMatchedByRegex:MPL2_REGEX] count] > 0)
{
subRipArray = [self processMPL2:fileContents];
}
else
{
NSString* reason = @"Unknown subtitles format";
NSException* e = [NSException exceptionWithName:@"SubtitlesException" reason:reason userInfo:nil];
@throw e;
}
if (subRipArray != nil)
{
[self printSubRip:subRipArray toFile:outputPath withEncoding:encoding];
}
}
- (void)convertWithoutProcessing:(NSString*)inputPath toFile:(NSString*)outputPath withEncoding:(NSStringEncoding)encoding
{
NSLog(@"Converting file %@", inputPath);
NSArray* fileContents = [self readFile:inputPath];
NSMutableString* entireText = [NSMutableString string];
for (NSArray* line in fileContents)
{
NSString* linePrint = [NSString stringWithFormat:@"%@\n", line, nil];
[entireText appendString:linePrint];
}
NSData *data = [entireText dataUsingEncoding:encoding allowLossyConversion:YES];
[data writeToFile:outputPath atomically:YES];
}
- (NSArray*)readFile:(NSString*)pathToFile
{
NSError* error = nil;
NSStringEncoding encoding = -1;
NSString* fileContents = [NSString stringWithContentsOfFile:pathToFile usedEncoding:&encoding error:&error];
if (fileContents == nil)
{
NSArray* encodings = [NSArray arrayWithObjects:
[NSNumber numberWithUnsignedInteger:NSUTF8StringEncoding],
[NSNumber numberWithUnsignedInteger:NSWindowsCP1250StringEncoding],
[NSNumber numberWithUnsignedInteger:NSWindowsCP1252StringEncoding],
[NSNumber numberWithUnsignedInteger:NSISOLatin2StringEncoding],
[NSNumber numberWithUnsignedInteger:NSISOLatin1StringEncoding], nil];
for (int i = 0; i < [encodings count] && fileContents == nil; i++)
{
encoding = [[encodings objectAtIndex:i] unsignedIntegerValue];
fileContents = [NSString stringWithContentsOfFile:pathToFile encoding:encoding error:&error];
}
if (fileContents == nil)
{
NSString* reason = @"Unknown file encoding";
NSException* e = [NSException exceptionWithName:@"SubtitlesException" reason:reason userInfo:nil];
@throw e;
}
}
NSLog(@"Encoding guessed: %@", [NSString localizedNameOfStringEncoding:encoding]);
// try LF first
NSArray *lines = [fileContents componentsSeparatedByString:@"\n"];
// if everything is in one line, try with CR again
if ([lines count] < 2) {
lines = [fileContents componentsSeparatedByString:@"\r"];
}
return lines;
}
- (NSArray*)processMicroDVD:(NSArray *)lines forMovie:(NSString *)moviePath
{
double framerate = [defaultFrameRate doubleValue];
if (moviePath != nil && [[NSFileManager defaultManager] fileExistsAtPath:moviePath])
{
framerate = [FrameRateCalculator calculateFrameRateForMovieInPath:moviePath];
}
NSArray* capturesArray = nil;
NSMutableArray* outputArray = [[NSMutableArray alloc] init];
for (NSString* line in lines) {
capturesArray = [line captureComponentsMatchedByRegex:MDVD_REGEX];
if ([capturesArray count] > 3)
{
int frameStart = [[capturesArray objectAtIndex:1] integerValue];
double startTime = (double)frameStart / framerate;
int frameEnd = [[capturesArray objectAtIndex:2] integerValue];
double endTime = (double)frameEnd / framerate;
NSString* text = [capturesArray objectAtIndex:3];
NSArray* singleLineArray = [NSArray arrayWithObjects:
[NSNumber numberWithDouble:startTime],
[NSNumber numberWithDouble:endTime],
text, nil];
[outputArray addObject:singleLineArray];
}
}
return outputArray;
}
- (NSArray*)processMPL2:(NSArray *)lines
{
NSArray* capturesArray = nil;
NSMutableArray* outputArray = [[NSMutableArray alloc] init];
for (NSString* line in lines) {
capturesArray = [line captureComponentsMatchedByRegex:MPL2_REGEX];
if ([capturesArray count] > 3)
{
int frameStart = [[capturesArray objectAtIndex:1] integerValue];
double startTime = (double)frameStart / 10;
int frameEnd = [[capturesArray objectAtIndex:2] integerValue];
double endTime = (double)frameEnd / 10;
NSString* text = [capturesArray objectAtIndex:3];
NSArray* singleLineArray = [NSArray arrayWithObjects:
[NSNumber numberWithDouble:startTime],
[NSNumber numberWithDouble:endTime],
text, nil];
[outputArray addObject:singleLineArray];
}
}
return outputArray;
}
- (NSArray*)processTMPlayer:(NSArray *)lines
{
NSArray* capturesArray = nil;
NSMutableArray* outputArray = [[NSMutableArray alloc] init];
for (NSString* line in lines) {
capturesArray = [line captureComponentsMatchedByRegex:TMP_REGEX];
if ([capturesArray count] > 4)
{
int hour = [[capturesArray objectAtIndex:1] integerValue];
int minute = [[capturesArray objectAtIndex:2] integerValue];
int second = [[capturesArray objectAtIndex:3] integerValue];
int startTime = hour * 3600 + minute * 60 + second;
int endTime = startTime + 5;
NSString* text = [capturesArray objectAtIndex:4];
NSMutableArray* singleLineArray = [NSMutableArray arrayWithObjects:
[NSNumber numberWithInteger:startTime],
[NSNumber numberWithInteger:endTime],
text, nil];
[outputArray addObject:singleLineArray];
}
}
NSMutableArray* previousLineArray = nil;
int previousEndTime = 0;
for (NSMutableArray* singleLineArray in outputArray)
{
int startTime = [[singleLineArray objectAtIndex:0] integerValue];
int endTime = [[singleLineArray objectAtIndex:1] integerValue];
if (previousEndTime > startTime)
{
[previousLineArray replaceObjectAtIndex:1 withObject:[NSNumber numberWithInteger:startTime]];
}
previousLineArray = singleLineArray;
previousEndTime = endTime;
}
return outputArray;
}
- (void)printSubRip:(NSArray *)input toFile:(NSString *)srtFilePath withEncoding:(NSStringEncoding)encoding
{
NSMutableString* entireText = [NSMutableString string];
int lineNumber = 1;
for (NSArray* line in input)
{
NSNumber* start = [line objectAtIndex:0];
NSString* formattedStart = [self formatSubRipTime:start];
NSNumber* end = [line objectAtIndex:1];
NSString* formattedEnd = [self formatSubRipTime:end];
NSString* text = [line objectAtIndex:2];
NSString* formattedText = [self formatSubRipText:text];
NSString* linePrint = [NSString stringWithFormat:@"%d\n%@ --> %@\n%@\n\n",
lineNumber++, formattedStart, formattedEnd, formattedText, nil];
[entireText appendString:linePrint];
}
NSData *data = [entireText dataUsingEncoding:encoding allowLossyConversion:YES];
[data writeToFile:srtFilePath atomically:YES];
}
/* time conversion from miliseconds */
- (NSString*)formatSubRipTime:(NSNumber*)value
{
int intValue = [value intValue];
float floatValue = [value floatValue];
floatValue -= intValue;
int miliseconds = floatValue * 1000;
int hours = intValue / 3600;
intValue -= hours * 3600;
int minutes = intValue / 60;
intValue -= minutes * 60;
int seconds = intValue;
return [NSString stringWithFormat:@"%02d:%02d:%02d,%03d", hours, minutes, seconds, miliseconds, nil];
}
/* TODO additional formatting for text */
- (NSString*)formatSubRipText:(NSString*)value
{
/*
NSMutableString* mutableValue = [[NSMutableString alloc] initWithString:value];
NSRange range = NSMakeRange(0, [mutableValue length]);
[mutableValue replaceOccurrencesOfString:@"/" withString:@"" options:NSLiteralSearch range:range];
[mutableValue replaceOccurrencesOfString:@"|" withString:@"\n" options:NSLiteralSearch range:range];
return mutableValue;
*/
value = [value stringByReplacingOccurrencesOfString:@"/" withString:@""];
value = [value stringByReplacingOccurrencesOfString:@"|" withString:@"\n"];
return value;
}
@end