Commit ab00a4da by Oliver Woodman

Allow non-strict webvtt parsing.

parent 7dfebc2e
...@@ -63,6 +63,16 @@ public class WebvttParser implements SubtitleParser { ...@@ -63,6 +63,16 @@ public class WebvttParser implements SubtitleParser {
private static final Pattern MEDIA_TIMESTAMP_OFFSET = Pattern.compile(OFFSET + "\\d+"); private static final Pattern MEDIA_TIMESTAMP_OFFSET = Pattern.compile(OFFSET + "\\d+");
private static final Pattern MEDIA_TIMESTAMP = Pattern.compile("MPEGTS:\\d+"); private static final Pattern MEDIA_TIMESTAMP = Pattern.compile("MPEGTS:\\d+");
private final boolean strictParsing;
public WebvttParser() {
this(true);
}
public WebvttParser(boolean strictParsing) {
this.strictParsing = strictParsing;
}
@Override @Override
public WebvttSubtitle parse(InputStream inputStream, String inputEncoding, long startTimeUs) public WebvttSubtitle parse(InputStream inputStream, String inputEncoding, long startTimeUs)
throws IOException { throws IOException {
...@@ -108,7 +118,7 @@ public class WebvttParser implements SubtitleParser { ...@@ -108,7 +118,7 @@ public class WebvttParser implements SubtitleParser {
Matcher matcher = WEBVTT_METADATA_HEADER.matcher(line); Matcher matcher = WEBVTT_METADATA_HEADER.matcher(line);
if (!matcher.find()) { if (!matcher.find()) {
throw new ParserException("Expected webvtt metadata header; got: " + line); handleNoncompliantLine(line);
} }
if (line.startsWith("X-TIMESTAMP-MAP")) { if (line.startsWith("X-TIMESTAMP-MAP")) {
...@@ -182,6 +192,12 @@ public class WebvttParser implements SubtitleParser { ...@@ -182,6 +192,12 @@ public class WebvttParser implements SubtitleParser {
return startTimeUs; return startTimeUs;
} }
protected void handleNoncompliantLine(String line) throws ParserException {
if (strictParsing) {
throw new ParserException("Unexpected line: " + line);
}
}
private static long parseTimestampUs(String s) throws NumberFormatException { private static long parseTimestampUs(String s) throws NumberFormatException {
if (!s.matches(WEBVTT_TIMESTAMP_STRING)) { if (!s.matches(WEBVTT_TIMESTAMP_STRING)) {
throw new NumberFormatException("has invalid format"); throw new NumberFormatException("has invalid format");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment