mirror of
https://github.com/samsonjs/media.git
synced 2026-03-29 10:05:48 +00:00
Further simplify SRT support
This commit is contained in:
parent
fbbf3f27fd
commit
1b1769bb6d
5 changed files with 41 additions and 62 deletions
|
|
@ -132,7 +132,7 @@ public final class VideoFormatSelectorUtil {
|
|||
}
|
||||
}
|
||||
|
||||
return Util.toArray(selectedIndexList);
|
||||
return Util.toIntArray(selectedIndexList);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -1,34 +0,0 @@
|
|||
/*
|
||||
* Copyright (C) 2014 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.google.android.exoplayer.text.subrip;
|
||||
|
||||
import com.google.android.exoplayer.text.Cue;
|
||||
|
||||
/**
|
||||
* A representation of a SubRip cue.
|
||||
*/
|
||||
/* package */ final class SubripCue extends Cue {
|
||||
|
||||
public final long startTime;
|
||||
public final long endTime;
|
||||
|
||||
public SubripCue(long startTime, long endTime, CharSequence text) {
|
||||
super(text);
|
||||
this.startTime = startTime;
|
||||
this.endTime = endTime;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -17,8 +17,10 @@ package com.google.android.exoplayer.text.subrip;
|
|||
|
||||
import com.google.android.exoplayer.C;
|
||||
import com.google.android.exoplayer.ParserException;
|
||||
import com.google.android.exoplayer.text.Cue;
|
||||
import com.google.android.exoplayer.text.SubtitleParser;
|
||||
import com.google.android.exoplayer.util.MimeTypes;
|
||||
import com.google.android.exoplayer.util.Util;
|
||||
|
||||
import android.text.Html;
|
||||
import android.text.Spanned;
|
||||
|
|
@ -52,7 +54,8 @@ public final class SubripParser implements SubtitleParser {
|
|||
@Override
|
||||
public SubripSubtitle parse(InputStream inputStream, String inputEncoding, long startTimeUs)
|
||||
throws IOException {
|
||||
ArrayList<SubripCue> cues = new ArrayList<>();
|
||||
ArrayList<Cue> cues = new ArrayList<>();
|
||||
ArrayList<Long> cueTimesUs = new ArrayList<>();
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, C.UTF8_NAME));
|
||||
String currentLine;
|
||||
|
||||
|
|
@ -65,13 +68,11 @@ public final class SubripParser implements SubtitleParser {
|
|||
}
|
||||
|
||||
// Read and parse the timing line.
|
||||
long cueStartTimeUs;
|
||||
long cueEndTimeUs;
|
||||
currentLine = reader.readLine();
|
||||
Matcher matcher = SUBRIP_TIMING_LINE.matcher(currentLine);
|
||||
if (matcher.find()) {
|
||||
cueStartTimeUs = parseTimestampUs(matcher.group(1)) + startTimeUs;
|
||||
cueEndTimeUs = parseTimestampUs(matcher.group(2)) + startTimeUs;
|
||||
cueTimesUs.add(startTimeUs + parseTimestampUs(matcher.group(1)));
|
||||
cueTimesUs.add(startTimeUs + parseTimestampUs(matcher.group(2)));
|
||||
} else {
|
||||
throw new ParserException("Expected timing line: " + currentLine);
|
||||
}
|
||||
|
|
@ -86,14 +87,16 @@ public final class SubripParser implements SubtitleParser {
|
|||
}
|
||||
|
||||
Spanned text = Html.fromHtml(textBuilder.toString());
|
||||
SubripCue cue = new SubripCue(cueStartTimeUs, cueEndTimeUs, text);
|
||||
cues.add(cue);
|
||||
cues.add(new Cue(text));
|
||||
}
|
||||
|
||||
reader.close();
|
||||
inputStream.close();
|
||||
SubripSubtitle subtitle = new SubripSubtitle(cues, startTimeUs);
|
||||
return subtitle;
|
||||
|
||||
Cue[] cuesArray = new Cue[cues.size()];
|
||||
cues.toArray(cuesArray);
|
||||
long[] cueTimesUsArray = Util.toLongArray(cueTimesUs);
|
||||
return new SubripSubtitle(startTimeUs, cuesArray, cueTimesUsArray);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -28,27 +28,20 @@ import java.util.List;
|
|||
*/
|
||||
/* package */ final class SubripSubtitle implements Subtitle {
|
||||
|
||||
private final List<SubripCue> cues;
|
||||
private final int numCues;
|
||||
private final long startTimeUs;
|
||||
|
||||
private final Cue[] cues;
|
||||
private final long[] cueTimesUs;
|
||||
|
||||
/**
|
||||
* @param cues A list of the cues in this subtitle.
|
||||
* @param startTimeUs The start time of the subtitle.
|
||||
* @param startTimeUs The start time of the subtitle, in microseconds.
|
||||
* @param cues The cues in the subtitle.
|
||||
* @param cueTimesUs Interleaved cue start and end times, in microseconds.
|
||||
*/
|
||||
public SubripSubtitle(List<SubripCue> cues, long startTimeUs) {
|
||||
this.cues = cues;
|
||||
public SubripSubtitle(long startTimeUs, Cue[] cues, long[] cueTimesUs) {
|
||||
this.startTimeUs = startTimeUs;
|
||||
|
||||
numCues = cues.size();
|
||||
cueTimesUs = new long[2 * numCues];
|
||||
for (int cueIndex = 0; cueIndex < numCues; cueIndex++) {
|
||||
SubripCue cue = cues.get(cueIndex);
|
||||
int arrayIndex = cueIndex * 2;
|
||||
cueTimesUs[arrayIndex] = cue.startTime;
|
||||
cueTimesUs[arrayIndex + 1] = cue.endTime;
|
||||
}
|
||||
this.cues = cues;
|
||||
this.cueTimesUs = cueTimesUs;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -58,7 +51,6 @@ import java.util.List;
|
|||
|
||||
@Override
|
||||
public int getNextEventTimeIndex(long timeUs) {
|
||||
Assertions.checkArgument(timeUs >= 0);
|
||||
int index = Util.binarySearchCeil(cueTimesUs, timeUs, false, false);
|
||||
return index < cueTimesUs.length ? index : -1;
|
||||
}
|
||||
|
|
@ -90,7 +82,7 @@ import java.util.List;
|
|||
// timeUs is earlier than the start of the first cue, or corresponds to a gap between cues.
|
||||
return Collections.<Cue>emptyList();
|
||||
} else {
|
||||
return Collections.singletonList((Cue) cues.get(index / 2));
|
||||
return Collections.singletonList(cues[index / 2]);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -459,7 +459,7 @@ public final class Util {
|
|||
* @param list A list of integers.
|
||||
* @return The list in array form, or null if the input list was null.
|
||||
*/
|
||||
public static int[] toArray(List<Integer> list) {
|
||||
public static int[] toIntArray(List<Integer> list) {
|
||||
if (list == null) {
|
||||
return null;
|
||||
}
|
||||
|
|
@ -471,6 +471,24 @@ public final class Util {
|
|||
return intArray;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a list of longs to a primitive array.
|
||||
*
|
||||
* @param list A list of longs.
|
||||
* @return The list in array form, or null if the input list was null.
|
||||
*/
|
||||
public static long[] toLongArray(List<Long> list) {
|
||||
if (list == null) {
|
||||
return null;
|
||||
}
|
||||
int length = list.size();
|
||||
long[] longArray = new long[length];
|
||||
for (int i = 0; i < length; i++) {
|
||||
longArray[i] = list.get(i);
|
||||
}
|
||||
return longArray;
|
||||
}
|
||||
|
||||
/**
|
||||
* On platform API levels 19 and 20, okhttp's implementation of {@link InputStream#close} can
|
||||
* block for a long time if the stream has a lot of data remaining. Call this method before
|
||||
|
|
|
|||
Loading…
Reference in a new issue