From a1ab1fc2a2b7562f699d5af6a89e78ad8fae74e7 Mon Sep 17 00:00:00 2001 From: Oliver Woodman Date: Fri, 26 Jun 2015 14:26:31 +0100 Subject: [PATCH] Detect WebVTT file header according to the spec: 1. An optional U+FEFF BYTE ORDER MARK (BOM) character. 2. The string "WEBVTT". 3. Optionally, either a U+0020 SPACE character or a U+0009 CHARACTER TABULATION (tab) character followed by any number of characters that are not U+000A LINE FEED (LF) or U+000D CARRIAGE RETURN (CR) characters. 4. Exactly one WebVTT line terminators to terminate the line with the file magic and separate it from the rest of the body. Issue: #580 --- .../google/android/exoplayer/text/webvtt/WebvttParser.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/library/src/main/java/com/google/android/exoplayer/text/webvtt/WebvttParser.java b/library/src/main/java/com/google/android/exoplayer/text/webvtt/WebvttParser.java index db5a456168..0503eb2299 100644 --- a/library/src/main/java/com/google/android/exoplayer/text/webvtt/WebvttParser.java +++ b/library/src/main/java/com/google/android/exoplayer/text/webvtt/WebvttParser.java @@ -44,6 +44,10 @@ public class WebvttParser implements SubtitleParser { private static final long SAMPLING_RATE = 90; + private static final String WEBVTT_FILE_HEADER_STRING = "^\uFEFF?WEBVTT((\\u0020|\u0009).*)?$"; + private static final Pattern WEBVTT_FILE_HEADER = + Pattern.compile(WEBVTT_FILE_HEADER_STRING); + private static final String WEBVTT_METADATA_HEADER_STRING = "\\S*[:=]\\S*"; private static final Pattern WEBVTT_METADATA_HEADER = Pattern.compile(WEBVTT_METADATA_HEADER_STRING); @@ -116,7 +120,7 @@ public class WebvttParser implements SubtitleParser { } } - if (!line.equals("WEBVTT") && !line.equals("\uFEFFWEBVTT")) { + if (!WEBVTT_FILE_HEADER.matcher(line).matches()) { throw new ParserException("Expected WEBVTT. Got " + line); }