com.google.android.exoplayer2.text.webvtt.WebvttParserUtil Java Examples

The following examples show how to use com.google.android.exoplayer2.text.webvtt.WebvttParserUtil. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WebvttExtractor.java    From MediaSDK with Apache License 2.0 6 votes vote down vote up
@Override
public boolean sniff(ExtractorInput input) throws IOException, InterruptedException {
  // Check whether there is a header without BOM.
  input.peekFully(
      sampleData, /* offset= */ 0, /* length= */ HEADER_MIN_LENGTH, /* allowEndOfInput= */ false);
  sampleDataWrapper.reset(sampleData, HEADER_MIN_LENGTH);
  if (WebvttParserUtil.isWebvttHeaderLine(sampleDataWrapper)) {
    return true;
  }
  // The header did not match, try including the BOM.
  input.peekFully(
      sampleData,
      /* offset= */ HEADER_MIN_LENGTH,
      HEADER_MAX_LENGTH - HEADER_MIN_LENGTH,
      /* allowEndOfInput= */ false);
  sampleDataWrapper.reset(sampleData, HEADER_MAX_LENGTH);
  return WebvttParserUtil.isWebvttHeaderLine(sampleDataWrapper);
}
 
Example #2
Source File: WebvttCueParser.java    From no-player with Apache License 2.0 6 votes vote down vote up
/**
   * Parses a string containing a list of cue settings.
   *
   * @param cueSettingsList String containing the settings for a given cue.
   * @param builder         The {@link WebvttCue.Builder} where incremental construction takes place.
   */
/* package */
  static void parseCueSettingsList(String cueSettingsList,
                                   WebvttCue.Builder builder) {
      // Parse the cue settings list.
      Matcher cueSettingMatcher = CUE_SETTING_PATTERN.matcher(cueSettingsList);
      while (cueSettingMatcher.find()) {
          String name = cueSettingMatcher.group(1);
          String value = cueSettingMatcher.group(2);
          try {
              if ("line".equals(name)) {
                  parseLineAttribute(value, builder);
              } else if ("align".equals(name)) {
                  builder.setTextAlignment(parseTextAlignment(value));
              } else if ("position".equals(name)) {
                  parsePositionAttribute(value, builder);
              } else if ("size".equals(name)) {
                  builder.setWidth(WebvttParserUtil.parsePercentage(value));
              } else {
                  Log.w(TAG, "Unknown cue setting " + name + ":" + value);
              }
          } catch (NumberFormatException e) {
              Log.w(TAG, "Skipping bad cue setting: " + cueSettingMatcher.group());
          }
      }
  }
 
Example #3
Source File: WebvttCueParser.java    From no-player with Apache License 2.0 6 votes vote down vote up
private static boolean parseCue(String id, Matcher cueHeaderMatcher, ParsableByteArray webvttData,
                                WebvttCue.Builder builder, StringBuilder textBuilder, List<WebvttCssStyle> styles) {
    try {
        // Parse the cue start and end times.
        builder.setStartTime(WebvttParserUtil.parseTimestampUs(cueHeaderMatcher.group(1)))
            .setEndTime(WebvttParserUtil.parseTimestampUs(cueHeaderMatcher.group(2)));
    } catch (NumberFormatException e) {
        Log.w(TAG, "Skipping cue with bad header: " + cueHeaderMatcher.group());
        return false;
    }

    parseCueSettingsList(cueHeaderMatcher.group(3), builder);

    // Parse the cue text.
    textBuilder.setLength(0);
    String line;
    while (!TextUtils.isEmpty(line = webvttData.readLine())) {
        if (textBuilder.length() > 0) {
            textBuilder.append("\n");
        }
        textBuilder.append(line.trim());
    }
    parseCueText(id, textBuilder.toString(), builder, styles);
    return true;
}
 
