1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-12-23 16:28:51 -05:00

* some subtitle parser optimizations

This commit is contained in:
Reinhard Pointner 2009-07-10 15:12:16 +00:00
parent cc2bb53910
commit 9c456b275a
5 changed files with 28 additions and 19 deletions

View File

@ -7,7 +7,6 @@ import static net.sourceforge.tuned.StringUtilities.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Pattern;
public class MicroDVDReader extends SubtitleReader {
@ -46,8 +45,8 @@ public class MicroDVDReader extends SubtitleReader {
return null;
}
long startFrame = Long.parseLong(properties.get(0));
long endFrame = Long.parseLong(properties.get(1));
int startFrame = Integer.parseInt(properties.get(0));
int endFrame = Integer.parseInt(properties.get(1));
String text = line.substring(from).trim();
if (startFrame == 1 && endFrame == 1) {
@ -59,7 +58,7 @@ public class MicroDVDReader extends SubtitleReader {
}
// translate '|' to new lines
String[] lines = text.split(Pattern.quote("|"));
String[] lines = text.split("[|]");
// convert frame interval to time interval
return new SubtitleElement(Math.round(startFrame * fps), Math.round(endFrame * fps), join(lines, "\n"));

View File

@ -46,8 +46,8 @@ public class SubRipReader extends SubtitleReader {
List<String> lines = new ArrayList<String>(2);
// read text
for (String line = scanner.nextLine(); !line.isEmpty() && scanner.hasNextLine(); line = scanner.nextLine()) {
// read all lines until the next empty line
for (String line = scanner.nextLine(); line.length() > 0; line = scanner.hasNextLine() ? scanner.nextLine() : "") {
lines.add(line);
}

View File

@ -16,6 +16,7 @@ import java.util.regex.Pattern;
public class SubStationAlphaReader extends SubtitleReader {
private final DateFormat timeFormat = new SubtitleTimeFormat();
private final Pattern newline = Pattern.compile(Pattern.quote("\\n"), Pattern.CASE_INSENSITIVE);
private Map<String, Integer> format;
@ -77,7 +78,7 @@ public class SubStationAlphaReader extends SubtitleReader {
String text = row[format.get("Text")].trim();
// translate "\\n" to new lines
String[] lines = Pattern.compile(Pattern.quote("\\N"), Pattern.CASE_INSENSITIVE).split(text);
String[] lines = newline.split(text);
return new SubtitleElement(start, end, join(lines, "\n"));
}

View File

@ -13,6 +13,7 @@ import java.util.regex.Pattern;
public class SubViewerReader extends SubtitleReader {
private final DateFormat timeFormat = new SubtitleTimeFormat();
private final Pattern newline = Pattern.compile(Pattern.quote("[br]"), Pattern.CASE_INSENSITIVE);
public SubViewerReader(Scanner scanner) {
@ -35,7 +36,7 @@ public class SubViewerReader extends SubtitleReader {
long t2 = timeFormat.parse(interval[1]).getTime();
// translate [br] to new lines
String[] lines = scanner.nextLine().split(Pattern.quote("[br]"));
String[] lines = newline.split(scanner.nextLine());
return new SubtitleElement(t1, t2, join(lines, "\n"));
} catch (InputMismatchException e) {

View File

@ -8,8 +8,8 @@ import java.text.ParsePosition;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.Scanner;
import java.util.TimeZone;
import java.util.regex.Pattern;
class SubtitleTimeFormat extends DateFormat {
@ -36,24 +36,32 @@ class SubtitleTimeFormat extends DateFormat {
}
private final Pattern delimiter = Pattern.compile("[:.]");
@Override
public Date parse(String source, ParsePosition pos) {
Scanner scanner = new Scanner(source).useDelimiter(":|\\.");
String[] split = delimiter.split(source, 4);
// reset state
calendar.clear();
// handle hours:minutes:seconds
calendar.set(Calendar.HOUR_OF_DAY, scanner.nextInt());
calendar.set(Calendar.MINUTE, scanner.nextInt());
calendar.set(Calendar.SECOND, scanner.nextInt());
try {
// handle hours:minutes:seconds
calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(split[0]));
calendar.set(Calendar.MINUTE, Integer.parseInt(split[1]));
calendar.set(Calendar.SECOND, Integer.parseInt(split[2]));
// handle hundredth seconds
calendar.set(Calendar.MILLISECOND, scanner.nextInt() * 10);
// handle hundredth seconds
calendar.set(Calendar.MILLISECOND, Integer.parseInt(split[3]) * 10);
} catch (Exception e) {
// cannot parse input
pos.setErrorIndex(0);
return null;
}
// update position
pos.setIndex(scanner.match().end());
pos.setIndex(source.length());
return calendar.getTime();
}
}