Search in sources :

Example 36 with Cue

use of com.google.android.exoplayer2.text.Cue in project ExoPlayer by google.

the class TtmlDecoderTest method testFontSizeWithInvalidValueIsIgnored.

public void testFontSizeWithInvalidValueIsIgnored() throws IOException, SubtitleDecoderException {
    TtmlSubtitle subtitle = getSubtitle(FONT_SIZE_INVALID_TTML_FILE);
    assertEquals(6, subtitle.getEventTimeCount());
    List<Cue> cues = subtitle.getCues(10 * 1000000);
    assertEquals(1, cues.size());
    SpannableStringBuilder spannable = (SpannableStringBuilder) cues.get(0).text;
    assertEquals("invalid", String.valueOf(spannable));
    assertEquals(0, spannable.getSpans(0, spannable.length(), RelativeSizeSpan.class).length);
    assertEquals(0, spannable.getSpans(0, spannable.length(), AbsoluteSizeSpan.class).length);
    cues = subtitle.getCues(20 * 1000000);
    assertEquals(1, cues.size());
    spannable = (SpannableStringBuilder) cues.get(0).text;
    assertEquals("invalid", String.valueOf(spannable));
    assertEquals(0, spannable.getSpans(0, spannable.length(), RelativeSizeSpan.class).length);
    assertEquals(0, spannable.getSpans(0, spannable.length(), AbsoluteSizeSpan.class).length);
    cues = subtitle.getCues(30 * 1000000);
    assertEquals(1, cues.size());
    spannable = (SpannableStringBuilder) cues.get(0).text;
    assertEquals("invalid dot", String.valueOf(spannable));
    assertEquals(0, spannable.getSpans(0, spannable.length(), RelativeSizeSpan.class).length);
    assertEquals(0, spannable.getSpans(0, spannable.length(), AbsoluteSizeSpan.class).length);
}
Also used : Cue(com.google.android.exoplayer2.text.Cue) SpannableStringBuilder(android.text.SpannableStringBuilder)

Example 37 with Cue

use of com.google.android.exoplayer2.text.Cue in project ExoPlayer by google.

the class Mp4WebvttDecoderTest method testTwoCuesSample.

public void testTwoCuesSample() throws SubtitleDecoderException {
    Mp4WebvttDecoder decoder = new Mp4WebvttDecoder();
    Subtitle result = decoder.decode(DOUBLE_CUE_SAMPLE, DOUBLE_CUE_SAMPLE.length);
    Cue firstExpectedCue = new Cue("Hello World");
    Cue secondExpectedCue = new Cue("Bye Bye");
    assertMp4WebvttSubtitleEquals(result, firstExpectedCue, secondExpectedCue);
}
Also used : Subtitle(com.google.android.exoplayer2.text.Subtitle) Cue(com.google.android.exoplayer2.text.Cue)

Example 38 with Cue

use of com.google.android.exoplayer2.text.Cue in project ExoPlayer by google.

the class SubripDecoder method decode.

@Override
protected SubripSubtitle decode(byte[] bytes, int length) {
    ArrayList<Cue> cues = new ArrayList<>();
    LongArray cueTimesUs = new LongArray();
    ParsableByteArray subripData = new ParsableByteArray(bytes, length);
    String currentLine;
    while ((currentLine = subripData.readLine()) != null) {
        if (currentLine.length() == 0) {
            // Skip blank lines.
            continue;
        }
        // Parse the index line as a sanity check.
        try {
            Integer.parseInt(currentLine);
        } catch (NumberFormatException e) {
            Log.w(TAG, "Skipping invalid index: " + currentLine);
            continue;
        }
        // Read and parse the timing line.
        boolean haveEndTimecode = false;
        currentLine = subripData.readLine();
        Matcher matcher = SUBRIP_TIMING_LINE.matcher(currentLine);
        if (matcher.matches()) {
            cueTimesUs.add(parseTimecode(matcher, 1));
            if (!TextUtils.isEmpty(matcher.group(6))) {
                haveEndTimecode = true;
                cueTimesUs.add(parseTimecode(matcher, 6));
            }
        } else {
            Log.w(TAG, "Skipping invalid timing: " + currentLine);
            continue;
        }
        // Read and parse the text.
        textBuilder.setLength(0);
        while (!TextUtils.isEmpty(currentLine = subripData.readLine())) {
            if (textBuilder.length() > 0) {
                textBuilder.append("<br>");
            }
            textBuilder.append(currentLine.trim());
        }
        Spanned text = Html.fromHtml(textBuilder.toString());
        cues.add(new Cue(text));
        if (haveEndTimecode) {
            cues.add(null);
        }
    }
    Cue[] cuesArray = new Cue[cues.size()];
    cues.toArray(cuesArray);
    long[] cueTimesUsArray = cueTimesUs.toArray();
    return new SubripSubtitle(cuesArray, cueTimesUsArray);
}
Also used : Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) Spanned(android.text.Spanned) LongArray(com.google.android.exoplayer2.util.LongArray) ParsableByteArray(com.google.android.exoplayer2.util.ParsableByteArray) Cue(com.google.android.exoplayer2.text.Cue)

Example 39 with Cue

use of com.google.android.exoplayer2.text.Cue in project ExoPlayer by google.

the class TtmlNode method getCues.

public List<Cue> getCues(long timeUs, Map<String, TtmlStyle> globalStyles, Map<String, TtmlRegion> regionMap) {
    TreeMap<String, SpannableStringBuilder> regionOutputs = new TreeMap<>();
    traverseForText(timeUs, false, regionId, regionOutputs);
    traverseForStyle(globalStyles, regionOutputs);
    List<Cue> cues = new ArrayList<>();
    for (Entry<String, SpannableStringBuilder> entry : regionOutputs.entrySet()) {
        TtmlRegion region = regionMap.get(entry.getKey());
        cues.add(new Cue(cleanUpText(entry.getValue()), null, region.line, region.lineType, Cue.TYPE_UNSET, region.position, Cue.TYPE_UNSET, region.width));
    }
    return cues;
}
Also used : Cue(com.google.android.exoplayer2.text.Cue) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) SpannableStringBuilder(android.text.SpannableStringBuilder)

Example 40 with Cue

use of com.google.android.exoplayer2.text.Cue in project ExoPlayer by google.

the class WebvttDecoder method decode.

@Override
protected WebvttSubtitle decode(byte[] bytes, int length) 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.
    WebvttParserUtil.validateWebvttHeaderLine(parsableWebvttData);
    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.");
            }
            // Consume the "STYLE" header.
            parsableWebvttData.readLine();
            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);
}
Also used : ArrayList(java.util.ArrayList) SubtitleDecoderException(com.google.android.exoplayer2.text.SubtitleDecoderException)

Aggregations

Cue (com.google.android.exoplayer2.text.Cue)58 Test (org.junit.Test)40 Subtitle (com.google.android.exoplayer2.text.Subtitle)13 ArrayList (java.util.ArrayList)12 Nullable (androidx.annotation.Nullable)10 Spanned (android.text.Spanned)8 SpannableStringBuilder (android.text.SpannableStringBuilder)7 ParsableByteArray (com.google.android.exoplayer2.util.ParsableByteArray)5 FakeExtractorOutput (com.google.android.exoplayer2.testutil.FakeExtractorOutput)4 SpannableString (android.text.SpannableString)3 UnderlineSpan (android.text.style.UnderlineSpan)3 Format (com.google.android.exoplayer2.Format)3 AdPlaybackState (com.google.android.exoplayer2.source.ads.AdPlaybackState)3 FakeExtractorInput (com.google.android.exoplayer2.testutil.FakeExtractorInput)3 FakeTrackOutput (com.google.android.exoplayer2.testutil.FakeTrackOutput)3 HorizontalTextInVerticalContextSpan (com.google.android.exoplayer2.text.span.HorizontalTextInVerticalContextSpan)3 RubySpan (com.google.android.exoplayer2.text.span.RubySpan)3 TextEmphasisSpan (com.google.android.exoplayer2.text.span.TextEmphasisSpan)3 WebvttDecoder (com.google.android.exoplayer2.text.webvtt.WebvttDecoder)3 Matcher (java.util.regex.Matcher)3