Example #4
Source File: WebvttCueParser.java    From no-player with Apache License 2.0 6 votes vote down vote up
private static void parseLineAttribute(String s, WebvttCue.Builder builder)
    throws NumberFormatException {
    int commaIndex = s.indexOf(',');
    if (commaIndex != -1) {
        builder.setLineAnchor(parsePositionAnchor(s.substring(commaIndex + 1)));
        s = s.substring(0, commaIndex);
    } else {
        builder.setLineAnchor(Cue.TYPE_UNSET);
    }
    if (s.endsWith("%")) {
        builder.setLine(WebvttParserUtil.parsePercentage(s)).setLineType(Cue.LINE_TYPE_FRACTION);
    } else {
        int lineNumber = Integer.parseInt(s);
        if (lineNumber < 0) {
            // WebVTT defines line -1 as last visible row when lineAnchor is ANCHOR_TYPE_START, where-as
            // Cue defines it to be the first row that's not visible.
            lineNumber--;
        }
        builder.setLine(lineNumber).setLineType(Cue.LINE_TYPE_NUMBER);
    }
}
 
Example #5
Source File: WebvttExtractor.java    From Telegram-FOSS with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean sniff(ExtractorInput input) throws IOException, InterruptedException {
  // Check whether there is a header without BOM.
  input.peekFully(
      sampleData, /* offset= */ 0, /* length= */ HEADER_MIN_LENGTH, /* allowEndOfInput= */ false);
  sampleDataWrapper.reset(sampleData, HEADER_MIN_LENGTH);
  if (WebvttParserUtil.isWebvttHeaderLine(sampleDataWrapper)) {
    return true;
  }
  // The header did not match, try including the BOM.
  input.peekFully(
      sampleData,
      /* offset= */ HEADER_MIN_LENGTH,
      HEADER_MAX_LENGTH - HEADER_MIN_LENGTH,
      /* allowEndOfInput= */ false);
  sampleDataWrapper.reset(sampleData, HEADER_MAX_LENGTH);
  return WebvttParserUtil.isWebvttHeaderLine(sampleDataWrapper);
}
 
Example #6
Source File: WebvttExtractor.java    From Telegram with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean sniff(ExtractorInput input) throws IOException, InterruptedException {
  // Check whether there is a header without BOM.
  input.peekFully(
      sampleData, /* offset= */ 0, /* length= */ HEADER_MIN_LENGTH, /* allowEndOfInput= */ false);
  sampleDataWrapper.reset(sampleData, HEADER_MIN_LENGTH);
  if (WebvttParserUtil.isWebvttHeaderLine(sampleDataWrapper)) {
    return true;
  }
  // The header did not match, try including the BOM.
  input.peekFully(
      sampleData,
      /* offset= */ HEADER_MIN_LENGTH,
      HEADER_MAX_LENGTH - HEADER_MIN_LENGTH,
      /* allowEndOfInput= */ false);
  sampleDataWrapper.reset(sampleData, HEADER_MAX_LENGTH);
  return WebvttParserUtil.isWebvttHeaderLine(sampleDataWrapper);
}
 
Example #7
Source File: WebvttDecoder.java    From no-player with Apache License 2.0 5 votes vote down vote up
@Override
protected WebvttSubtitle decode(byte[] bytes, int length, boolean reset)
    throws SubtitleDecoderException {
  parsableWebvttData.reset(bytes, length);
  // Initialization for consistent starting state.
  webvttCueBuilder.reset();
  definedStyles.clear();

  // Validate the first line of the header, and skip the remainder.
  try {
    WebvttParserUtil.validateWebvttHeaderLine(parsableWebvttData);
  } catch (ParserException e) {
    throw new SubtitleDecoderException(e);
  }
  while (!TextUtils.isEmpty(parsableWebvttData.readLine())) {
  }

  int event;
  ArrayList<WebvttCue> subtitles = new ArrayList<>();
  while ((event = getNextEvent(parsableWebvttData)) != EVENT_END_OF_FILE) {
    if (event == EVENT_COMMENT) {
      skipComment(parsableWebvttData);
    } else if (event == EVENT_STYLE_BLOCK) {
      if (!subtitles.isEmpty()) {
        throw new SubtitleDecoderException("A style block was found after the first cue.");
      }
      parsableWebvttData.readLine(); // Consume the "STYLE" header.
      WebvttCssStyle styleBlock = cssParser.parseBlock(parsableWebvttData);
      if (styleBlock != null) {
        definedStyles.add(styleBlock);
      }
    } else if (event == EVENT_CUE) {
      if (cueParser.parseCue(parsableWebvttData, webvttCueBuilder, definedStyles)) {
        subtitles.add(webvttCueBuilder.build());
        webvttCueBuilder.reset();
      }
    }
  }
  return new WebvttSubtitle(subtitles);
}
 
Example #8
Source File: WebvttCueParser.java    From no-player with Apache License 2.0 5 votes vote down vote up
private static void parsePositionAttribute(String s, WebvttCue.Builder builder)
    throws NumberFormatException {
    int commaIndex = s.indexOf(',');
    if (commaIndex != -1) {
        builder.setPositionAnchor(parsePositionAnchor(s.substring(commaIndex + 1)));
        s = s.substring(0, commaIndex);
    } else {
        builder.setPositionAnchor(Cue.TYPE_UNSET);
    }
    builder.setPosition(WebvttParserUtil.parsePercentage(s));
}
 
Example #9
Source File: WebvttExtractor.java    From MediaSDK with Apache License 2.0 4 votes vote down vote up
@RequiresNonNull("output")
private void processSample() throws ParserException {
  ParsableByteArray webvttData = new ParsableByteArray(sampleData);

  // Validate the first line of the header.
  WebvttParserUtil.validateWebvttHeaderLine(webvttData);

  // Defaults to use if the header doesn't contain an X-TIMESTAMP-MAP header.
  long vttTimestampUs = 0;
  long tsTimestampUs = 0;

  // Parse the remainder of the header looking for X-TIMESTAMP-MAP.
  for (String line = webvttData.readLine();
      !TextUtils.isEmpty(line);
      line = webvttData.readLine()) {
    if (line.startsWith("X-TIMESTAMP-MAP")) {
      Matcher localTimestampMatcher = LOCAL_TIMESTAMP.matcher(line);
      if (!localTimestampMatcher.find()) {
        throw new ParserException("X-TIMESTAMP-MAP doesn't contain local timestamp: " + line);
      }
      Matcher mediaTimestampMatcher = MEDIA_TIMESTAMP.matcher(line);
      if (!mediaTimestampMatcher.find()) {
        throw new ParserException("X-TIMESTAMP-MAP doesn't contain media timestamp: " + line);
      }
      vttTimestampUs = WebvttParserUtil.parseTimestampUs(localTimestampMatcher.group(1));
      tsTimestampUs = TimestampAdjuster.ptsToUs(Long.parseLong(mediaTimestampMatcher.group(1)));
    }
  }

  // Find the first cue header and parse the start time.
  Matcher cueHeaderMatcher = WebvttParserUtil.findNextCueHeader(webvttData);
  if (cueHeaderMatcher == null) {
    // No cues found. Don't output a sample, but still output a corresponding track.
    buildTrackOutput(0);
    return;
  }

  long firstCueTimeUs = WebvttParserUtil.parseTimestampUs(cueHeaderMatcher.group(1));
  long sampleTimeUs = timestampAdjuster.adjustTsTimestamp(
      TimestampAdjuster.usToPts(firstCueTimeUs + tsTimestampUs - vttTimestampUs));
  long subsampleOffsetUs = sampleTimeUs - firstCueTimeUs;
  // Output the track.
  TrackOutput trackOutput = buildTrackOutput(subsampleOffsetUs);
  // Output the sample.
  sampleDataWrapper.reset(sampleData, sampleSize);
  trackOutput.sampleData(sampleDataWrapper, sampleSize);
  trackOutput.sampleMetadata(sampleTimeUs, C.BUFFER_FLAG_KEY_FRAME, sampleSize, 0, null);
}
 
Example #10
Source File: WebvttExtractor.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
private void processSample() throws ParserException {
  ParsableByteArray webvttData = new ParsableByteArray(sampleData);

  // Validate the first line of the header.
  try {
    WebvttParserUtil.validateWebvttHeaderLine(webvttData);
  } catch (SubtitleDecoderException e) {
    throw new ParserException(e);
  }

  // Defaults to use if the header doesn't contain an X-TIMESTAMP-MAP header.
  long vttTimestampUs = 0;
  long tsTimestampUs = 0;

  // Parse the remainder of the header looking for X-TIMESTAMP-MAP.
  String line;
  while (!TextUtils.isEmpty(line = webvttData.readLine())) {
    if (line.startsWith("X-TIMESTAMP-MAP")) {
      Matcher localTimestampMatcher = LOCAL_TIMESTAMP.matcher(line);
      if (!localTimestampMatcher.find()) {
        throw new ParserException("X-TIMESTAMP-MAP doesn't contain local timestamp: " + line);
      }
      Matcher mediaTimestampMatcher = MEDIA_TIMESTAMP.matcher(line);
      if (!mediaTimestampMatcher.find()) {
        throw new ParserException("X-TIMESTAMP-MAP doesn't contain media timestamp: " + line);
      }
      vttTimestampUs = WebvttParserUtil.parseTimestampUs(localTimestampMatcher.group(1));
      tsTimestampUs = TimestampAdjuster.ptsToUs(Long.parseLong(mediaTimestampMatcher.group(1)));
    }
  }

  // Find the first cue header and parse the start time.
  Matcher cueHeaderMatcher = WebvttParserUtil.findNextCueHeader(webvttData);
  if (cueHeaderMatcher == null) {
    // No cues found. Don't output a sample, but still output a corresponding track.
    buildTrackOutput(0);
    return;
  }

  long firstCueTimeUs = WebvttParserUtil.parseTimestampUs(cueHeaderMatcher.group(1));
  long sampleTimeUs = timestampAdjuster.adjustTsTimestamp(
      TimestampAdjuster.usToPts(firstCueTimeUs + tsTimestampUs - vttTimestampUs));
  long subsampleOffsetUs = sampleTimeUs - firstCueTimeUs;
  // Output the track.
  TrackOutput trackOutput = buildTrackOutput(subsampleOffsetUs);
  // Output the sample.
  sampleDataWrapper.reset(sampleData, sampleSize);
  trackOutput.sampleData(sampleDataWrapper, sampleSize);
  trackOutput.sampleMetadata(sampleTimeUs, C.BUFFER_FLAG_KEY_FRAME, sampleSize, 0, null);
}
 
Example #11
Source File: WebvttExtractor.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
private void processSample() throws ParserException {
  ParsableByteArray webvttData = new ParsableByteArray(sampleData);

  // Validate the first line of the header.
  try {
    WebvttParserUtil.validateWebvttHeaderLine(webvttData);
  } catch (SubtitleDecoderException e) {
    throw new ParserException(e);
  }

  // Defaults to use if the header doesn't contain an X-TIMESTAMP-MAP header.
  long vttTimestampUs = 0;
  long tsTimestampUs = 0;

  // Parse the remainder of the header looking for X-TIMESTAMP-MAP.
  String line;
  while (!TextUtils.isEmpty(line = webvttData.readLine())) {
    if (line.startsWith("X-TIMESTAMP-MAP")) {
      Matcher localTimestampMatcher = LOCAL_TIMESTAMP.matcher(line);
      if (!localTimestampMatcher.find()) {
        throw new ParserException("X-TIMESTAMP-MAP doesn't contain local timestamp: " + line);
      }
      Matcher mediaTimestampMatcher = MEDIA_TIMESTAMP.matcher(line);
      if (!mediaTimestampMatcher.find()) {
        throw new ParserException("X-TIMESTAMP-MAP doesn't contain media timestamp: " + line);
      }
      vttTimestampUs = WebvttParserUtil.parseTimestampUs(localTimestampMatcher.group(1));
      tsTimestampUs = TimestampAdjuster.ptsToUs(Long.parseLong(mediaTimestampMatcher.group(1)));
    }
  }

  // Find the first cue header and parse the start time.
  Matcher cueHeaderMatcher = WebvttParserUtil.findNextCueHeader(webvttData);
  if (cueHeaderMatcher == null) {
    // No cues found. Don't output a sample, but still output a corresponding track.
    buildTrackOutput(0);
    return;
  }

  long firstCueTimeUs = WebvttParserUtil.parseTimestampUs(cueHeaderMatcher.group(1));
  long sampleTimeUs = timestampAdjuster.adjustTsTimestamp(
      TimestampAdjuster.usToPts(firstCueTimeUs + tsTimestampUs - vttTimestampUs));
  long subsampleOffsetUs = sampleTimeUs - firstCueTimeUs;
  // Output the track.
  TrackOutput trackOutput = buildTrackOutput(subsampleOffsetUs);
  // Output the sample.
  sampleDataWrapper.reset(sampleData, sampleSize);
  trackOutput.sampleData(sampleDataWrapper, sampleSize);
  trackOutput.sampleMetadata(sampleTimeUs, C.BUFFER_FLAG_KEY_FRAME, sampleSize, 0, null);
}
 
Example #12
Source File: WebvttExtractor.java    From K-Sonic with MIT License 4 votes vote down vote up
private void processSample() throws ParserException {
  ParsableByteArray webvttData = new ParsableByteArray(sampleData);

  // Validate the first line of the header.
  try {
    WebvttParserUtil.validateWebvttHeaderLine(webvttData);
  } catch (SubtitleDecoderException e) {
    throw new ParserException(e);
  }

  // Defaults to use if the header doesn't contain an X-TIMESTAMP-MAP header.
  long vttTimestampUs = 0;
  long tsTimestampUs = 0;

  // Parse the remainder of the header looking for X-TIMESTAMP-MAP.
  String line;
  while (!TextUtils.isEmpty(line = webvttData.readLine())) {
    if (line.startsWith("X-TIMESTAMP-MAP")) {
      Matcher localTimestampMatcher = LOCAL_TIMESTAMP.matcher(line);
      if (!localTimestampMatcher.find()) {
        throw new ParserException("X-TIMESTAMP-MAP doesn't contain local timestamp: " + line);
      }
      Matcher mediaTimestampMatcher = MEDIA_TIMESTAMP.matcher(line);
      if (!mediaTimestampMatcher.find()) {
        throw new ParserException("X-TIMESTAMP-MAP doesn't contain media timestamp: " + line);
      }
      vttTimestampUs = WebvttParserUtil.parseTimestampUs(localTimestampMatcher.group(1));
      tsTimestampUs = TimestampAdjuster.ptsToUs(
          Long.parseLong(mediaTimestampMatcher.group(1)));
    }
  }

  // Find the first cue header and parse the start time.
  Matcher cueHeaderMatcher = WebvttParserUtil.findNextCueHeader(webvttData);
  if (cueHeaderMatcher == null) {
    // No cues found. Don't output a sample, but still output a corresponding track.
    buildTrackOutput(0);
    return;
  }

  long firstCueTimeUs = WebvttParserUtil.parseTimestampUs(cueHeaderMatcher.group(1));
  long sampleTimeUs = timestampAdjuster.adjustSampleTimestamp(
      firstCueTimeUs + tsTimestampUs - vttTimestampUs);
  long subsampleOffsetUs = sampleTimeUs - firstCueTimeUs;
  // Output the track.
  TrackOutput trackOutput = buildTrackOutput(subsampleOffsetUs);
  // Output the sample.
  sampleDataWrapper.reset(sampleData, sampleSize);
  trackOutput.sampleData(sampleDataWrapper, sampleSize);
  trackOutput.sampleMetadata(sampleTimeUs, C.BUFFER_FLAG_KEY_FRAME, sampleSize, 0, null);
}
 
Example #13
Source File: WebvttExtractor.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
private void processSample() throws ParserException {
  ParsableByteArray webvttData = new ParsableByteArray(sampleData);

  // Validate the first line of the header.
  WebvttParserUtil.validateWebvttHeaderLine(webvttData);

  // Defaults to use if the header doesn't contain an X-TIMESTAMP-MAP header.
  long vttTimestampUs = 0;
  long tsTimestampUs = 0;

  // Parse the remainder of the header looking for X-TIMESTAMP-MAP.
  String line;
  while (!TextUtils.isEmpty(line = webvttData.readLine())) {
    if (line.startsWith("X-TIMESTAMP-MAP")) {
      Matcher localTimestampMatcher = LOCAL_TIMESTAMP.matcher(line);
      if (!localTimestampMatcher.find()) {
        throw new ParserException("X-TIMESTAMP-MAP doesn't contain local timestamp: " + line);
      }
      Matcher mediaTimestampMatcher = MEDIA_TIMESTAMP.matcher(line);
      if (!mediaTimestampMatcher.find()) {
        throw new ParserException("X-TIMESTAMP-MAP doesn't contain media timestamp: " + line);
      }
      vttTimestampUs = WebvttParserUtil.parseTimestampUs(localTimestampMatcher.group(1));
      tsTimestampUs = TimestampAdjuster.ptsToUs(Long.parseLong(mediaTimestampMatcher.group(1)));
    }
  }

  // Find the first cue header and parse the start time.
  Matcher cueHeaderMatcher = WebvttParserUtil.findNextCueHeader(webvttData);
  if (cueHeaderMatcher == null) {
    // No cues found. Don't output a sample, but still output a corresponding track.
    buildTrackOutput(0);
    return;
  }

  long firstCueTimeUs = WebvttParserUtil.parseTimestampUs(cueHeaderMatcher.group(1));
  long sampleTimeUs = timestampAdjuster.adjustTsTimestamp(
      TimestampAdjuster.usToPts(firstCueTimeUs + tsTimestampUs - vttTimestampUs));
  long subsampleOffsetUs = sampleTimeUs - firstCueTimeUs;
  // Output the track.
  TrackOutput trackOutput = buildTrackOutput(subsampleOffsetUs);
  // Output the sample.
  sampleDataWrapper.reset(sampleData, sampleSize);
  trackOutput.sampleData(sampleDataWrapper, sampleSize);
  trackOutput.sampleMetadata(sampleTimeUs, C.BUFFER_FLAG_KEY_FRAME, sampleSize, 0, null);
}
 
Example #14
Source File: WebvttExtractor.java    From Telegram with GNU General Public License v2.0 4 votes vote down vote up
private void processSample() throws ParserException {
  ParsableByteArray webvttData = new ParsableByteArray(sampleData);

  // Validate the first line of the header.
  WebvttParserUtil.validateWebvttHeaderLine(webvttData);

  // Defaults to use if the header doesn't contain an X-TIMESTAMP-MAP header.
  long vttTimestampUs = 0;
  long tsTimestampUs = 0;

  // Parse the remainder of the header looking for X-TIMESTAMP-MAP.
  String line;
  while (!TextUtils.isEmpty(line = webvttData.readLine())) {
    if (line.startsWith("X-TIMESTAMP-MAP")) {
      Matcher localTimestampMatcher = LOCAL_TIMESTAMP.matcher(line);
      if (!localTimestampMatcher.find()) {
        throw new ParserException("X-TIMESTAMP-MAP doesn't contain local timestamp: " + line);
      }
      Matcher mediaTimestampMatcher = MEDIA_TIMESTAMP.matcher(line);
      if (!mediaTimestampMatcher.find()) {
        throw new ParserException("X-TIMESTAMP-MAP doesn't contain media timestamp: " + line);
      }
      vttTimestampUs = WebvttParserUtil.parseTimestampUs(localTimestampMatcher.group(1));
      tsTimestampUs = TimestampAdjuster.ptsToUs(Long.parseLong(mediaTimestampMatcher.group(1)));
    }
  }

  // Find the first cue header and parse the start time.
  Matcher cueHeaderMatcher = WebvttParserUtil.findNextCueHeader(webvttData);
  if (cueHeaderMatcher == null) {
    // No cues found. Don't output a sample, but still output a corresponding track.
    buildTrackOutput(0);
    return;
  }

  long firstCueTimeUs = WebvttParserUtil.parseTimestampUs(cueHeaderMatcher.group(1));
  long sampleTimeUs = timestampAdjuster.adjustTsTimestamp(
      TimestampAdjuster.usToPts(firstCueTimeUs + tsTimestampUs - vttTimestampUs));
  long subsampleOffsetUs = sampleTimeUs - firstCueTimeUs;
  // Output the track.
  TrackOutput trackOutput = buildTrackOutput(subsampleOffsetUs);
  // Output the sample.
  sampleDataWrapper.reset(sampleData, sampleSize);
  trackOutput.sampleData(sampleDataWrapper, sampleSize);
  trackOutput.sampleMetadata(sampleTimeUs, C.BUFFER_FLAG_KEY_FRAME, sampleSize, 0, null);
}